Reordering unit tests in Python

How can I do this so that unit tests in Python (using unittest ) run in the order they are listed in the file?

+25
python unit-testing
Oct 23 '10 at 19:32
source share
6 answers

You can change the default sort behavior by setting a custom comparison function. In unittest.py you can find the class variable unittest.TestLoader.sortTestMethodsUsing , which is set to the built-in cmp function by default.

For example, you can return the order of execution of your tests by doing the following:

 import unittest unittest.TestLoader.sortTestMethodsUsing = lambda _, x, y: cmp(y, x) 
+23
Oct. 23 2018-10-10
source share

Clever naming.

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

One way to enforce a specific order.

+14
Oct 23 '10 at 19:36
source share

As stated above, usually tests in test cases should be tested in any (i.e. random) order.

However, if you want to order tests in a test case, this is apparently not trivial. Tests (method names) are extracted from test cases using dir(MyTest) , which returns a sorted list of participants. You can use smart (?) Hacks to sort methods by their line numbers. This will work for one test case:

 if __name__ == "__main__": loader = unittest.TestLoader() ln = lambda f: getattr(MyTestCase, f).im_func.func_code.co_firstlineno lncmp = lambda a, b: cmp(ln(a), ln(b)) loader.sortTestMethodsUsing = lncmp unittest.main(testLoader=loader, verbosity=2) 
+4
Aug 28 '13 at 22:10
source share

There are also test videos that do it themselves - I think py.test does it.

+2
Oct 23 '10 at 20:31
source share

Use the proboscis library, as I mentioned (see brief description there).

+1
Feb 07 '13 at 14:05
source share

I found a solution for it using the PyTest order plugin provided here .

Try py.test YourModuleName.py -vv in the CLI and the test will work in the order in which they appeared in your module.

I did the same and worked great for me.

Note. You need to install the PyTest package and import it.

0
May 7, '15 at 17:57
source share



All Articles