3

While the unittesting philosophy is that tests can be ran in any order and it should pass, what if you're implementing an API where there is no other means of communicating with a server... and you need to test a certain very basic feature (such as delete) before you can do more complicated tasks? Is ordering the tests then reasonable?

If so, how can I do it with python's unittest module?

Pwnna
  • 9,178
  • 21
  • 65
  • 91
  • 1
    Elmar in http://stackoverflow.com/questions/4095319/unittest-tests-order seems to give an answer. – Niek de Klein Mar 09 '12 at 16:34
  • To clarify: you're saying you want to run the delete test first, because there's no point running the others if it fails? – Katriel Mar 09 '12 at 16:55
  • Even if those first tests fail, the later tests with delete will still be run, so what's the problem? – David Robinson Mar 09 '12 at 16:57
  • In broadbrush strokes, "If your unit tests have dependencies on external services or other unit tests, they're not unit tests." Sounds a little dogmatic. But it's a big red flag. If you're testing complex dependent semantics of the service itself then you likely want to encapsulate all of those 'steps' in a single test. But that becomes protocol testing. Unit tests really should be independent. – Michael Wilson Mar 09 '12 at 17:30
  • Let's say I have a put operation, I need to know that works then to test more complicated cases, such as using some sort of indexing, or search.. and so on. They will run fine independently if everything works.. but otherwise everything will fail if one of the basic operations fail. – Pwnna Mar 09 '12 at 17:44
  • 1
    @ultimatebuster: What you're trying to do here is some kind of integration test, not a unit test. – Niklas B. Mar 09 '12 at 17:47

1 Answers1

0

You already seem to realise that your unit tests should be independent. The only other reason I can see that you want to run the tests in some fixed order is that you want to stop running the suite if an early test fails. To do that, you can use the command-line option

-f, --failfast

Stop the test run on the first error or failure.

By the way, the tests are run in alphabetical order:

the order in which the various test cases will be run is determined by sorting the test function names with respect to the built-in ordering for strings.

(docs)

Katriel
  • 120,462
  • 19
  • 136
  • 170