Unittest tests order

后端 未结 19 1313
刺人心
刺人心 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 03:47

    http://docs.python.org/library/unittest.html

    Note that the order in which the various test cases will be run is determined by sorting the test function names with respect to the built-in ordering for strings.

    If you need set order explicitly, use a monolithic test.

    class Monolithic(TestCase):
      def step1(self):
          ...
    
      def step2(self):
          ...
    
      def steps(self):
        for name in sorted(dir(self)):
          if name.startswith("step"):
            yield name, getattr(self, name) 
    
      def test_steps(self):
        for name, step in self.steps():
          try:
            step()
          except Exception as e:
            self.fail("{} failed ({}: {})".format(step, type(e), e)
    

    Check out post for details.

提交回复
热议问题