pytest supports setup and teardown methods similar to those used in unittest, but it provides even more flexibility. We'll discuss these briefly, since they are familiar, but they are not used as extensively as in the unittest module, as pytest provides us with a powerful fixtures facility, which we'll discuss in the next section.
If we are writing class-based tests, we can use two methods called setup_method and teardown_method in the same way that setUp and tearDown are called in unittest. They are called before and after each test method in the class to perform setup and cleanup duties. There is one difference from the unittest methods though. Both methods accept an argument: the function object representing the method being called.
In addition, pytest provides other setup and teardown functions to give us more control over when setup and cleanup code is executed. The setup_class and teardown_class methods are expected to be class methods; they accept a single argument (there is no self argument) representing the class in question. These methods are only run when the class is initiated rather than on each test run.
Finally, we have the setup_module and teardown_module functions, which are run immediately before and after all tests (in functions or classes) in that module. These can be useful for one time setup, such as creating a socket or database connection that will be used by all tests in the module. Be careful with this one, as it can accidentally introduce dependencies between tests if the object stores state that isn't correctly cleaned up between tests.
That short description doesn't do a great job of explaining exactly when these methods are called, so let's look at an example that illustrates exactly when it happens:
def setup_module(module):
print("setting up MODULE {0}".format(module.__name__))
def teardown_module(module):
print("tearing down MODULE {0}".format(module.__name__))
def test_a_function():
print("RUNNING TEST FUNCTION")
class BaseTest:
def setup_class(cls):
print("setting up CLASS {0}".format(cls.__name__))
def teardown_class(cls):
print("tearing down CLASS {0}\n".format(cls.__name__))
def setup_method(self, method):
print("setting up METHOD {0}".format(method.__name__))
def teardown_method(self, method):
print("tearing down METHOD {0}".format(method.__name__))
class TestClass1(BaseTest):
def test_method_1(self):
print("RUNNING METHOD 1-1")
def test_method_2(self):
print("RUNNING METHOD 1-2")
class TestClass2(BaseTest):
def test_method_1(self):
print("RUNNING METHOD 2-1")
def test_method_2(self):
print("RUNNING METHOD 2-2")
The sole purpose of the BaseTest class is to extract four methods that are otherwise identical to the test classes, and use inheritance to reduce the amount of duplicate code. So, from the point of view of pytest, the two subclasses have not only two test methods each, but also two setup and two teardown methods (one at the class level, one at the method level).
If we run these tests using pytest with the print function output suppression disabled (by passing the -s or --capture=no flag), they show us when the various functions are called in relation to the tests themselves:
setup_teardown.py setting up MODULE setup_teardown RUNNING TEST FUNCTION .setting up CLASS TestClass1 setting up METHOD test_method_1 RUNNING METHOD 1-1 .tearing down METHOD test_method_1 setting up METHOD test_method_2 RUNNING METHOD 1-2 .tearing down METHOD test_method_2 tearing down CLASS TestClass1 setting up CLASS TestClass2 setting up METHOD test_method_1 RUNNING METHOD 2-1 .tearing down METHOD test_method_1 setting up METHOD test_method_2 RUNNING METHOD 2-2 .tearing down METHOD test_method_2 tearing down CLASS TestClass2 tearing down MODULE setup_teardown
The setup and teardown methods for the module are executed at the beginning and end of the session. Then the lone module-level test function is run. Next, the setup method for the first class is executed, followed by the two tests for that class. These tests are each individually wrapped in separate setup_method and teardown_method calls. After the tests have executed, the teardown method on the class is called. The same sequence happens for the second class, before the teardown_module method is finally called, exactly once.