简体   繁体   English

Python单元测试:TestSuite仅运行第一个TestCase

[英]Python unittest: TestSuite running only first TestCase

Running first_TestCase and second_TestCase separately all works fine. 分别运行first_TestCasesecond_TestCase都可以正常工作。 But when i created TestSuite, it runs only first_TestCase . 但是当我创建TestSuite时,它仅运行first_TestCase Why is this happening? 为什么会这样呢?

import unittest
from first_TestCase import first_TestCase
from second_TestCase import second_TestCase


     def suite():
         suite = unittest.TestSuite()
         suite.addTest(first_TestCase())
         suite.addTest(second_TestCase())
         return suite

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

You're saying: 您是在说:

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

You're loading tests from only first_TestCase right before you run via the TextTestRunner . 通过TextTestRunner运行之前,您仅从first_TestCase加载测试。 You're never hitting that suite() function. 您永远不会碰到suite()函数。

You should do: 你应该做:

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

Because you're not calling the suite() function in your current implementation. 因为您没有在当前的实现中调用suite()函数。

Instead of: 代替:

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

I should use: 我应该使用:

 if __name__ == "__main__":
     unittest.main()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM