Debian Codegraph @

docs/plugins/layers.rst

raw
1 ====================================
2 Organizing Test Fixtures into Layers
3 ====================================
4
5 .. note ::
6
7 New in version 0.4
8
9 Layers allow more flexible organization of test fixtures than test-, class- and
10 module- level fixtures. Layers in nose2 are inspired by and aim to be
11 compatible with the layers used by Zope's testrunner.
12
13 Using layers, you can do things like:
14
15 * Implement package-level fixtures by sharing a layer among all test cases in
16 the package.
17
18 * Share fixtures across tests in different modules without having them run
19 multiple times.
20
21 * Create a fixture tree deeper than three levels (test, class and module).
22
23 * Make fixtures available for other packages or projects to use.
24
25 A layer is a *new-style* class that implements at least a ``setUp``
26 classmethod:
27
28 .. code-block:: python
29
30 class Layer(object):
31 @classmethod
32 def setUp(cls):
33 # ...
34
35 It may also implement ``tearDown``, ``testSetUp`` and ``testTearDown``, all as
36 classmethods.
37
38 To assign a layer to a test case, set the test case's ``layer`` property:
39
40 .. code-block:: python
41
42 class Test(unittest.TestCase):
43 layer = Layer
44
45 Note that the layer *class* is assigned, not an instance of the layer.
46 Typically layer classes are not instantiated.
47
48 Sub-layers
49 ==========
50
51 Layers may subclass other layers:
52
53 .. code-block:: python
54
55 class SubLayer(Layer):
56 @classmethod
57 def setUp(cls):
58 # ...
59
60 In this case, all tests that belong to the sub-layer also belong to the base
61 layer. For example for this test case:
62
63 .. code-block:: python
64
65 class SubTest(unittest.TestCase):
66 layer = SubLayer
67
68 The ``setUp`` methods from *both* ``SubLayer`` and ``Layer`` will run before
69 any tests are run. The superclass's setup will always run before the subclass's
70 setup. For ``teardown``, the reverse: the subclass's ``teardown`` runs before
71 the superclass's.
72
73 .. warning::
74
75 One important thing to note: layers that subclass other layers *must
76 not* call their superclass's ``setUp``, ``tearDown``, etc. The test
77 runner will take care of organizing tests so that the superclass's
78 methods are called in the right order::
79
80 Layer.setUp ->
81 SubLayer.setUp ->
82 Layer.testSetUp ->
83 SubLayer.testSetUp ->
84 TestCase.setUp
85 TestCase.run
86 TestCase.tearDown
87 SubLayer.testTearDown <-
88 Layer.testTearDown <-
89 SubLayer.tearDown <-
90 Layer.tearDown <-
91
92 If a sublayer calls it superclass's methods directly, *those
93 methods will be called twice*.
94
95
96 Layer method reference
97 ======================
98
99 .. class:: Layer
100
101 Not an actual class, but reference documentation for
102 the methods layers can implement. There is no layer
103 base class. Layers must be subclasses of :class:`object`
104 or other layers.
105
106 .. classmethod:: setUp(cls)
107
108 The layer's ``setUp`` method is called before any tests belonging to
109 that layer are executed. If no tests belong to the layer (or one of
110 its sub-layers) then the ``setUp`` method will not
111 be called.
112
113 .. classmethod:: tearDown(cls)
114
115 The layer's ``tearDown`` method is called after any tests
116 belonging to the layer are executed, if the layer's ``setUp``
117 method was called and did not raise an exception. It will not
118 be called if the layer has no ``setUp`` method, or if that
119 method did not run or did raise an exception.
120
121 .. classmethod:: testSetUp(cls[, test])
122
123 The layer's ``testSetUp`` method is called before each test
124 belonging to the layer (and its sub-layers). If
125 the method is defined to accept an argument, the test case
126 instance is passed to the method. The method may also be
127 defined to take no arguments.
128
129 .. classmethod:: testTearDown(cls[, test])
130
131 The layer's ``testTearDown`` method is called after each test
132 belonging to the layer (and its sub-layers), if
133 the layer also defines a ``setUpTest`` method and that method
134 ran successfully (did not raise an exception) for this test
135 case.
136
137 Layers DSL
138 ==========
139
140 nose2 includes a DSL for setting up layer-using tests called
141 "such". Read all about it here: :doc:`../such_dsl`.
142
143 Pretty reports
144 ==============
145
146 The layers plugin module includes a second plugin that alters test
147 report output to make the layer groupings more clear. When activated
148 with the :option:`--layer-reporter` command-line option (or via a config
149 file), test output that normally looks like this::
150
151 test (test_layers.NoLayer) ... ok
152 test (test_layers.Outer) ... ok
153 test (test_layers.InnerD) ... ok
154 test (test_layers.InnerA) ... ok
155 test (test_layers.InnerA_1) ... ok
156 test (test_layers.InnerB_1) ... ok
157 test (test_layers.InnerC) ... ok
158 test2 (test_layers.InnerC) ... ok
159
160 ----------------------------------------------------------------------
161 Ran 8 tests in 0.001s
162
163 OK
164
165 Will instead look like this::
166
167 test (test_layers.NoLayer) ... ok
168 Base
169 test (test_layers.Outer) ... ok
170 LayerD
171 test (test_layers.InnerD) ... ok
172 LayerA
173 test (test_layers.InnerA) ... ok
174 LayerB
175 LayerC
176 test (test_layers.InnerC) ... ok
177 test2 (test_layers.InnerC) ... ok
178 LayerB_1
179 test (test_layers.InnerB_1) ... ok
180 LayerA_1
181 test (test_layers.InnerA_1) ... ok
182
183 ----------------------------------------------------------------------
184 Ran 8 tests in 0.002s
185
186 OK
187
188 The layer reporter plugin can also optionally colorize the keywords
189 (by default, 'A', 'having', and 'should') in output from tests defined
190 with the :doc:`such DSL <../such_dsl>`.
191
192 If you would like to change how the layer is displayed, set the ``description`` attribute.
193
194 .. code-block:: python
195
196 class LayerD(Layer):
197 description = '*** This is a very important custom layer description ***'
198
199 Now the output will be the following::
200
201
202 test (test_layers.NoLayer) ... ok
203 Base
204 test (test_layers.Outer) ... ok
205 *** This is a very important custom layer description ***
206 test (test_layers.InnerD) ... ok
207 LayerA
208 test (test_layers.InnerA) ... ok
209 LayerB
210 LayerC
211 test (test_layers.InnerC) ... ok
212 test2 (test_layers.InnerC) ... ok
213 LayerB_1
214 test (test_layers.InnerB_1) ... ok
215 LayerA_1
216 test (test_layers.InnerA_1) ... ok
217
218 ----------------------------------------------------------------------
219 Ran 8 tests in 0.002s
220
221 OK
222
223
224 Warnings and Caveats
225 ====================
226
227 Test case order and module isolation
228 ------------------------------------
229
230 Test cases that use layers will not execute in the same order as test
231 cases that do not. In order to execute the layers efficiently, the
232 test runner must reorganize *all* tests in the loaded test suite to
233 group those having like layers together (and sub-layers under their
234 parents). If you share layers across modules this may result in tests
235 from one module executing interleaved with tests from a different
236 module.
237
238
239 Mixing layers with ``setUpClass`` and module fixtures
240 -----------------------------------------------------
241
242 **Don't cross the streams.**
243
244 The implementation of class- and module-level fixtures in unittest2
245 depends on introspecting the class hierarchy inside of the
246 ``unittest.TestSuite``. Since the suites that the ``layers`` plugin uses to
247 organize tests derive from :class:`unittest.BaseTestSuite` (instead of
248 :class:`unittest.TestSuite`), class- and module- level fixtures in
249 TestCase classes that use layers will be ignored.
250
251 Mixing layers and multiprocess testing
252 --------------------------------------
253
254 In the initial release, *test suites using layers are incompatible with
255 the multiprocess plugin*. This should be fixed in a future release.
256
257
258 Plugin reference
259 ================
260
261 .. autoplugin :: nose2.plugins.layers