Is it not possible to run test cases directly without putting them into a test suite first?

一个人想着一个人 提交于 2019-12-08 09:45:26

问题


Is it true that unittest can't run test cases directly without putting them into a test suite first? I saw it somewhere, but forgot where I saw it and whether it refers to python's unnittest module or Java's JUnit.

Python's unittest offers two ways for implicitly creating and running test cases, but I am not sure whether they internally use test suite. For example, given a test case class:

# test_module.py

import unittest
class WidgetTestCase(unittest.TestCase):
    def setUp(self):
        self.widget = Widget('The widget')
    def tearDown(self):
        self.widget.dispose()
        self.widget = None
    def test_default_size(self):
        self.assertEqual(self.widget.size(), (50,50),'incorrect default size')
    def test_resize(self):
        self.widget.resize(100,150)
        self.assertEqual(self.widget.size(), (100,150),'wrong size after resize')
    def test_somethingelse(self):
        ...

I can explicitly select some of the test cases to create and run, by test suite

widgetTestSuite = unittest.TestSuite()
widgetTestSuite.addTest(WidgetTestCase('test_default_size'))
widgetTestSuite.addTest(WidgetTestCase('test_resize'))
unittest.TextTestRunner(verbosity=2).run(suite)

Alternatively, I can also implicitly create all the test cases and run them, by either

python3 -m unittest test_module.py

or adding to test_module.py

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

and then

python3 test_module.py

I was wondering if the two implicit methods make use of test suite internally in a way similar to the explicit method? If not, do they create test cases (i.e. instantiate the test case class) and, if yes, how do they run test cases?

来源:https://stackoverflow.com/questions/55622158/is-it-not-possible-to-run-test-cases-directly-without-putting-them-into-a-test-s

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