Debian Codegraph @

docs/dev/hook_reference.rst

raw
1 Hook reference
2 ==============
3
4 .. note ::
5
6 Hooks are listed here in order of execution.
7
8
9 Pre-registration Hooks
10 ----------------------
11
12 .. function :: pluginsLoaded(self, event)
13
14 :param event: :class:`nose2.events.PluginsLoadedEvent`
15
16 The ``pluginsLoaded`` hook is called after all config files have been read,
17 and all plugin classes loaded. Plugins that register automatically
18 (those that call :meth:`nose2.events.Plugin.register` in ``__init__``
19 or have ``always-on = True`` set in their config file sections) will
20 have already been registered with the hooks they implement. Plugins
21 waiting for command-line activation will not yet be registered.
22
23 Plugins can use this hook to examine or modify the set of loaded plugins,
24 inject their own hook methods using
25 :meth:`nose2.events.PluginInterface.addMethod`, or take other
26 actions to set up or configure themselves or the test run.
27
28 Since ``pluginsLoaded`` is a pre-registration hook, it is called
29 for *all plugins* that implement the method, whether they have
30 registered or not. Plugins that do not automatically register
31 themselves should limit their actions in this hook to
32 configuration, since they may not actually be active during the
33 test run.
34
35 .. function :: handleArgs(self, event)
36
37 :param event: :class:`nose2.events.CommandLineArgsEvent`
38
39 The ``handleArgs`` hook is called after all arguments from the command
40 line have been parsed. Plugins can use this hook to handle command-line
41 arguments in non-standard ways. They should not use it to try to modify
42 arguments seen by other plugins, since the order in which plugins
43 execute in a hook is not guaranteed.
44
45 Since ``handleArgs`` is a pre-registration hook, it is called for
46 *all plugins* that implement the method, whether they have registered
47 or not. Plugins that do not automatically register
48 themselves should limit their actions in this hook to
49 configuration, since they may not actually be active during the
50 test run.
51
52
53 Standard Hooks
54 --------------
55
56 These hooks are called for registered plugins only.
57
58
59 .. function :: createTests(self, event)
60
61 :param event: A :class:`nose2.events.CreateTestsEvent` instance
62
63 Plugins can take over test loading by returning a test suite and setting
64 ``event.handled`` to True.
65
66 .. function :: loadTestsFromNames(self, event)
67
68 :param event: A :class:`nose2.events.LoadFromNamesEvent` instance
69
70 Plugins can return a test suite or list of test suites and set
71 ``event.handled`` to ``True`` to prevent other plugins from loading
72 tests from the given names, or append tests to
73 ``event.extraTests``. Plugins can also remove names from
74 ``event.names`` to prevent other plugins from acting on those
75 names.
76
77 .. function :: loadTestsFromName(self, event)
78
79 :param event: A :class:`nose2.events.LoadFromNameEvent` instance
80
81 Plugins can return a test suite and set ``event.handled`` to ``True``
82 to prevent other plugins from loading tests from the given name,
83 or append tests to ``event.extraTests``.
84
85 .. function :: handleFile(self, event)
86
87 :param event: A :class:`nose2.events.HandleFileEvent` instance
88
89 Plugins can use this hook to load tests from files that are not
90 Python modules. Plugins may either append tests to ``event.extraTest``,
91 or, if they want to prevent other plugins from processing the file,
92 set ``event.handled`` to True and return a test case or test suite.
93
94 .. function :: matchPath(self, event)
95
96 :param event: A :class:`nose2.events.MatchPathEvent` instance
97
98 Plugins can use this hook to prevent python modules from being
99 loaded by the test loader or force them to be loaded by the test
100 loader. Set ``event.handled`` to ``True`` and return ``False`` to cause the
101 loader to skip the module. Set ``event.handled`` to ``True`` and return
102 ``True`` to cause the loader to load the module.
103
104 .. function :: loadTestsFromModule(self, event)
105
106 :param event: A :class:`nose2.events.LoadFromModuleEvent` instance
107
108 Plugins can use this hook to load tests from test modules. To
109 prevent other plugins from loading from the module, set
110 ``event.handled`` and return a test suite. Plugins can also append
111 tests to ``event.extraTests`` -- usually that's what you want to
112 do, since that will allow other plugins to load their tests from
113 the module as well.
114
115 See also :ref:`this warning <loading-from-module>` about test cases
116 not defined in the module.
117
118 .. function :: loadTestsFromTestCase(self, event)
119
120 :param event: A :class:`nose2.events.LoadFromTestCaseEvent` instance
121
122 Plugins can use this hook to load tests from a
123 :class:`unittest.TestCase`. To prevent other plugins from loading
124 tests from the test case, set ``event.handled`` to ``True`` and return
125 a test suite. Plugins can also append tests to ``event.extraTests``
126 -- usually that's what you want to do, since that will allow other
127 plugins to load their tests from the test case as well.
128
129 .. function :: getTestCaseNames(self, event)
130
131 :param event: A :class:`nose2.events.GetTestCaseNamesEvent` instance
132
133 Plugins can use this hook to limit or extend the list of test case
134 names that will be loaded from a :class:`unittest.TestCase` by the
135 standard nose2 test loader plugins (and other plugins that respect
136 the results of the hook). To force a specific list of names, set
137 ``event.handled`` to ``True`` and return a list: this exact list will
138 be the only test case names loaded from the test case. Plugins can
139 also extend the list of names by appending test names to
140 ``event.extraNames``, and exclude names by appending test names to
141 ``event.excludedNames``.
142
143 .. function :: runnerCreated(self, event)
144
145 :param event: A :class:`nose2.events.RunnerCreatedEvent` instance
146
147 Plugins can use this hook to wrap, capture or replace the test
148 runner. To replace the test runner, set ``event.runner``.
149
150 .. function :: resultCreated(self, event)
151
152 :param event: A :class:`nose2.events.ResultCreatedEvent` instance
153
154 Plugins can use this hook to wrap, capture or replace the test
155 result. To replace the test result, set ``event.result``.
156
157 .. function :: startTestRun(self, event)
158
159 :param event: A :class:`nose2.events.StartTestRunEvent` instance
160
161 Plugins can use this hook to take action before the start of the
162 test run, and to replace or wrap the test executor. To replace the
163 executor, set ``event.executeTests``. This must be a callable that
164 takes two arguments: the top-level test and the test result.
165
166 To prevent the test executor from running at all, set
167 ``event.handled`` to ``True``.
168
169 .. function :: startLayerSetup(self, event)
170
171 :param event: A :class:`nose2.events.StartLayerSetupEvent` instance (only
172 available in suites with layers).
173
174 Plugins can use this hook to take action before the start of the ``setUp``
175 in a layer.
176
177 .. function :: stopLayerSetup(self, event)
178
179 :param event: A :class:`nose2.events.StopLayerSetupEvent` instance (only
180 available in suites with layers).
181
182 Plugins can use this hook to take action after ``setUp`` finishes, in a
183 layer.
184
185 .. function :: startLayerSetupTest(self, event)
186
187 :param event: A :class:`nose2.events.StartLayerSetupTestEvent` instance
188 (only available in suites with layers).
189
190 Plugins can use this hook to take action before the start of ``testSetUp``
191 in a layer.
192
193 .. function :: stopLayerSetupTest(self, event)
194
195 :param event: A :class:`nose2.events.StopLayerSetupTestEvent` instance (only
196 available in suites with layers).
197
198 Plugins can use this hook to take action after ``testSetUp`` finishes, in a
199 layer.
200
201 .. function :: startTest(self, event)
202
203 :param event: A :class:`nose2.events.StartTestEvent` instance
204
205 Plugins can use this hook to take action immediately before a test
206 runs.
207
208 .. function :: reportStartTest(self, event)
209
210 :param event: A :class:`nose2.events.ReportTestEvent` instance
211
212 Plugins can use this hook to produce output for the user at the
213 start of a test. If you want to print to the console, write to
214 ``event.stream``. Remember to respect ``self.session.verbosity`` when
215 printing to the console. To prevent other plugins from reporting
216 to the user, set ``event.handled`` to ``True``.
217
218 .. function :: describeTest(self, event)
219
220 :param event: A :class:`nose2.events.DescribeTestEvent` instance
221
222 Plugins can use this hook to alter test descriptions. To return a
223 nonstandard description for a test, set ``event.description``. Be
224 aware that other plugins may have set this also!
225
226 .. function :: setTestOutcome(self, event)
227
228 :param event: A :class:`nose2.events.TestOutcomeEvent` instance
229
230 Plugins can use this hook to alter test outcomes. Plugins can
231 ``event.outcome`` to change the outcome of the event, tweak, change
232 or remove ``event.exc_info``, set or clear ``event.expected``, and
233 so on.
234
235 .. function :: testOutcome(self, event)
236
237 :param event: A :class:`nose2.events.TestOutcomeEvent` instance
238
239 Plugins can use this hook to take action based on the outcome of
240 tests. Plugins *must not* alter test outcomes in this hook: that's
241 what :func:`setTestOutcome` is for. Here, plugins may only react to
242 the outcome event, not alter it.
243
244 .. function :: reportSuccess(self, event)
245
246 :param event: A :class:`nose2.events.LoadFromNamesEvent` instance
247
248 Plugins can use this hook to report test success to the user. If
249 you want to print to the console, write to
250 ``event.stream``. Remember to respect ``self.session.verbosity`` when
251 printing to the console. To prevent other plugins from reporting to
252 the user, set ``event.handled`` to ``True``.
253
254 .. function :: reportError(self, event)
255
256 :param event: A :class:`nose2.events.ReportTestEvent` instance
257
258 Plugins can use this hook to report a test error to the user. If
259 you want to print to the console, write to
260 ``event.stream``. Remember to respect ``self.session.verbosity`` when
261 printing to the console. To prevent other plugins from reporting to
262 the user, set ``event.handled`` to ``True``.
263
264 .. function :: reportFailure(self, event)
265
266 :param event: A :class:`nose2.events.ReportTestEvent` instance
267
268 Plugins can use this hook to report test failure to the user. If
269 you want to print to the console, write to
270 ``event.stream``. Remember to respect ``self.session.verbosity`` when
271 printing to the console. To prevent other plugins from reporting to
272 the user, set ``event.handled`` to ``True``.
273
274 .. function :: reportSkip(self, event)
275
276 :param event: A :class:`nose2.events.ReportTestEvent` instance
277
278 Plugins can use this hook to report a skipped test to the user. If
279 you want to print to the console, write to
280 ``event.stream``. Remember to respect ``self.session.verbosity`` when
281 printing to the console. To prevent other plugins from reporting to
282 the user, set ``event.handled`` to ``True``.
283
284 .. function :: reportExpectedFailure(self, event)
285
286 :param event: A :class:`nose2.events.ReportTestEvent` instance
287
288 Plugins can use this hook to report an expected failure to the
289 user. If you want to print to the console, write to
290 ``event.stream``. Remember to respect ``self.session.verbosity`` when
291 printing to the console. To prevent other plugins from reporting to
292 the user, set ``event.handled`` to ``True``.
293
294 .. function :: reportUnexpectedSuccess(self, event)
295
296 :param event: A :class:`nose2.events.ReportTestEvent` instance
297
298 Plugins can use this hook to report an unexpected success to the
299 user. If you want to print to the console, write to
300 ``event.stream``. Remember to respect ``self.session.verbosity`` when
301 printing to the console. To prevent other plugins from reporting to
302 the user, set ``event.handled`` to ``True``.
303
304 .. function :: reportOtherOutcome(self, event)
305
306 :param event: A :class:`nose2.events.ReportTestEvent` instance
307
308 Plugins can use this hook to report a custom test outcome to the
309 user. If you want to print to the console, write to
310 ``event.stream``. Remember to respect ``self.session.verbosity`` when
311 printing to the console. To prevent other plugins from reporting to
312 the user, set ``event.handled`` to ``True``.
313
314 nose2 will never produce this event by itself. It only gets triggered if
315 a plugin creates a test result with an unrecognized outcome.
316
317 .. function :: stopTest(self, event)
318
319 :param event: A :class:`nose2.events.StopTestEvent` instance
320
321 Plugins can use this hook to take action after a test has completed
322 running and reported its outcome.
323
324 .. function :: startLayerTeardownTest(self, event)
325
326 :param event: A :class:`nose2.events.StartLayerTeardownTestEvent` instance
327 (only available in suites with layers).
328
329 Plugins can use this hook to take action before the start of
330 ``testTearDown()`` in a layer.
331
332 .. function :: stopLayerTeardownTest(self, event)
333
334 :param event: A :class:`nose2.events.StopLayerTeardownTestEvent` instance
335 (only available in suites with layers).
336
337 Plugins can use this hook to take action after ``testTearDown()`` finishes,
338 in a layer.
339
340 .. function :: startLayerTeardown(self, event)
341
342 :param event: A :class:`nose2.events.StartLayerTeardownEvent` instance (only
343 available in suites with layers).
344
345 Plugins can use this hook to take action before the start of the
346 ``tearDown()`` in a layer.
347
348 .. function :: stopLayerTeardown(self, event)
349
350 :param event: A :class:`nose2.events.StopLayerTeardownEvent` instance (only
351 available in suites with layers).
352
353 Plugins can use this hook to take action after ``tearDown()`` finishes, in a
354 layer.
355
356 .. function :: stopTestRun(self, event)
357
358 :param event: A :class:`nose2.events.StopTestRunEvent` instance
359
360 Plugins can use this hook to take action at the end of a test run.
361
362 .. function :: afterTestRun(self, event)
363
364 :param event: A :class:`nose2.events.StopTestRunEvent` instance
365
366 .. note ::
367
368 New in version 0.2
369
370 Plugins can use this hook to take action *after* the end of a test
371 run, such as printing summary reports like the builtin result
372 reporter plugin :class:`nose2.plugins.result.ResultReporter`.
373
374 .. function :: resultStop(self, event)
375
376 :param event: A :class:`nose2.events.ResultStopEvent` instance
377
378 Plugins can use this hook to *prevent* other plugins from stopping
379 a test run. This hook fires when something calls
380 :meth:`nose2.result.PluggableTestResult.stop`. If you want to
381 prevent this from stopping the test run, set ``event.shouldStop``
382 to ``False``.
383
384 .. function :: beforeErrorList(self, event)
385
386 :param event: A :class:`nose2.events.ReportSummaryEvent` instance
387
388 Plugins can use this hook to output or modify summary information
389 before the list of errors and failures is output. To modify the
390 categories of outcomes that will be reported, plugins can modify
391 the ``event.reportCategories`` dictionary. Plugins can set, wrap, or
392 capture the output stream by reading or setting ``event.stream``.
393 If you want to print to the console, write to
394 ``event.stream``. Remember to respect ``self.session.verbosity`` when
395 printing to the console.
396
397 .. function :: outcomeDetail(self, event)
398
399 :param event: A :class:`nose2.events.OutcomeDetailEvent` instance
400
401 Plugins can use this hook to add additional elements to error list
402 output. Append extra detail lines to ``event.extraDetail``; these
403 will be joined together with newlines before being output as part
404 of the detailed error/failure message, after the traceback.
405
406 .. function :: beforeSummaryReport(self, event)
407
408 :param event: A :class:`nose2.events.ReportSummaryEvent` instance
409
410 Plugins can use this hook to output or modify summary information
411 before the summary lines are output. To modify the categories of
412 outcomes that will be reported in the summary, plugins can modify
413 the ``event.reportCategories`` dictionary. Plugins can set, wrap or
414 capture the output stream by reading or setting
415 ``event.stream``. If you want to print to the console, write to
416 ``event.stream``. Remember to respect ``self.session.verbosity`` when
417 printing to the console.
418
419 .. function :: wasSuccessful(self, event)
420
421 :param event: A :class:`nose2.events.ResultSuccessEvent` instance
422
423 Plugins can use this hook to mark a test run as successful or
424 unsuccessful. If not plugin marks the run as successful, the
425 default state is failure. To mark a run as successful, set
426 ``event.success`` to ``True``. Be ware that other plugins may set this
427 attribute as well!
428
429 .. function :: afterSummaryReport(self, event)
430
431 :param event: A :class:`nose2.events.ReportSummaryEvent` instance
432
433 Plugins can use this hook to output a report to the user after the
434 summary line is output. If you want to print to the console, write
435 to ``event.stream``. Remember to respect ``self.session.verbosity``
436 when printing to the console.
437
438
439 User Interaction Hooks
440 ----------------------
441
442 These hooks are called when plugins want to interact with the user.
443
444 .. function :: beforeInteraction(event)
445
446 :param event: A :class:`nose2.events.UserInteractionEvent`
447
448 Plugins should respond to this hook by getting out of the way of
449 user interaction, if the need to, or setting ``event.handled`` and
450 returning ``False``, if they need to but can't.
451
452
453 .. function :: afterInteraction(event)
454
455 :param event: A :class:`nose2.events.UserInteractionEvent`
456
457 Plugins can respond to this hook by going back to whatever they
458 were doing before the user stepped in and started poking around.