0

I have a number of test cases, well say TestA, TestB, TestC, and I want them to be run in a specific order such as TestB --> TestC --> TestA. How can I make sure that the tests are actually run in this order?

Note that those test cases are classes which inherit from unittest.TestCase not just methods inside of a TestCase. That is to say, I am not wondering about the execution order of tests within a TestCase, I'm wondering about how to change the order in which the TestCase's themselves are run.

For those of you who are going to say that I'm doing something terrible and that's not how you write unit tests, I'm doing integration testing and I am aware this is a bad practice for unit tests.

Nick Chapman
  • 4,402
  • 1
  • 27
  • 41
  • Maybe [something like this](https://stackoverflow.com/questions/5387299/python-unittest-testcase-execution-order)? Each test can perform its own `self.assertFoo` but have one "main" test that actually specifies the order – Cory Kramer Oct 03 '17 at 22:42
  • @CoryKramer the problem is not the order that the methods within the test are run, just the order in which the tests themselves are run. – Nick Chapman Oct 03 '17 at 22:43
  • Possible duplicate of [Python unittest.TestCase execution order](https://stackoverflow.com/questions/5387299/python-unittest-testcase-execution-order) – kww Oct 04 '17 at 00:58

2 Answers2

1

The answer to this question is to use a unittest.TestSuite which preserves the order in which tests are added. You can do the following:

loader = unittest.TestLoader()
suite = unittest.TestSuite()
tests_to_run = [TestCaseA, TestCaseB, TestCaseC]
for test in tests_to_run:
    suite.addTests(loader.loadTestsFromTestCase(test)
runner = unittest.TextTestRunner()
runner.run(suite)
Nick Chapman
  • 4,402
  • 1
  • 27
  • 41
0

You can select a specific test case to run by passing it to the unittest module. You may put something like this in a script!

python -m unittest your.package.test.TestCaseB
python -m unittest your.package.test.TestCaseA
geckos
  • 5,687
  • 1
  • 41
  • 53