Python unittest.TestCase object has no attribute 'runTest'

霸气de小男生 提交于 2019-11-30 11:13:21
Janne Karila

You need to invoke a TestLoader:

if __name__ == "__main__":
    suite = unittest.defaultTestLoader.loadTestsFromTestCase(Test)
    unittest.TextTestRunner().run(suite)

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

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)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!