testing units in a specific order?

Tim Peters tim.peters at gmail.com
Mon Jan 9 10:14:44 EST 2006

[Antoon Pardon]
> I  have  used  unit  tests now for a number of project. One thing
> that I dislike is it that the order in which the tests  are  done
> bears no relationship to the order they appear in the source.
> This  makes  using  unit tests somewhat cumbersome. Is there some
> way to force the tests being done in a particular order?

They're run in alphabetical order, sorting on the test methods' names.
 For that reason some people name test methods like 'test_001',
'test_002', ..., although unit tests really "shouldn't" case which
order they get run in.  Sometimes this is abused in a different way,
by naming a setup kind of method starting with AAA and its
corresponding teardown kind of method with zzz.

You could presumably change the sort order by subclassing TestLoader
and overriding its class-level .sortTestMethodsUsing attribute (which
is `cmp` in TestLoader).  Sounds painful and useless to me, though ;-)
 The source-code order isn't available in any case (unittest doesn't
analyze source code).

More information about the Python-list mailing list