1 2.. _`classic xunit`: 3.. _xunitsetup: 4 5classic xunit-style setup 6======================================== 7 8This section describes a classic and popular way how you can implement 9fixtures (setup and teardown test state) on a per-module/class/function basis. 10 11 12.. note:: 13 14 While these setup/teardown methods are simple and familiar to those 15 coming from a ``unittest`` or nose ``background``, you may also consider 16 using pytest's more powerful :ref:`fixture mechanism 17 <fixture>` which leverages the concept of dependency injection, allowing 18 for a more modular and more scalable approach for managing test state, 19 especially for larger projects and for functional testing. You can 20 mix both fixture mechanisms in the same file but 21 test methods of ``unittest.TestCase`` subclasses 22 cannot receive fixture arguments. 23 24 25Module level setup/teardown 26-------------------------------------- 27 28If you have multiple test functions and test classes in a single 29module you can optionally implement the following fixture methods 30which will usually be called once for all the functions:: 31 32 def setup_module(module): 33 """ setup any state specific to the execution of the given module.""" 34 35 def teardown_module(module): 36 """ teardown any state that was previously setup with a setup_module 37 method. 38 """ 39 40As of pytest-3.0, the ``module`` parameter is optional. 41 42Class level setup/teardown 43---------------------------------- 44 45Similarly, the following methods are called at class level before 46and after all test methods of the class are called:: 47 48 @classmethod 49 def setup_class(cls): 50 """ setup any state specific to the execution of the given class (which 51 usually contains tests). 52 """ 53 54 @classmethod 55 def teardown_class(cls): 56 """ teardown any state that was previously setup with a call to 57 setup_class. 58 """ 59 60Method and function level setup/teardown 61----------------------------------------------- 62 63Similarly, the following methods are called around each method invocation:: 64 65 def setup_method(self, method): 66 """ setup any state tied to the execution of the given method in a 67 class. setup_method is invoked for every test method of a class. 68 """ 69 70 def teardown_method(self, method): 71 """ teardown any state that was previously setup with a setup_method 72 call. 73 """ 74 75As of pytest-3.0, the ``method`` parameter is optional. 76 77If you would rather define test functions directly at module level 78you can also use the following functions to implement fixtures:: 79 80 def setup_function(function): 81 """ setup any state tied to the execution of the given function. 82 Invoked for every test function in the module. 83 """ 84 85 def teardown_function(function): 86 """ teardown any state that was previously setup with a setup_function 87 call. 88 """ 89 90As of pytest-3.0, the ``function`` parameter is optional. 91 92Remarks: 93 94* It is possible for setup/teardown pairs to be invoked multiple times 95 per testing process. 96* teardown functions are not called if the corresponding setup function existed 97 and failed/was skipped. 98 99.. _`unittest.py module`: http://docs.python.org/library/unittest.html 100