Python unittest and test discovery

前端 未结 4 1751
一生所求
一生所求 2020-12-18 20:47

What exactly I need to do to make python\'s unittest work? I checked the official documentation, SO questions and even tried using nose, b

相关标签:
4条回答
  • 2020-12-18 21:18

    unittest.main() will run all the function that begin by "test". So you should rename your functions

    class EchoTest(unittest.TestCase):  
        def testfoo(self):
            self.assertTrue(1==1)
    
        def testbar(self):
            self.assertTrue(1==2)
    
    0 讨论(0)
  • 2020-12-18 21:20

    You need to rename the methods to begin with the word "test".

    As seen on http://docs.python.org/library/unittest.html :

    A testcase is created by subclassing unittest.TestCase. The three individual tests are defined with methods whose names start with the letters test. This naming convention informs the test runner about which methods represent tests.

    0 讨论(0)
  • 2020-12-18 21:21

    I would like to mention that you CANNOT run a python unit test as though it were an executable python file. For example, this was my problem:

    python -m unittest ./TestMyPythonModule.py
    
    ... (stack trace) ...
    ValueError: Empty module name
    

    It fails.

    However, this works:

    python -m unittest TestMyPythonModule
    

    It's easy to overlook at first.

    0 讨论(0)
  • 2020-12-18 21:41

    Test functions must start with the name test So fooTest should be testFoo. See the docs here

    Also, there isn't a need for the __init__.py file, assuming those are the only two files in your tests directory.

    0 讨论(0)
提交回复
热议问题