How do I run multiple Classes in a single test suite in Python using unit testing?

后端 未结 5 931
无人共我
无人共我 2021-02-01 03:09

How do I run multiple Classes in a single test suite in Python using unit testing?

相关标签:
5条回答
  • 2021-02-01 03:28

    If you want to run all of the tests from a specific list of test classes, rather than all of the tests from all of the test classes in a module, you can use a TestLoader's loadTestsFromTestCase method to get a TestSuite of tests for each class, and then create a single combined TestSuite from a list containing all of those suites that you can use with run:

    import unittest
    
    # Some tests
    
    class TestClassA(unittest.TestCase):
        def testOne(self):
            # test code
            pass
    
    class TestClassB(unittest.TestCase):
        def testOne(self):
            # test code
            pass
    
    class TestClassC(unittest.TestCase):
        def testOne(self):
            # test code
            pass
    
    def run_some_tests():
        # Run only the tests in the specified classes
    
        test_classes_to_run = [TestClassA, TestClassC]
    
        loader = unittest.TestLoader()
    
        suites_list = []
        for test_class in test_classes_to_run:
            suite = loader.loadTestsFromTestCase(test_class)
            suites_list.append(suite)
            
        big_suite = unittest.TestSuite(suites_list)
    
        runner = unittest.TextTestRunner()
        results = runner.run(big_suite)
    
        # ...
    
    if __name__ == '__main__':
        run_some_tests()
    
    0 讨论(0)
  • 2021-02-01 03:29

    I'm a bit unsure at what you're asking here, but if you want to know how to test multiple classes in the same suite, usually you just create multiple testclasses in the same python file and run them together:

    import unittest
    
    class TestSomeClass(unittest.TestCase):
        def testStuff(self):
                # your testcode here
                pass
    
    class TestSomeOtherClass(unittest.TestCase):
        def testOtherStuff(self):
                # testcode of second class here
                pass
    
    if __name__ == '__main__':
        unittest.main()
    

    And run with for example:

    python mytestsuite.py
    

    Better examples can be found in the official documention.

    If on the other hand you want to run multiple test files, as detailed in "How to organize python test in a way that I can run all tests in a single command?", then the other answer is probably better.

    0 讨论(0)
  • 2021-02-01 03:30

    I've found nose to be a good tool for this. It discovers all unit tests in a directory structure and executes them.

    0 讨论(0)
  • 2021-02-01 03:39

    The unittest.TestLoader.loadTestsFromModule() method will discover and load all classes in the specified module. So you can just do this:

    import unittest
    import sys
    
    class T1(unittest.TestCase):
      def test_A(self):
        pass
      def test_B(self):
        pass
    
    class T2(unittest.TestCase):
      def test_A(self):
        pass
      def test_B(self):
        pass
    
    if __name__ == "__main__":
      suite = unittest.TestLoader().loadTestsFromModule( sys.modules[__name__] )
      unittest.TextTestRunner(verbosity=3).run( suite )
    
    0 讨论(0)
  • 2021-02-01 03:41

    Normally you would do in the following way (which adds only one class per suite):

    # Add tests.
    alltests = unittest.TestSuite()
    alltests.addTest(unittest.makeSuite(Test1))
    alltests.addTest(unittest.makeSuite(Test2))
    

    If you'd like to have multiple classes per suite, you can use add these tests in the following way:

    for name in testnames:
        suite.addTest(tc_class(name, cargs=args))
    

    Here is same example to run all classes per separate suite you can define your own make_suite method:

    # Credits: http://codereview.stackexchange.com/a/88662/15346
    def make_suite(tc_class):
        testloader = unittest.TestLoader()
        testnames = testloader.getTestCaseNames(tc_class)
        suite = unittest.TestSuite()
        for name in testnames:
            suite.addTest(tc_class(name, cargs=args))
        return suite
    
    # Add all tests.
    alltests = unittest.TestSuite()
    for name, obj in inspect.getmembers(sys.modules[__name__]):
        if inspect.isclass(obj) and name.startswith("FooTest"):
            alltests.addTest(make_suite(obj))
    
    result = unittest.TextTestRunner(verbosity=2).run(alltests)
    

    If above doesn't suite, you can convert above example into method which could accept multiple classes.

    0 讨论(0)
提交回复
热议问题