Doctests: run doctests with nose

Use the Doctest plugin with --with-doctest or the NOSE_WITH_DOCTEST environment variable to enable collection and execution of doctests. Because doctests are usually included in the tested package (instead of being grouped into packages or modules of their own), nose only looks for them in the non-test packages it discovers in the working directory.

Doctests may also be placed into files other than python modules, in which case they can be collected and executed by using the --doctest-extension switch or NOSE_DOCTEST_EXTENSION environment variable to indicate which file extension(s) to load.

When loading doctests from non-module files, use the --doctest-fixtures switch to specify how to find modules containing fixtures for the tests. A module name will be produced by appending the value of that switch to the base name of each doctest file loaded. For example, a doctest file “widgets.rst” with the switch --doctest_fixtures=_fixt will load fixtures from the module widgets_fixt.py.

A fixtures module may define any or all of the following functions:

  • setup([module]) or setup_module([module])

    Called before the test runs. You may raise SkipTest to skip all tests.

  • teardown([module]) or teardown_module([module])

    Called after the test runs, if setup/setup_module did not raise an unhandled exception.

  • setup_test(test)

    Called before the test. NOTE: the argument passed is a doctest.DocTest instance, not a unittest.TestCase.

  • teardown_test(test)

    Called after the test, if setup_test did not raise an exception. NOTE: the argument passed is a doctest.DocTest instance, not a unittest.TestCase.

Doctests are run like any other test, with the exception that output capture does not work; doctest does its own output capture while running a test.

Note

See ../doc_tests/test_doctest_fixtures/doctest_fixtures for additional documentation and examples.

Options

--with-doctest

Enable plugin Doctest: Activate doctest plugin to find and run doctests in non-test modules. [NOSE_WITH_DOCTEST]

--doctest-tests

Also look for doctests in test modules. Note that classes, methods and functions should have either doctests or non-doctest tests, not both. [NOSE_DOCTEST_TESTS]

--doctest-extension=EXT

Also look for doctests in files with this extension [NOSE_DOCTEST_EXTENSION]

--doctest-result-variable=VAR

Change the variable name set to the result of the last interpreter command from the default ‘_’. Can be used to avoid conflicts with the _() function used for text translation. [NOSE_DOCTEST_RESULT_VAR]

--doctest-fixtures=SUFFIX

Find fixtures for a doctest file in module with this name appended to the base name of the doctest file

Plugin

class nose.plugins.doctests.Doctest

Bases: nose.plugins.base.Plugin

Activate doctest plugin to find and run doctests in non-test modules.

configure(options, config)

Configure plugin.

loadTestsFromFile(filename)

Load doctests from the file.

Tests are loaded only if filename’s extension matches configured doctest extension.

loadTestsFromModule(module)

Load doctests from the module.

makeTest(obj, parent)

Look for doctests in the given object, which will be a function, method or class.

options(parser, env)

Register commmandline options.

prepareTestLoader(loader)

Capture loader’s suiteClass.

This is used to create test suites from doctest files.

suiteClass

alias of DoctestSuite

wantFile(file)

Override to select all modules and any file ending with configured doctest extension.

Source

Table Of Contents

Previous topic

Deprecated: mark tests as deprecated

Next topic

Failure Detail: introspect asserts

This Page