0

I have a set of tests, but some of them use as input, the output of some previous tests. Here is how I do define my suite tests:

listOfTests = []
listOfTests+= unittest.TestLoader().loadTestsFromTestCase(TTestA)
listOfTests+= unittest.TestLoader().loadTestsFromTestCase(TTestB)
listOfTests+= unittest.TestLoader().loadTestsFromTestCase(TTestC)

unittest.main(testRunner=xmlrunner.XMLTestRunner(output=os.getcwd() + '/xmlTests')).suite(listOfTests)

How Can I ensure that the tests are executed in the order sequence I define them? Is there some option which needs to be set for unittest such that TTestA is ran after TTestA. Thanks for your valuable help.

Nabil
  • 1,175
  • 2
  • 13
  • 29
  • 4
    Well... you've gotta do what you what to do, but I'd like to point out that this is usually considered a very bad practice, though... You unit tests should be independent and isolated from one another. Imagine you send your code to a repository that runs an automated test tool that runs them in parallel? Best case scenario, you'll get them sequentially (hence making the test run slower). Most likely scenario, you will end up with unordered tests. I really suggest you make them independent. – Savir Nov 06 '16 at 18:53
  • 2
    If I may give you some advice about unittests. It is typically against idiomatic unittest behaviour and considered very bad practice to write unittests that depend on each other. I strongly suggest you rethink your unittest design approach as to have each unittest independent and idempotent. If you need to have something exist already for each of your unittests, consider using the `setUp` method which runs before each test. You can also make use of `tearDown` to clean things up after each test. This really helps with the *idempotent* part. – idjaw Nov 06 '16 at 18:53
  • @idjaw: the issue is that I have a binary applciation for which i need to check first that it runs, then that it produces files, then that these files are consistent with a previous release. In my case in case of missing files, I need to rerun the reference application. It seems that unittest orders the tests in an alphabetic order, which I can of course workaround, but if there is some native approach, it would be great. – Nabil Nov 06 '16 at 19:02
  • @Nabil.G It sounds like you need to break down your function to smaller components so you can unittest each piece on its own. This is a clear sign that there is too much happening in your function. – idjaw Nov 06 '16 at 19:02
  • How can I do that? As I wrote, TestB needs TestA to run to produce the input files. Is there some option/approach to ensure that TestA is run before TestB?thanks. – Nabil Nov 06 '16 at 19:17
  • And another reference: http://stackoverflow.com/questions/4005695/changing-order-of-unit-tests-in-python – idjaw Nov 06 '16 at 20:00
  • The link you provide is for tests un on single unit class. in my case i want all tests in test class TTestA to be run before tests in TTestB and TTestC. – Nabil Nov 08 '16 at 03:27

0 Answers0