unittest.Testsuite and execution order

Francesco Russo francescor82 at gmail.com
Wed Apr 18 12:51:12 EDT 2018


Hello!

I'm reading the documentation of unittest.TestSuite (Python 2 and 3), but I
can't find any explicit sentence stating that TestSuite will honor the
order. I can only read that TestSuite can group test cases together. Please
blame it on my poor English skills if I'm not interpreting the
documentation correctly.

My use case: my SUT is split into modules. Besides writing unit tests for
each module, I want to write an integration test, and I also need to
perform some actions between two calls to the SUT. In my case, the order of
the execution is important.

Now, the current implementation of TestSuite uses a list, internally, so,
today, the order is honored if I create a TestSuite calling addTest() in
the proper order, or if I pass a list to the constructor. I've seen
examples like this:

class MyTestCode(unittest.TestCase):
   def test_func_1(self):
      # do something to test func_1 on the SUT
      sut.func_1()
      self.assert(...)

   def perform_intermediate_step(self):
      # do something between func_1 and func_2

   def test_func_2(self):
      # do something to test func_2 on the SUT
      sut.func_2()
      self.assert(...)

suite = unittest.TestSuite()
suite.addTest(MyTestCode("test_func_1"))
suite.addTest(MyTestCode("perform_intermediate_step"))
suite.addTest(MyTestCode("test_func_2"))

Such an example works, today, since TestSuite uses a list, and addTest()
appends to the list.
My question is: is this something that I can rely on for the future? I
definitely don't want to rely on the current implementation, unless I see
it in the documentation.

If it's something that I can't rely on for the future, then I'd rather
write my test code in a different way.

Regards,
Francesco

P.S.: I strongly believe that there are better ways to implement a test
like the one I just described, but what I'm interested in now is whether
TestSuite is meant to be future-proof for such a case.

-- 
Francesco Russo
The White Rabbit put on his spectacles. 'Where shall I begin, please your
Majesty?' he asked.
'Begin at the beginning,' the King said gravely, 'and go on till you come
to the end: then stop.'


More information about the Python-list mailing list