/doc/plugins/interface.rst

https://bitbucket.org/jpellerin/nose/ · ReStructuredText · 122 lines · 91 code · 31 blank · 0 comment · 0 complexity · 5145c3b96d498404a14cdf73e6c55961 MD5 · raw file

  1. .. _plugin-interface:
  2. Plugin Interface
  3. ================
  4. Plugin base class
  5. -----------------
  6. .. autoclass :: nose.plugins.base.Plugin
  7. :members:
  8. Nose plugin API
  9. ---------------
  10. Plugins may implement any or all of the methods documented below. Please note
  11. that they *must not* subclass `IPluginInterface`; `IPluginInterface` is only a
  12. description of the plugin API.
  13. When plugins are called, the first plugin that implements a method and returns
  14. a non-None value wins, and plugin processing ends. The exceptions to this are
  15. methods marked as `generative` or `chainable`. `generative` methods combine
  16. the output of all plugins that respond with an iterable into a single
  17. flattened iterable response (a generator, really). `chainable` methods pass
  18. the results of calling plugin A as the input to plugin B, where the positions
  19. in the chain are determined by the plugin sort order, which is in order by
  20. `score` descending.
  21. In general, plugin methods correspond directly to methods of
  22. `nose.selector.Selector`, `nose.loader.TestLoader` and
  23. `nose.result.TextTestResult` are called by those methods when they are
  24. called. In some cases, the plugin hook doesn't neatly match the method in
  25. which it is called; for those, the documentation for the hook will tell you
  26. where in the test process it is called.
  27. Plugin hooks fall into four broad categories: selecting and loading tests,
  28. handling errors raised by tests, preparing objects used in the testing
  29. process, and watching and reporting on test results.
  30. Selecting and loading tests
  31. ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  32. To alter test selection behavior, implement any necessary `want*` methods as
  33. outlined below. Keep in mind, though, that when your plugin returns True from
  34. a `want*` method, you will send the requested object through the normal test
  35. collection process. If the object represents something from which normal tests
  36. can't be collected, you must also implement a loader method to load the tests.
  37. Examples:
  38. * The builtin :doc:`doctests plugin <doctests>` implements `wantFile` to
  39. enable loading of doctests from files that are not python modules. It
  40. also implements `loadTestsFromModule` to load doctests from
  41. python modules, and `loadTestsFromFile` to load tests from the
  42. non-module files selected by `wantFile`.
  43. * The builtin :doc:`attrib plugin <attrib>` implements `wantFunction` and
  44. `wantMethod` so that it can reject tests that don't match the
  45. specified attributes.
  46. Handling errors
  47. ^^^^^^^^^^^^^^^
  48. To alter error handling behavior -- for instance to catch a certain class of
  49. exception and handle it differently from the normal error or failure handling
  50. -- you should subclass :class:`nose.plugins.errorclass.ErrorClassPlugin`. See
  51. :doc:`the section on ErrorClass plugins <errorclasses>` for more details.
  52. Examples:
  53. * The builtin :doc:`skip <skip>` and :doc:`deprecated <deprecated>` plugins are
  54. ErrorClass plugins.
  55. Preparing test objects
  56. ^^^^^^^^^^^^^^^^^^^^^^
  57. To alter, get a handle on, or replace test framework objects such as the
  58. loader, result, runner, and test cases, use the appropriate prepare methods.
  59. The simplest reason to use prepare is in the case that you need to use an
  60. object yourself. For example, the isolate plugin implements `prepareTestLoader`
  61. so that it can use the loader later on to load tests. If you return a value
  62. from a prepare method, that value will be used in place of the loader, result,
  63. runner or test case, depending on which prepare method you use. Be aware that
  64. when replacing test cases, you are replacing the *entire* test case -- including
  65. the whole `run(result)` method of the `unittest.TestCase` -- so if you want
  66. normal unittest test result reporting, you must implement the same calls to
  67. result as `unittest.TestCase.run`.
  68. Examples:
  69. * The builtin :doc:`isolate plugin <isolate>` implements `prepareTestLoader`
  70. but does not replace the test loader.
  71. * The builtin :doc:`profile plugin <prof>` implements `prepareTest` and does
  72. replace the top-level test case by returning the case wrapped in
  73. the profiler function.
  74. Watching or reporting on tests
  75. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  76. To record information about tests or other modules imported during
  77. the testing process, output additional reports, or entirely change
  78. test report output, implement any of the methods outlined below that
  79. correspond to TextTestResult methods.
  80. Examples:
  81. * The builtin :doc:`cover plugin <cover>` implements `begin` and `report` to
  82. capture and report code coverage metrics for all or selected modules
  83. loaded during testing.
  84. * The builtin :doc:`profile plugin <prof>` implements `begin`, `prepareTest`
  85. and `report` to record and output profiling information. In this
  86. case, the plugin's `prepareTest` method constructs a function that
  87. runs the test through the hotshot profiler's runcall() method.
  88. Plugin interface methods
  89. ------------------------
  90. .. autoclass :: nose.plugins.base.IPluginInterface
  91. :members: