Debian Codegraph @

doc/en/how-to/xunit_setup.rst

raw
1
2 .. _`classic xunit`:
3 .. _xunitsetup:
4
5 How to implement xunit-style set-up
6 ========================================
7
8 This section describes a classic and popular way how you can implement
9 fixtures (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
25 Module level setup/teardown
26 --------------------------------------
27
28 If you have multiple test functions and test classes in a single
29 module you can optionally implement the following fixture methods
30 which will usually be called once for all the functions:
31
32 .. code-block:: python
33
34 def setup_module(module):
35 """setup any state specific to the execution of the given module."""
36
37
38 def teardown_module(module):
39 """teardown any state that was previously setup with a setup_module
40 method.
41 """
42
43 As of pytest-3.0, the ``module`` parameter is optional.
44
45 Class level setup/teardown
46 ----------------------------------
47
48 Similarly, the following methods are called at class level before
49 and after all test methods of the class are called:
50
51 .. code-block:: python
52
53 @classmethod
54 def setup_class(cls):
55 """setup any state specific to the execution of the given class (which
56 usually contains tests).
57 """
58
59
60 @classmethod
61 def teardown_class(cls):
62 """teardown any state that was previously setup with a call to
63 setup_class.
64 """
65
66 .. _xunit-method-setup:
67
68 Method and function level setup/teardown
69 -----------------------------------------------
70
71 Similarly, the following methods are called around each method invocation:
72
73 .. code-block:: python
74
75 def setup_method(self, method):
76 """setup any state tied to the execution of the given method in a
77 class. setup_method is invoked for every test method of a class.
78 """
79
80
81 def teardown_method(self, method):
82 """teardown any state that was previously setup with a setup_method
83 call.
84 """
85
86 As of pytest-3.0, the ``method`` parameter is optional.
87
88 If you would rather define test functions directly at module level
89 you can also use the following functions to implement fixtures:
90
91 .. code-block:: python
92
93 def setup_function(function):
94 """setup any state tied to the execution of the given function.
95 Invoked for every test function in the module.
96 """
97
98
99 def teardown_function(function):
100 """teardown any state that was previously setup with a setup_function
101 call.
102 """
103
104 As of pytest-3.0, the ``function`` parameter is optional.
105
106 Remarks:
107
108 * It is possible for setup/teardown pairs to be invoked multiple times
109 per testing process.
110
111 * teardown functions are not called if the corresponding setup function existed
112 and failed/was skipped.
113
114 * Prior to pytest-4.2, xunit-style functions did not obey the scope rules of fixtures, so
115 it was possible, for example, for a ``setup_method`` to be called before a
116 session-scoped autouse fixture.
117
118 Now the xunit-style functions are integrated with the fixture mechanism and obey the proper
119 scope rules of fixtures involved in the call.