Python unit test with base and sub class

前端 未结 15 2361
独厮守ぢ
独厮守ぢ 2020-11-28 18:44

I currently have a few unit tests which share a common set of tests. Here\'s an example:

import unittest

class BaseTest(unittest.TestCase):

    def testCo         


        
15条回答
  •  天命终不由人
    2020-11-28 19:13

    As of Python 3.2, you can add a test_loader function to a module to control which tests (if any) are found by the test discovery mechanism.

    For example, the following will only load the original poster's SubTest1 and SubTest2 Test Cases, ignoring Base:

    def load_tests(loader, standard_tests, pattern):
        suite = TestSuite()
        suite.addTests([SubTest1, SubTest2])
        return suite
    

    It ought to be possible to iterate over standard_tests (a TestSuite containing the tests the default loader found) and copy all but Base to suite instead, but the nested nature of TestSuite.__iter__ makes that a lot more complicated.

提交回复
热议问题