changing order of unit tests in Python

python, unit-testing

Solution

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)

Problem

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?

Original source