File: hook_reference.rst

package info (click to toggle)
nose2 0.15.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,912 kB
  • sloc: python: 10,721; makefile: 126
file content (458 lines) | stat: -rw-r--r-- 17,714 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
Hook reference
==============

.. note ::

   Hooks are listed here in order of execution.


Pre-registration Hooks
----------------------

.. function :: pluginsLoaded(self, event)

   :param event: :class:`nose2.events.PluginsLoadedEvent`

   The ``pluginsLoaded`` hook is called after all config files have been read,
   and all plugin classes loaded. Plugins that register automatically
   (those that call :meth:`nose2.events.Plugin.register` in ``__init__``
   or have ``always-on = True`` set in their config file sections) will
   have already been registered with the hooks they implement. Plugins
   waiting for command-line activation will not yet be registered.

   Plugins can use this hook to examine or modify the set of loaded plugins,
   inject their own hook methods using
   :meth:`nose2.events.PluginInterface.addMethod`, or take other
   actions to set up or configure themselves or the test run.

   Since ``pluginsLoaded`` is a pre-registration hook, it is called
   for *all plugins* that implement the method, whether they have
   registered or not. Plugins that do not automatically register
   themselves should limit their actions in this hook to
   configuration, since they may not actually be active during the
   test run.

.. function :: handleArgs(self, event)

   :param event: :class:`nose2.events.CommandLineArgsEvent`

   The ``handleArgs`` hook is called after all arguments from the command
   line have been parsed. Plugins can use this hook to handle command-line
   arguments in non-standard ways. They should not use it to try to modify
   arguments seen by other plugins, since the order in which plugins
   execute in a hook is not guaranteed.

   Since ``handleArgs`` is a pre-registration hook, it is called for
   *all plugins* that implement the method, whether they have registered
   or not. Plugins that do not automatically register
   themselves should limit their actions in this hook to
   configuration, since they may not actually be active during the
   test run.


Standard Hooks
--------------

These hooks are called for registered plugins only.


.. function :: createTests(self, event)

   :param event: A :class:`nose2.events.CreateTestsEvent` instance

   Plugins can take over test loading by returning a test suite and setting
   ``event.handled`` to True.

.. function :: loadTestsFromNames(self, event)

   :param event: A :class:`nose2.events.LoadFromNamesEvent` instance

   Plugins can return a test suite or list of test suites and set
   ``event.handled`` to ``True`` to prevent other plugins from loading
   tests from the given names, or append tests to
   ``event.extraTests``. Plugins can also remove names from
   ``event.names`` to prevent other plugins from acting on those
   names.

.. function :: loadTestsFromName(self, event)

   :param event: A :class:`nose2.events.LoadFromNameEvent` instance

   Plugins can return a test suite and set ``event.handled`` to ``True``
   to prevent other plugins from loading tests from the given name,
   or append tests to ``event.extraTests``.

.. function :: handleFile(self, event)

   :param event: A :class:`nose2.events.HandleFileEvent` instance

   Plugins can use this hook to load tests from files that are not
   Python modules. Plugins may either append tests to ``event.extraTest``,
   or, if they want to prevent other plugins from processing the file,
   set ``event.handled`` to True and return a test case or test suite.

.. function :: matchPath(self, event)

   :param event: A :class:`nose2.events.MatchPathEvent` instance

   Plugins can use this hook to prevent python modules from being
   loaded by the test loader or force them to be loaded by the test
   loader. Set ``event.handled`` to ``True`` and return ``False`` to cause the
   loader to skip the module. Set ``event.handled`` to ``True`` and return
   ``True`` to cause the loader to load the module.

.. function :: loadTestsFromModule(self, event)

   :param event: A :class:`nose2.events.LoadFromModuleEvent` instance

   Plugins can use this hook to load tests from test modules. To
   prevent other plugins from loading from the module, set
   ``event.handled`` and return a test suite. Plugins can also append
   tests to ``event.extraTests`` -- usually that's what you want to
   do, since that will allow other plugins to load their tests from
   the module as well.

   See also :ref:`this warning <loading-from-module>` about test cases
   not defined in the module.

