Python unittest.TestCase object has no attribute 'runTest'

前端 未结 4 1981
执笔经年
执笔经年 2020-12-30 22:28

For the following code:

import unittest

class Test(unittest.TestCase):
    def test1(self):
        assert(True == True)

if __name__ == \"__main__\":
    s         


        
相关标签:
4条回答
  • 2020-12-30 23:14

    You can run it like this:

    python -m unittest <your-module-name>
    

    I don't fully understand why it works though.

    0 讨论(0)
  • 2020-12-30 23:15

    You need to invoke a TestLoader:

    if __name__ == "__main__":
        suite = unittest.defaultTestLoader.loadTestsFromTestCase(Test)
        unittest.TextTestRunner().run(suite)
    
    0 讨论(0)
  • 2020-12-30 23:18

    You have to specify the test method name (test1):

    import unittest
    
    class Test(unittest.TestCase):
        def test1(self):
            assert(True == True)
    
    if __name__ == "__main__":
        suite = unittest.TestSuite()
        suite.addTest(Test('test1')) # <----------------
        unittest.TextTestRunner().run(suite)
    

    Or, if you want to run all tests in the file, Just calling unittest.main() is enough:

    if __name__ == "__main__":
        unittest.main()
    
    0 讨论(0)
  • 2020-12-30 23:18

    The actual test for any TestCase subclass is performed in the runTest() method. Simply change your code to:

    class Test(unittest.TestCase):
        def runTest(self):
            assert(True == True)
    
    0 讨论(0)
提交回复
热议问题