开发者

What is the right way to organize python unittests into suites?

开发者 https://www.devze.com 2022-12-12 09:47 出处:网络
I have some test case classes organized in directories foo_tests foo_tests1.py foo_tests2.py ... bar_tests

I have some test case classes organized in directories

foo_tests
    foo_tests1.py
    foo_tests2.py
    ...
bar_tests
    bar_tests开发者_如何学C1.py
    ...

The test cases look like:

foo_tests1.py:
import unittest
class FooTestsOne(unittest.TestCase):
    def test_1():
       assert(1=1)
    def test_2():
       #...

How do you organize test suites out of test case classes like this? There are facilities in unittest for TestLoaders and TestSuite objects but where are they declared and used? What I want is to define certain suites in a separate file that i can run the tests with:

suite1.py

import unittest
import foo_test1

suite = unittest.TestSuite((unittest.makeSuite(foo_tests1.FooTestsOne),
                            unittest.makeSuite(foo_tests2.FooTeststwo),
                             ))

if __name__ == "__main__":
    result = unittest.TextTestRunner(verbosity=2).run(suite())
    sys.exit(not result.wasSuccessful())

But this is not the right way to aggregate tests into suites. When I import the testcase class ("import foo_test1") to reference it so I can put it in a larger suite the tests inside are immediately run (during the import). What is the right way to aggregates tests into suites? I need fine grain control as to what tests go into which suites... I've read the TestSuite documentation, but it doesn't seem to provide many examples...


Tests are not supposed to run during import. Maybe you have unittest.main() at the bottom of foo_test1.py?

Your script should work, except that

result = unittest.TextTestRunner(verbosity=2).run(suite())

should be

result = unittest.TextTestRunner(verbosity=2).run(suite)
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号