2017-10-09 314 views
0

我開始學習TDD。我剛剛從python開始進行單元測試。當我嘗試執行:Python unitest不起作用

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

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

OK 

我在其他links讀過,我需要在一開始與test_重命名自己的功能。然而,這正是我所做的,但它仍然不起作用。

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() 

和python_daily.py文件:

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

def get_string(number): 
    return 'Hello' 

有什麼不好?

回答

1

如果您python_daily.py Python模塊是:

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


def get_string(number): 
    return 'Hello' 

和你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() 

你應該有:

$ 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) 

注意你的縮進源代碼!

+0

所有來自C++的典型錯誤。我瞎了。非常感謝 – Maverick94