2017-10-09 4 views
0

Je commence à apprendre le TDD. Je viens de commencer avec un test unitaire de python. Lorsque je tente d'exécuter:Python unitest ne fonctionne pas

[email protected]:~/pruebaTestPython$ python test_python_daily_software.py 

---------------------------------------------------------------------- 
Ran 0 tests in 0.000s 

OK 

J'ai lu dans d'autres links que je dois renommer mes propres fonctions avec test_ au début. Cependant, c'est exactement ce que j'ai fait, mais ça ne marche toujours pas.

fichier test_python_daily_software.py:

#!/usr/bin/python 
# -*- coding: utf-8 -*- 

import unittest 
import python_daily 

class TestPythonSoftware(unittest.TestCase): 

    def test_should_return_python_when_number_is_3(self): 
     self.assertEqual('Python', python_daily.get_string(3)) 

    def test_should_return_daily_when_number_is_5(self): 
     self.assertEqual('Daily', python_daily.get_string(5)) 

    if __name__ == '__main__': 
     unittest.main() 

et fichier python_daily.py:

#!/usr/bin/python 
# -*- coding: utf-8 -*- 

def get_string(number): 
    return 'Hello' 

ce qui est faux?

Répondre

1

Si votre python_daily.py le module Python est:

#!/usr/bin/python 
# -*- coding: utf-8 -*- 


def get_string(number): 
    return 'Hello' 

et votre module de test test_python_daily_software.py est:

#!/usr/bin/python 
# -*- coding: utf-8 -*- 

import unittest 

import python_daily 


class TestPythonSoftware(unittest.TestCase): 
    def test_should_return_python_when_number_is_3(self): 
     self.assertEqual('Python', python_daily.get_string(3)) 

    def test_should_return_daily_when_number_is_5(self): 
     self.assertEqual('Daily', python_daily.get_string(5)) 


if __name__ == '__main__': 
    unittest.main() 

Vous devriez avoir:

$ python test_python_daily_software.py 
FF 
====================================================================== 
FAIL: test_should_return_daily_when_number_is_5 (__main__.TestPythonSoftware) 
---------------------------------------------------------------------- 
Traceback (most recent call last): 
    File "test_python_daily_software.py", line 11, in test_should_return_daily_when_number_is_5 
    self.assertEqual('Daily', python_daily.get_string(5)) 
AssertionError: 'Daily' != 'Hello' 

====================================================================== 
FAIL: test_should_return_python_when_number_is_3 (__main__.TestPythonSoftware) 
---------------------------------------------------------------------- 
Traceback (most recent call last): 
    File "test_python_daily_software.py", line 8, in test_should_return_python_when_number_is_3 
    self.assertEqual('Python', python_daily.get_string(3)) 
AssertionError: 'Python' != 'Hello' 

---------------------------------------------------------------------- 
Ran 2 tests in 0.000s 

FAILED (failures=2) 

esprit votre empreinte dans le code source!

+0

Erreur typique pour tout ce qui vient de C++. Je suis aveugle. Merci beaucoup – Maverick94