.. function :: loadTestsFromTestCase(self, event)

   :param event: A :class:`nose2.events.LoadFromTestCaseEvent` instance

   Plugins can use this hook to load tests from a
   :class:`unittest.TestCase`. To prevent other plugins from loading
   tests from the test case, set ``event.handled`` to ``True`` and return
   a test suite. Plugins can also append tests to ``event.extraTests``
   -- usually that's what you want to do, since that will allow other
   plugins to load their tests from the test case as well.

.. function :: getTestCaseNames(self, event)

   :param event: A :class:`nose2.events.GetTestCaseNamesEvent` instance

   Plugins can use this hook to limit or extend the list of test case
   names that will be loaded from a :class:`unittest.TestCase` by the
   standard nose2 test loader plugins (and other plugins that respect
   the results of the hook). To force a specific list of names, set
   ``event.handled`` to ``True`` and return a list: this exact list will
   be the only test case names loaded from the test case. Plugins can
   also extend the list of names by appending test names to
   ``event.extraNames``, and exclude names by appending test names to
   ``event.excludedNames``.

.. function :: runnerCreated(self, event)

   :param event: A :class:`nose2.events.RunnerCreatedEvent` instance

   Plugins can use this hook to wrap, capture or replace the test
   runner. To replace the test runner, set ``event.runner``.

.. function :: resultCreated(self, event)

   :param event: A :class:`nose2.events.ResultCreatedEvent` instance

   Plugins can use this hook to wrap, capture or replace the test
   result. To replace the test result, set ``event.result``.

.. function :: startTestRun(self, event)

   :param event: A :class:`nose2.events.StartTestRunEvent` instance

   Plugins can use this hook to take action before the start of the
   test run, and to replace or wrap the test executor. To replace the
   executor, set ``event.executeTests``. This must be a callable that
   takes two arguments: the top-level test and the test result.

   To prevent the test executor from running at all, set
   ``event.handled`` to ``True``.

.. function :: startLayerSetup(self, event)

   :param event: A :class:`nose2.events.StartLayerSetupEvent` instance (only
                 available in suites with layers).

   Plugins can use this hook to take action before the start of the ``setUp``
   in a layer.

.. function :: stopLayerSetup(self, event)

   :param event: A :class:`nose2.events.StopLayerSetupEvent` instance (only
                available in suites with layers).

   Plugins can use this hook to take action after ``setUp`` finishes, in a
   layer.

.. function :: startLayerSetupTest(self, event)

   :param event: A :class:`nose2.events.StartLayerSetupTestEvent` instance
                (only available in suites with layers).

   Plugins can use this hook to take action before the start of ``testSetUp``
   in a layer.

.. function :: stopLayerSetupTest(self, event)

   :param event: A :class:`nose2.events.StopLayerSetupTestEvent` instance (only
                 available in suites with layers).

   Plugins can use this hook to take action after ``testSetUp`` finishes, in a
   layer.

.. function :: startTest(self, event)

   :param event: A :class:`nose2.events.StartTestEvent` instance

   Plugins can use this hook to take action immediately before a test
   runs.

.. function :: reportStartTest(self, event)

   :param event: A :class:`nose2.events.ReportTestEvent` instance

   Plugins can use this hook to produce output for the user at the
   start of a test. If you want to print to the console, write to
   ``event.stream``. Remember to respect ``self.session.verbosity`` when
   printing to the console. To prevent other plugins from reporting
   to the user, set ``event.handled`` to ``True``.

.. function :: describeTest(self, event)

   :param event: A :class:`nose2.events.DescribeTestEvent` instance

   Plugins can use this hook to alter test descriptions. To return a
   nonstandard description for a test, set ``event.description``. Be
   aware that other plugins may have set this also!

