Running Python Test Cases in Desired Order

Good codebase are well tested, not necessarily in aiming towards 100% test coverage. But core algorithm are at least covered in unittests; and major system workflows, covered in integration tests. While it is a common practice to assume unit tests to be independent and stateless, it is not always true with integration tests.

In this short example we have an integration test where the flow is to first preprocess the item and then compute its score.

1
2
3
4
5
6
7
8
9
import unittest

class IntegrationTest(unittest.TestCase):

def test_preprocess_item(self):
pass

def test_compute_item_score(self):
pass

If we just run this test case, test_compute_item_score is going to happen before test_preprocess_item, because unittest applies sorting to the tests.

There are two ways to overcome this: 1. disable sorting (left to the reader as an exercise). 2. using cleverly chose test case names (show below).

1
2
3
4
5
6
7
8
9
import unittest

class IntegrationTest(unittest.TestCase):

def test_0010_preprocess_item(self):
pass

def test_0020_compute_item_score(self):
pass