Execution order on Python unittest

python, unit-testing

Solution

Better do not do it.

Tests should be independent.

To do what you want best would be to put the code into functions that are called by the test.

Like that:

def assert_can_log_in(self):
    ...

def test_1(self):
    self.assert_can_log_in()
    ...

def test_2(self):
    self.assert_can_log_in()
    ...

Or even to split the test class and put the assertions into the setUp function.

class LoggedInTests(unittest.TestCase):
    def setUp(self):
        # test for login or not - your decision

    def test_1(self):
        ...

When I split the class I often write more and better tests because the tests are split up and I can see better through all the cases that should be tested.

Problem

I need to set an order of execution for my tests, because I need some data verified before the others. Is possible to set an order? ``` class OneTestCase(unittest.TestCase): def setUp(self): # something to do def test_login (self): # first test pass def test_other (self): # any order after test_login def test_othermore (self): # any order after test_login if __name__ == '__main__': unittest.main() ```

Original source

Related problems