Plugin Interface — nose 1.3.7 documentation

# Plugin Interface

## Plugin base class

class `nose.plugins.base.` `Plugin`

  Base class for nose plugins. It’s recommended but not necessary to subclass this class to create a plugin, but all plugins must implement options(self, parser, env) and configure(self, options, conf), and must have the attributes enabled, name and score. The name attribute may contain hyphens (‘-‘).

  Plugins should not be enabled by default.

  Subclassing Plugin (and calling the superclass methods in __init__, configure, and options, if you override them) will give your plugin some friendly default behavior:

  - A –with-$name option will be added to the command line interface to enable the plugin, and a corresponding environment variable will be used as the default value. The plugin class’s docstring will be used as the help for this option.
  - The plugin will not be enabled unless this option is selected by the user.
  `addOptions`(parser, env=None)

    Add command-line options for this plugin.

    The base plugin class adds –with-$name by default, used to enable the plugin.

    Warning

    Don’t implement addOptions unless you want to override all default option handling behavior, including warnings for conflicting options. Implement `options` instead.

  `add_options`(parser, env=None)

    Non-camel-case version of func name for backwards compatibility.

    Warning

    DEPRECATED: Do not use this method, use `options` instead.

  `configure`(options, conf)

    Configure the plugin and system, based on selected options.

    The base plugin class sets the plugin to enabled if the enable option for the plugin (self.enableOpt) is true.

  `help`()

    Return help for this plugin. This will be output as the help section of the –with-$name option that enables the plugin.

  `options`(parser, env)

    Register commandline options.

    Implement this method for normal options behavior with protection from OptionConflictErrors. If you override this method and want the default –with-$name option to be registered, be sure to call super().

## Nose plugin API

Plugins may implement any or all of the methods documented below. Please note that they must not subclass IPluginInterface; IPluginInterface is only a description of the plugin API.

When plugins are called, the first plugin that implements a method and returns a non-None value wins, and plugin processing ends. The exceptions to this are methods marked as generative or chainable. generative methods combine the output of all plugins that respond with an iterable into a single flattened iterable response (a generator, really). chainable methods pass the results of calling plugin A as the input to plugin B, where the positions in the chain are determined by the plugin sort order, which is in order by score descending.

In general, plugin methods correspond directly to methods of nose.selector.Selector, nose.loader.TestLoader and nose.result.TextTestResult are called by those methods when they are called. In some cases, the plugin hook doesn’t neatly match the method in which it is called; for those, the documentation for the hook will tell you where in the test process it is called.

Plugin hooks fall into four broad categories: selecting and loading tests, handling errors raised by tests, preparing objects used in the testing process, and watching and reporting on test results.

### Selecting and loading tests

To alter test selection behavior, implement any necessary want* methods as outlined below. Keep in mind, though, that when your plugin returns True from a want* method, you will send the requested object through the normal test collection process. If the object represents something from which normal tests can’t be collected, you must also implement a loader method to load the tests.

Examples:

- The builtin doctests plugin implements wantFile to enable loading of doctests from files that are not python modules. It also implements loadTestsFromModule to load doctests from python modules, and loadTestsFromFile to load tests from the non-module files selected by wantFile.
- The builtin attrib plugin implements wantFunction and wantMethod so that it can reject tests that don’t match the specified attributes.

### Handling errors

To alter error handling behavior – for instance to catch a certain class of exception and handle it differently from the normal error or failure handling – you should subclass `nose.plugins.errorclass.ErrorClassPlugin`. See the section on ErrorClass plugins for more details.

Examples:

- The builtin skip and deprecated plugins are ErrorClass plugins.

### Preparing test objects

To alter, get a handle on, or replace test framework objects such as the loader, result, runner, and test cases, use the appropriate prepare methods. The simplest reason to use prepare is in the case that you need to use an object yourself. For example, the isolate plugin implements prepareTestLoader so that it can use the loader later on to load tests. If you return a value from a prepare method, that value will be used in place of the loader, result, runner or test case, depending on which prepare method you use. Be aware that when replacing test cases, you are replacing the entire test case – including the whole run(result) method of the unittest.TestCase – so if you want normal unittest test result reporting, you must implement the same calls to result as unittest.TestCase.run.

Examples:

- The builtin isolate plugin implements prepareTestLoader but does not replace the test loader.
- The builtin profile plugin implements prepareTest and does replace the top-level test case by returning the case wrapped in the profiler function.

### Watching or reporting on tests

To record information about tests or other modules imported during the testing process, output additional reports, or entirely change test report output, implement any of the methods outlined below that correspond to TextTestResult methods.

Examples:

- The builtin cover plugin implements begin and report to capture and report code coverage metrics for all or selected modules loaded during testing.
- The builtin profile plugin implements begin, prepareTest and report to record and output profiling information. In this case, the plugin’s prepareTest method constructs a function that runs the test through the hotshot profiler’s runcall() method.

## Plugin interface methods

class `nose.plugins.base.` `IPluginInterface`

  IPluginInterface describes the plugin API. Do not subclass or use this class directly.

  `addDeprecated`(test)

    Called when a deprecated test is seen. DO NOT return a value unless you want to stop other plugins from seeing the deprecated test.

    Warning

    DEPRECATED – check error class in addError instead

  `addError`(test, err)

    Called when a test raises an uncaught exception. DO NOT return a value unless you want to stop other plugins from seeing that the test has raised an error.

    Parameters: | test (`nose.case.Test`) – the test case err (3-tuple) – sys.exc_info() tuple

  `addFailure`(test, err)

    Called when a test fails. DO NOT return a value unless you want to stop other plugins from seeing that the test has failed.

    Parameters: | test (`nose.case.Test`) – the test case err (sys.exc_info() tuple) – 3-tuple

  `addOptions`(parser, env)

    Called to allow plugin to register command-line options with the parser. DO NOT return a value from this method unless you want to stop all other plugins from setting their options.

    Warning

    DEPRECATED – implement `options` instead.

  `addSkip`(test)

    Called when a test is skipped. DO NOT return a value unless you want to stop other plugins from seeing the skipped test.

    Warning

    DEPRECATED – check error class in addError instead

  `addSuccess`(test)

    Called when a test passes. DO NOT return a value unless you want to stop other plugins from seeing the passing test.

    Parameters: | test (`nose.case.Test`) – the test case

  `add_options`(parser, env)

    Called to allow plugin to register command-line options with the parser. DO NOT return a value from this method unless you want to stop all other plugins from setting their options.

    Warning

    DEPRECATED – implement `options` instead.

  `afterContext`()

    Called after a context (generally a module) has been lazy-loaded, imported, setup, had its tests loaded and executed, and torn down.

  `afterDirectory`(path)

    Called after all tests have been loaded from directory at path and run.

    Parameters: | path (string) – the directory that has finished processing

  `afterImport`(filename, module)

    Called after module is imported from filename. afterImport is called even if the import failed.

    Parameters: | filename (string) – The file that was loaded module (string) – The name of the module

  `afterTest`(test)

    Called after the test has been run and the result recorded (after stopTest).

    Parameters: | test (`nose.case.Test`) – the test case

  `beforeContext`()

    Called before a context (generally a module) is examined. Because the context is not yet loaded, plugins don’t get to know what the context is; so any context operations should use a stack that is pushed in beforeContext and popped in afterContext to ensure they operate symmetrically.

    beforeContext and afterContext are mainly useful for tracking and restoring global state around possible changes from within a context, whatever the context may be. If you need to operate on contexts themselves, see startContext and stopContext, which are passed the context in question, but are called after it has been loaded (imported in the module case).

  `beforeDirectory`(path)

    Called before tests are loaded from directory at path.

    Parameters: | path – the directory that is about to be processed

  `beforeImport`(filename, module)

    Called before module is imported from filename.

    Parameters: | filename – The file that will be loaded module (string) – The name of the module found in file

  `beforeTest`(test)

    Called before the test is run (before startTest).

    Parameters: | test (`nose.case.Test`) – the test case

  `begin`()

    Called before any tests are collected or run. Use this to perform any setup needed before testing begins.

  `configure`(options, conf)

    Called after the command line has been parsed, with the parsed options and the config container. Here, implement any config storage or changes to state or operation that are set by command line options.

    DO NOT return a value from this method unless you want to stop all other plugins from being configured.

  `describeTest`(test)

    Return a test description.

    Called by `nose.case.Test.shortDescription()`.

    Parameters: | test (`nose.case.Test`) – the test case

  `finalize`(result)

    Called after all report output, including output from all plugins, has been sent to the stream. Use this to print final test results or perform final cleanup. Return None to allow other plugins to continue printing, or any other value to stop them.

    Parameters: | result – test result object

    Note

    When tests are run under a test runner other than `nose.core.TextTestRunner`, such as via `python setup.py test`, this method may be called before the default report output is sent.

  `formatError`(test, err)

    Called in result.addError, before plugin.addError. If you want to replace or modify the error tuple, return a new error tuple, otherwise return err, the original error tuple.

    Parameters: | test (`nose.case.Test`) – the test case err (3-tuple) – sys.exc_info() tuple

  `formatFailure`(test, err)

    Called in result.addFailure, before plugin.addFailure. If you want to replace or modify the error tuple, return a new error tuple, otherwise return err, the original error tuple.

    Parameters: | test (`nose.case.Test`) – the test case err (3-tuple) – sys.exc_info() tuple

  `handleError`(test, err)

    Called on addError. To handle the error yourself and prevent normal error processing, return a true value.

    Parameters: | test (`nose.case.Test`) – the test case err (3-tuple) – sys.exc_info() tuple

  `handleFailure`(test, err)

    Called on addFailure. To handle the failure yourself and prevent normal failure processing, return a true value.

    Parameters: | test (`nose.case.Test`) – the test case err (3-tuple) – sys.exc_info() tuple

  `loadTestsFromDir`(path)

    Return iterable of tests from a directory. May be a generator. Each item returned must be a runnable unittest.TestCase (or subclass) instance or suite instance. Return None if your plugin cannot collect any tests from directory.

    Parameters: | path – The path to the directory.

  `loadTestsFromFile`(filename)

    Return tests in this file. Return None if you are not interested in loading any tests, or an iterable if you are and can load some. May be a generator. If you are interested in loading tests from the file and encounter no errors, but find no tests, yield False or return [False].

    Note

    This method replaces loadTestsFromPath from the 0.9 API.

    Parameters: | filename – The full path to the file or directory.

  `loadTestsFromModule`(module, path=None)

    Return iterable of tests in a module. May be a generator. Each item returned must be a runnable unittest.TestCase (or subclass) instance. Return None if your plugin cannot collect any tests from module.

    Parameters: | module (python module) – The module object path – the path of the module to search, to distinguish from namespace package modules Note NEW. The `path` parameter will only be passed by nose 0.11 or above.

  `loadTestsFromName`(name, module=None, importPath=None)

    Return tests in this file or module. Return None if you are not able to load any tests, or an iterable if you are. May be a generator.

    Parameters: | name – The test name. May be a file or module name plus a test callable. Use split_test_name to split into parts. Or it might be some crazy name of your own devising, in which case, do whatever you want. module – Module from which the name is to be loaded importPath – Path from which file (must be a python module) was found Warning DEPRECATED: this argument will NOT be passed.

  `loadTestsFromNames`(names, module=None)

    Return a tuple of (tests loaded, remaining names). Return None if you are not able to load any tests. Multiple plugins may implement loadTestsFromNames; the remaining name list from each will be passed to the next as input.

    Parameters: | names (iterable) – List of test names. module – Module from which the names are to be loaded

  `loadTestsFromPath`(path)

    Warning

    DEPRECATED – use loadTestsFromFile instead

  `loadTestsFromTestCase`(cls)

    Return tests in this test case class. Return None if you are not able to load any tests, or an iterable if you are. May be a generator.

    Parameters: | cls – The test case class. Must be subclass of `unittest.TestCase`.

  `loadTestsFromTestClass`(cls)

    Return tests in this test class. Class will not be a unittest.TestCase subclass. Return None if you are not able to load any tests, an iterable if you are. May be a generator.

    Parameters: | cls – The test case class. Must be not be subclass of `unittest.TestCase`.

  `makeTest`(obj, parent)

    Given an object and its parent, return or yield one or more test cases. Each test must be a unittest.TestCase (or subclass) instance. This is called before default test loading to allow plugins to load an alternate test case or cases for an object. May be a generator.

    Parameters: | obj – The object to be made into a test parent – The parent of obj (eg, for a method, the class)

  `options`(parser, env)

    Called to allow plugin to register command line options with the parser.

    DO NOT return a value from this method unless you want to stop all other plugins from setting their options.

    Parameters: | parser (`ConfigParser.ConfigParser`) – options parser instance env – environment, default is os.environ

  `prepareTest`(test)

    Called before the test is run by the test runner. Please note the article the in the previous sentence: prepareTest is called only once, and is passed the test case or test suite that the test runner will execute. It is not called for each individual test case. If you return a non-None value, that return value will be run as the test. Use this hook to wrap or decorate the test with another function. If you need to modify or wrap individual test cases, use prepareTestCase instead.

    Parameters: | test (`nose.case.Test`) – the test case

  `prepareTestCase`(test)

    Prepare or wrap an individual test case. Called before execution of the test. The test passed here is a nose.case.Test instance; the case to be executed is in the test attribute of the passed case. To modify the test to be run, you should return a callable that takes one argument (the test result object) – it is recommended that you do not side-effect the nose.case.Test instance you have been passed.

    Keep in mind that when you replace the test callable you are replacing the run() method of the test case – including the exception handling and result calls, etc.

    Parameters: | test (`nose.case.Test`) – the test case

  `prepareTestLoader`(loader)

    Called before tests are loaded. To replace the test loader, return a test loader. To allow other plugins to process the test loader, return None. Only one plugin may replace the test loader. Only valid when using nose.TestProgram.

    Parameters: | loader – `nose.loader.TestLoader` (or other loader) instance

  `prepareTestResult`(result)

    Called before the first test is run. To use a different test result handler for all tests than the given result, return a test result handler. NOTE however that this handler will only be seen by tests, that is, inside of the result proxy system. The TestRunner and TestProgram – whether nose’s or other – will continue to see the original result handler. For this reason, it is usually better to monkeypatch the result (for instance, if you want to handle some exceptions in a unique way). Only one plugin may replace the result, but many may monkeypatch it. If you want to monkeypatch and stop other plugins from doing so, monkeypatch and return the patched result.

    Parameters: | result – `nose.result.TextTestResult` (or other result) instance

  `prepareTestRunner`(runner)

    Called before tests are run. To replace the test runner, return a test runner. To allow other plugins to process the test runner, return None. Only valid when using nose.TestProgram.

    Parameters: | runner – `nose.core.TextTestRunner` (or other runner) instance

  `report`(stream)

    Called after all error output has been printed. Print your plugin’s report to the provided stream. Return None to allow other plugins to print reports, any other value to stop them.

    Parameters: | stream (file-like object) – stream object; send your output here

  `setOutputStream`(stream)

    Called before test output begins. To direct test output to a new stream, return a stream object, which must implement a write(msg) method. If you only want to note the stream, not capture or redirect it, then return None.

    Parameters: | stream (file-like object) – stream object; send your output here

  `startContext`(context)

    Called before context setup and the running of tests in the context. Note that tests have already been loaded from the context before this call.

    Parameters: | context – the context about to be setup. May be a module or class, or any other object that contains tests.

  `startTest`(test)

    Called before each test is run. DO NOT return a value unless you want to stop other plugins from seeing the test start.

    Parameters: | test (`nose.case.Test`) – the test case

  `stopContext`(context)

    Called after the tests in a context have run and the context has been torn down.

    Parameters: | context – the context that has been torn down. May be a module or class, or any other object that contains tests.

  `stopTest`(test)

    Called after each test is run. DO NOT return a value unless you want to stop other plugins from seeing that the test has stopped.

    Parameters: | test (`nose.case.Test`) – the test case

  `testName`(test)

    Return a short test name. Called by nose.case.Test.__str__.

    Parameters: | test (`nose.case.Test`) – the test case

  `wantClass`(cls)

    Return true if you want the main test selector to collect tests from this class, false if you don’t, and None if you don’t care.

    Parameters: | cls – The class being examined by the selector

  `wantDirectory`(dirname)

    Return true if you want test collection to descend into this directory, false if you do not, and None if you don’t care.

    Parameters: | dirname – Full path to directory being examined by the selector

  `wantFile`(file)

    Return true if you want to collect tests from this file, false if you do not and None if you don’t care.

    Change from 0.9: The optional package parameter is no longer passed.

    Parameters: | file – Full path to file being examined by the selector

  `wantFunction`(function)

    Return true to collect this function as a test, false to prevent it from being collected, and None if you don’t care.

    Parameters: | function – The function object being examined by the selector

  `wantMethod`(method)

    Return true to collect this method as a test, false to prevent it from being collected, and None if you don’t care.

    Parameters: | method (unbound method) – The method object being examined by the selector

  `wantModule`(module)

    Return true if you want to collection to descend into this module, false to prevent the collector from descending into the module, and None if you don’t care.

    Parameters: | module (python module) – The module object being examined by the selector

  `wantModuleTests`(module)

    Warning

    DEPRECATED – this method will not be called, it has been folded into wantModule.