1

I have written code that finds all of the tests that can be run in a package and gathers them into a single test suite. The only thing is that some of the tests are dependent on other tests already being run.

That might seem like bad test architecture but what I'm testing is the startup and shutdown of an application. So I have tests that makes sure everything comes to life appropriately, and then another set of tests that make sure that everything is cleaned up properly. Obviously though, I can't run the shutdown tests until the startup tests have finished.

Is it possible within Python's unittest to say "only run this test if the following is true, otherwise run other tests and come back to me"?

I understand that I could enforce this ordering by explicitly saying run the startup tests, now run the shutdown tests. But I'm using test discovery so that you can write more tests without explicitly having to invoke them so I am trying to avoid doing that.

Nick Chapman
  • 4,402
  • 1
  • 27
  • 41
  • If the tests are dependent on one another, they're not unit tests. What you want is an integration test. You should probably write your own testing framework to do this, instead of using `unittest`, which is specifically for unit testing. – Jeremy McGibbon Sep 29 '17 at 20:17
  • Have a look at this question: https://stackoverflow.com/questions/3843171/unit-testing-with-dependencies-between-tests there seems to be demand and supply for those cases. – Constantinius Sep 29 '17 at 20:21
  • Yeah, integration testing is an important part of testing your application. There *are* packages like [`proboscis`](https://pythonhosted.org/proboscis/) that let tests depend on one another, but this just means a test is only run if the previous test passed. In general you can't (and shouldn't!) pass state between unit tests. You should use an integration testing framework for this. One option is to write a single "unit test" which is an integration test, to use alongside your actual unit tests. – Jeremy McGibbon Sep 29 '17 at 20:28
  • Unless what I said about `proboscis` is actually exactly what you're looking for, and you're not passing state between tests. In which case, use that! – Jeremy McGibbon Sep 29 '17 at 20:30

1 Answers1

1

The unittest module requires each test to be able to run independently: no dependence on other tests running before or after. However, you can depend on other code to run before or after each test by adding setUp() or tearDown() methods to a test class. You can also depend on other code to run before or after a group of tests with setUpClass(), setUpModule(), and their tear downs.

I think there's even more flexibility in the PyTest fixtures, but I haven't used them much.

Don Kirkby
  • 53,582
  • 27
  • 205
  • 286