How to have unittest tests to be executed in the order they appear?

Ben Finney bignose+hates-spam at benfinney.id.au
Wed Apr 16 18:29:48 EDT 2008


Matthew Woodcraft <mattheww at chiark.greenend.org.uk> writes:

> Ben Finney  <bignose+hates-spam at benfinney.id.au> wrote:
> > Your test cases should *not* depend on any state from other test
> > cases; they should function equally well when executed in any
> > arbitrary sequence. Dependencies between separate test cases (e.g.
> > "they only work correctly when run in a specific sequence") means
> > you're not isolating them properly.
> 
> So a mode to randomise the test sequence would be nice to have.

Twisted has a "trial" framework that allows just such a mode
<URL:http://buildbot.net/repos/release/docs/reference/buildbot.steps.python_twisted.Trial-class.html>.

> Unittest's behaviour (using alphabetical order) doesn't really help
> to detect undesired dependencies (which might be bugs in the test
> suite or bugs in the underlying code).

Agreed. It's just a matter of making a custom unittest.TestRunner,
though. Yes, a Small Matter of Programming which I haven't actually
done, but unittest doesn't make it difficult to do that.

> But running tests in the order they appear is often helpful: you can
> put the tests for basic stuff before the tests for advanced stuff,
> and then if you suddenly get seventeen failing tests, you know that
> the first failure is the best bet to investigate first.

Surely, since "suddenly" implies you changed one small area of the
code, that area of the code is the best place to look for what caused
the failure.

-- 
 \         "I planted some bird seed. A bird came up. Now I don't know |
  `\                               what to feed it."  -- Steven Wright |
_o__)                                                                  |
Ben Finney



More information about the Python-list mailing list