Django TestCase testing order


If there are several methods in the test class, I found that the order to execute is alphabetical. But I want to customize the order of execution. How to define the execution order?

For example: testTestA will be loaded first than testTestB.

class Test(TestCase):
    def setUp(self):

    def testTestB(self):
        #test code

    def testTestA(self):
        #test code
Asked By: zs2020



As far as I know, there is no way to order tests other than rename them. Could you explain why you need to run test cases in the specific order? In unit testing it usually considered as bad practice since it means that your cases are not independent.

Answered By: Yaroslav

A tenet of unit-testing is that each test should be independent of all others.
If in your case the code in testTestA must come before testTestB, then you could
combine both into one test:

def testTestA_and_TestB(self):
    # test code from testTestA
    # test code from testTestB

or, perhaps better would be

def TestA(self):
    # test code
def TestB(self):
    # test code
def test_A_then_B(self):

The Test class only tests those methods who name begins with a lower-case test....
So you can put in extra helper methods TestA and TestB which won’t get run unless you explicitly call them.

Answered By: unutbu

To update on the topic (from documentation):

Order in which tests are executed

In order to guarantee that all TestCase code starts with a clean
database, the Django test runner reorders tests in the following way:

  • All TestCase subclasses are run first.
  • Then, all other Django-based
    tests (test cases based on SimpleTestCase, including
    TransactionTestCase) are run with no particular ordering guaranteed
    nor enforced among them.
  • Then any other unittest.TestCase tests
    (including doctests) that may alter the database without restoring it
    to its original state are run.

Note: The new ordering of tests may reveal unexpected dependencies on test
case ordering. This is the case with doctests that relied on state
left in the database by a given TransactionTestCase test, they must be
updated to be able to run independently.

Answered By: Wtower
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.