Unittest tests order

后端 未结 19 1288
刺人心
刺人心 2020-12-01 03:17

How do I be sure of the unittest methods order? Is the alphabetical or numeric prefixes the proper way?

class TestFoo(TestCase):
    def test_1(self):
               


        
19条回答
  •  栀梦
    栀梦 (楼主)
    2020-12-01 04:03

    Here is a simpler method that has the following advantages:

    • No need to create a custom TestCase class.
    • No need to decorate every test method.
    • Use the unittest standard load test protocol. See the Python docs here.

    The idea is to go through all the test cases of the test suites given to the test loader protocol and create a new suite but with the tests ordered by their line number.

    Here is the code:

    import unittest
    
    def load_ordered_tests(loader, standard_tests, pattern):
        """
        Test loader that keeps the tests in the order they were declared in the class.
        """
        ordered_cases = []
        for test_suite in standard_tests:
            ordered = []
            for test_case in test_suite:
                test_case_type = type(test_case)
                method_name = test_case._testMethodName
                testMethod = getattr(test_case, method_name)
                line = testMethod.__code__.co_firstlineno
                ordered.append( (line, test_case_type, method_name) )
            ordered.sort()
            for line, case_type, name in ordered:
                ordered_cases.append(case_type(name))
        return unittest.TestSuite(ordered_cases)
    

    You can put this in a module named order_tests and then in each unittest Python file, declare the test loader like this:

    from order_tests import load_ordered_tests
    
    # This orders the tests to be run in the order they were declared.
    # It uses the unittest load_tests protocol.
    load_tests = load_ordered_tests
    

    Note: the often suggested technique of setting the test sorter to None no longer works because Python now sorts the output of dir() and unittest uses dir() to find tests. So even though you have no sorting method, they still get sorted by Python itself!

提交回复
热议问题