【问题标题】:Python unitest doesn't workPython单元测试不起作用
【发布时间】:2017-10-09 08:52:42
【问题描述】:

我开始学习 TDD。我刚刚开始从 python 进行单元测试。当我尝试执行时:

vagrant@vagrant:~/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'

怎么了?

【问题讨论】:

    标签: python unit-testing tdd


    【解决方案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)
    

    注意源代码中的缩进!

    【讨论】:

    • 来自 C++ 的所有内容的典型错误。我瞎了。非常感谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-23
    • 2013-02-20
    • 1970-01-01
    • 2023-03-11
    • 2021-06-26
    • 1970-01-01
    相关资源
    最近更新 更多