testing units in a specific order?

Tim Peters tim.peters at gmail.com
Wed Jan 11 04:17:31 CET 2006


[Antoon Pardon]
> Well maybe unit tests shouldn't care (Thats what I think you meant),

Yup!

> I care. Some methods are vital for the functionality of other methods.
> So it the test for the first method fails it is very likely a number of
> other methods will fail too. However I'm not interrested in the results
> of those other tests in that case. Having to weed through all the test
> results in order to check first if the vital methods are working before
> checking other methods is cumbersome.
>
> Having the vital methods tested first and ignore the rest of the results
> if they fail is much easier.

So put the tests for the different kinds of methods into different
test classes, and run the corresponding test suites in the order you
want them to run.  This is easy.  Code like:

test_classes = [FileStorageConnectionTests,
                FileStorageReconnectionTests,
                FileStorageInvqTests,
                FileStorageTimeoutTests,
                MappingStorageConnectionTests,
                MappingStorageTimeoutTests]

def test_suite():
    suite = unittest.TestSuite()
    for klass in test_classes:
        suite.addTest(unittest.makeSuite(klass))
    return suite

is common in large projects.  unittest runs tests added to a suite in
the order you add them (although  _within_ a test class, the test
methods are run in alphabetical order of method name -- when you want
ordering, that's the wrong level to try to force it; forcing order is
natural & easy at higher levels).



More information about the Python-list mailing list