[Tutor] Order of unittests

Mats Wichmann mats at wichmann.us
Sat Oct 16 15:39:04 EDT 2021


On 10/12/21 01:58, Albert-Jan Roskam wrote:
>     Hi,
>     How can I write a unittest where tge tesrs are run in the order in which
>     they are defined? I tried the code below. I prefer not to change the names
>     of the tests.

in general, it's expected that the tests be written such that order does 
not matter. If it does matter you have several choices, most of which 
you've explored.

you spotted that unittest provides the TestSuite class to let you define 
your own order. I'm not sure what's wrong with explicitly adding the 
tests there in the order you want - adding them by poking around in the 
object's __dict__ does indeed seem a little hacky.

or, you can code the tests where order does matter - if, say, you're 
depending on setup to carry over from one test to the next - in a more 
monolithic fashion, making the related tests into a single test case, 
since if they're dependent, that's essentially what they are, a single 
test unit.

unittest does actually support supplying your own sort function, which 
you can simply set to None to avoid sorting.  Search for 
"sortTestMethodsUsing".



More information about the Tutor mailing list