[Tutor] unittest testing order...

Steven D'Aprano steve at pearwood.info
Tue Sep 28 02:47:39 CEST 2010


On Tue, 28 Sep 2010 08:07:30 am Modulok wrote:
> On 9/27/10, Steven D'Aprano <steve at pearwood.info> wrote:
> > On Tue, 28 Sep 2010 04:03:17 am Modulok wrote:
> >> List,
> >>
> >> When using the unittest module, tests are run in alphanumeric
> >> order. What's the suggested way of specifying a test order?
> >
> > There isn't one. It shouldn't matter what order the tests run, no
> > test should *rely* on another test.
> >
> > (Although of course, if one test fails, any tests which assume the
> > missing functionality will also fail.)
>
> In an ideal world, I agree. This is possible for pure functional
> programming where state is avoided. However, when dealing with object
> oriented, imperative programming, state changes cannot be avoided.
> Thus if a method requires an object be passed to it, I'd like to
> verify that object is being constructed correctly by the object
> factory, before verifying that the method which uses said object, is
> correct.

It is reasonable to write two tests:

test_construction # verify object is constructed correctly
test_method  # verify object is used correctly

But you shouldn't rely on the tests being *executed* in any specific 
order, just as you shouldn't rely on them being *written* in any 
specific order. You should be able to go back at any time and add an 
extra test:

test_special_construction

even though test_method already exists. Likewise because every test is 
independent, it doesn't matter whether test_construction executes 
before or after test_method, or even simultaneously! (An advanced 
testing infrastructure might execute each test in its own thread.)

Tests can have their own setup and teardown code. You should not put the 
setup code to test_method in test_construction -- they should be 
independent.


-- 
Steven D'Aprano


More information about the Tutor mailing list