Python unittest.TestCase execution order

Don’t make them independent tests – if you want a monolithic test, write a monolithic test.

class Monolithic(TestCase):
  def step1(self):

  def step2(self):

  def _steps(self):
    for name in dir(self): # dir() result is implicitly sorted
      if name.startswith("step"):
        yield name, getattr(self, name) 

  def test_steps(self):
    for name, step in self._steps():
      except Exception as e:"{} failed ({}: {})".format(step, type(e), e))

If the test later starts failing and you want information on all failing steps instead of halting the test case at the first failed step, you can use the subtests feature:

(The subtest feature is available via unittest2 for versions prior to Python 3.4: )

Leave a Comment