.. function :: setTestOutcome(self, event)

   :param event: A :class:`nose2.events.TestOutcomeEvent` instance

   Plugins can use this hook to alter test outcomes. Plugins can
   ``event.outcome`` to change the outcome of the event, tweak, change
   or remove ``event.exc_info``, set or clear ``event.expected``, and
   so on.

.. function :: testOutcome(self, event)

   :param event: A :class:`nose2.events.TestOutcomeEvent` instance

   Plugins can use this hook to take action based on the outcome of
   tests. Plugins *must not* alter test outcomes in this hook: that's
   what :func:`setTestOutcome` is for. Here, plugins may only react to
   the outcome event, not alter it.

.. function :: reportSuccess(self, event)

   :param event: A :class:`nose2.events.LoadFromNamesEvent` instance

   Plugins can use this hook to report test success to the user. If
   you want to print to the console, write to
   ``event.stream``. Remember to respect ``self.session.verbosity`` when
   printing to the console. To prevent other plugins from reporting to
   the user, set ``event.handled`` to ``True``.

.. function :: reportError(self, event)

   :param event: A :class:`nose2.events.ReportTestEvent` instance

   Plugins can use this hook to report a test error to the user. If
   you want to print to the console, write to
   ``event.stream``. Remember to respect ``self.session.verbosity`` when
   printing to the console. To prevent other plugins from reporting to
   the user, set ``event.handled`` to ``True``.

.. function :: reportFailure(self, event)

   :param event: A :class:`nose2.events.ReportTestEvent` instance

   Plugins can use this hook to report test failure to the user. If
   you want to print to the console, write to
   ``event.stream``. Remember to respect ``self.session.verbosity`` when
   printing to the console. To prevent other plugins from reporting to
   the user, set ``event.handled`` to ``True``.

.. function :: reportSkip(self, event)

   :param event: A :class:`nose2.events.ReportTestEvent` instance

   Plugins can use this hook to report a skipped test to the user. If
   you want to print to the console, write to
   ``event.stream``. Remember to respect ``self.session.verbosity`` when
   printing to the console. To prevent other plugins from reporting to
   the user, set ``event.handled`` to ``True``.

.. function :: reportExpectedFailure(self, event)

   :param event: A :class:`nose2.events.ReportTestEvent` instance

   Plugins can use this hook to report an expected failure to the
   user. If you want to print to the console, write to
   ``event.stream``. Remember to respect ``self.session.verbosity`` when
   printing to the console. To prevent other plugins from reporting to
   the user, set ``event.handled`` to ``True``.

.. function :: reportUnexpectedSuccess(self, event)

   :param event: A :class:`nose2.events.ReportTestEvent` instance

   Plugins can use this hook to report an unexpected success to the
   user. If you want to print to the console, write to
   ``event.stream``. Remember to respect ``self.session.verbosity`` when
   printing to the console. To prevent other plugins from reporting to
   the user, set ``event.handled`` to ``True``.

.. function :: reportOtherOutcome(self, event)

   :param event: A :class:`nose2.events.ReportTestEvent` instance

   Plugins can use this hook to report a custom test outcome to the
   user. If you want to print to the console, write to
   ``event.stream``. Remember to respect ``self.session.verbosity`` when
   printing to the console. To prevent other plugins from reporting to
   the user, set ``event.handled`` to ``True``.

   nose2 will never produce this event by itself. It only gets triggered if
   a plugin creates a test result with an unrecognized outcome.

.. function :: stopTest(self, event)

   :param event: A :class:`nose2.events.StopTestEvent` instance

   Plugins can use this hook to take action after a test has completed
   running and reported its outcome.

.. function :: startLayerTeardownTest(self, event)

   :param event: A :class:`nose2.events.StartLayerTeardownTestEvent` instance
                 (only available in suites with layers).

   Plugins can use this hook to take action before the start of
   ``testTearDown()`` in a layer.

.. function :: stopLayerTeardownTest(self, event)

   :param event: A :class:`nose2.events.StopLayerTeardownTestEvent` instance
                 (only available in suites with layers).

   Plugins can use this hook to take action after ``testTearDown()`` finishes,
   in a layer.

.. function :: startLayerTeardown(self, event)

   :param event: A :class:`nose2.events.StartLayerTeardownEvent` instance (only
                 available in suites with layers).

   Plugins can use this hook to take action before the start of the
   ``tearDown()`` in a layer.

.. function :: stopLayerTeardown(self, event)

   :param event: A :class:`nose2.events.StopLayerTeardownEvent` instance (only
                 available in suites with layers).

   Plugins can use this hook to take action after ``tearDown()`` finishes, in a
   layer.

.. function :: stopTestRun(self, event)

   :param event: A :class:`nose2.events.StopTestRunEvent` instance

   Plugins can use this hook to take action at the end of a test run.

.. function :: afterTestRun(self, event)

   :param event: A :class:`nose2.events.StopTestRunEvent` instance

   .. note ::

      New in version 0.2

   Plugins can use this hook to take action *after* the end of a test
   run, such as printing summary reports like the builtin result
   reporter plugin :class:`nose2.plugins.result.ResultReporter`.

.. function :: resultStop(self, event)

   :param event: A :class:`nose2.events.ResultStopEvent` instance

   Plugins can use this hook to *prevent* other plugins from stopping
   a test run. This hook fires when something calls
   :meth:`nose2.result.PluggableTestResult.stop`. If you want to
   prevent this from stopping the test run, set ``event.shouldStop``
   to ``False``.

.. function :: beforeErrorList(self, event)

   :param event: A :class:`nose2.events.ReportSummaryEvent` instance

   Plugins can use this hook to output or modify summary information
   before the list of errors and failures is output. To modify the
   categories of outcomes that will be reported, plugins can modify
   the ``event.reportCategories`` dictionary. Plugins can set, wrap, or
   capture the output stream by reading or setting ``event.stream``.
   If you want to print to the console, write to
   ``event.stream``. Remember to respect ``self.session.verbosity`` when
   printing to the console.

.. function :: outcomeDetail(self, event)

   :param event: A :class:`nose2.events.OutcomeDetailEvent` instance

   Plugins can use this hook to add additional elements to error list
   output. Append extra detail lines to ``event.extraDetail``; these
   will be joined together with newlines before being output as part
   of the detailed error/failure message, after the traceback.

.. function :: beforeSummaryReport(self, event)

   :param event: A :class:`nose2.events.ReportSummaryEvent` instance

   Plugins can use this hook to output or modify summary information
   before the summary lines are output.  To modify the categories of
   outcomes that will be reported in the summary, plugins can modify
   the ``event.reportCategories`` dictionary. Plugins can set, wrap or
   capture the output stream by reading or setting
   ``event.stream``. If you want to print to the console, write to
   ``event.stream``. Remember to respect ``self.session.verbosity`` when
   printing to the console.

.. function :: wasSuccessful(self, event)

   :param event: A :class:`nose2.events.ResultSuccessEvent` instance

   Plugins can use this hook to mark a test run as successful or
   unsuccessful. If not plugin marks the run as successful, the
   default state is failure. To mark a run as successful, set
   ``event.success`` to ``True``. Be ware that other plugins may set this
   attribute as well!

.. function :: afterSummaryReport(self, event)

   :param event: A :class:`nose2.events.ReportSummaryEvent` instance

   Plugins can use this hook to output a report to the user after the
   summary line is output. If you want to print to the console, write
   to ``event.stream``. Remember to respect ``self.session.verbosity``
   when printing to the console.


User Interaction Hooks
----------------------

These hooks are called when plugins want to interact with the user.

.. function :: beforeInteraction(event)

   :param event: A :class:`nose2.events.UserInteractionEvent`

   Plugins should respond to this hook by getting out of the way of
   user interaction, if the need to, or setting ``event.handled`` and
   returning ``False``, if they need to but can't.


.. function :: afterInteraction(event)

   :param event: A :class:`nose2.events.UserInteractionEvent`

   Plugins can respond to this hook by going back to whatever they
   were doing before the user stepped in and started poking around.