Skip to content

13. Test Fixtures

NaveenS edited this page Aug 11, 2019 · 8 revisions

Test fixtures are the resources and initial conditions that a test needs to operate correctly and independently from other tests.

Test fixtures are functions that run before and after a test.

The intent is to provide testers hooks to set up preconditions needed for the test, and cleanup after the test.

The methods are,

  • setUp() / tearDown() – before and after test methods
  • setUpClass() / tearDownClass() – before and after a class of tests
  • setUpModule() / tearDownModule() – before and after a module of tests
  • Cleanup functions – extra tearDown methods that can be added at runtime to any test method during setUp, or during the test method itself.

Sample Code (Without Cleanup functions):

import unittest

#called once, before anything else in this module
def setUpModule():
    print("setUpModule")

#called once, after anything else in this module
def tearDownModule():
    print("tearDownModule")

class TestCase1(unittest.TestCase):
    # called once, before first tests
    @classmethod
    def setUpClass(inst):
        print("setUpClass")

    # called once, before every tests
    def setUp(self):
        print("setUP")
        def cleanup_a():
            print("cleanup_a")
        self.addCleanup(cleanup_a)

    def test_method1(self):
        print("test method1")

    def test_method2(self):
        print("test method2")

    # called once, after every tests
    def tearDown(self):
        print("tearDown")

    # called once, after last tests
    @classmethod
    def tearDownClass(inst):
        print("tearDownClass")

if __name__ == '__main__':
    unittest.main()

Cleanup Method

Extra cleanup methods can be added from either a test or a setUp method. Cleanup functions are called AFTER tearDown() but BEFORE tearDownClass()

import unittest

#called once, before anything else in this module
def setUpModule():
    print("setUpModule")

#called once, after anything else in this module
def tearDownModule():
    print("tearDownModule")

class TestCase1(unittest.TestCase):
    # called once, before first tests
    @classmethod
    def setUpClass(inst):
        print("setUpClass")

    # called once, before every tests
    def setUp(self):
        print("setUP")
        # --- add a cleanup method fixture for all tests
        def cleanup_a():
            print("cleanup_a")
        self.addCleanup(cleanup_a)

    def test_method1(self):
        print("test method1")
        # --- add a cleanup method fixture for just this test
        def cleanup_b():
            print("cleanup_b")
        self.addCleanup(cleanup_b)

    def test_method2(self):
        print("test method2")

    # called once, after every tests
    def tearDown(self):
        print("tearDown")
        def cleanup_c():
            print("cleanup_c")
        self.addCleanup(cleanup_c)

    # called once, after last tests
    @classmethod
    def tearDownClass(inst):
        print("tearDownClass")

if __name__ == '__main__':
    unittest.main()

Skipping tests within setUp()

In the setUp method, you can decide to skip a test. If skipped, the test will not be run. Also, the tearDown method will not be run.

    def setUp(self):
        print("setUP")
        currentTest = self.id().split('.')[-1]
        if currentTest == 'test_method2':
            self.skipTest('reason for skipping')

Clone this wiki locally