tags:

views:

50

answers:

3

How can I make it so unit tests in Python (using unittest) are run in the order in which they are specified in the file?

thanks.

+1  A: 

Clever Naming.

class Test01_Run_Me_First( unittest.TestCase ):
    def test010_do_this( self ):
        assertTrue( True )
    def test020_do_that( self ):
        etc.

Is one way to force a specific order.

S.Lott
A: 

There are also test runners which do that by themselves – I think py.test does it.

Felix Schwarz
+3  A: 

You can change the default sorting behavior by setting a custom comparison function. In unittest.py you can find the class variable unittest.TestLoader.sortTestMethodsUsing which is set to the builtin function cmp by default.

For example you can revert the execution order of your tests with doing this:

import unittest
unittest.TestLoader.sortTestMethodsUsing = lambda _, x, y: cmp(y, x)
atomocopter