pytest plugin

Note

This plugin disables pytest’s standard doctest plugin.

The pytest plugin is built on top of the Doctest w/ docutils module, which is in turn compatible with doctest.

Using fixtures

Normal pytest convention apply, a visible conftest.py must be available for the file being tested.

This requires a understanding of confest.py - in the same way pytest’s vanilla doctest plugin does.

You know your project’s package structure best, the follow just serve as examples of new places conftest.py will be needed:

Example: Root-level README

In other words, if you want to test a project’s README.md in the project root, a conftest.py would be needed at the root-level. This also has the benefit of reducing duplication:

conftest.py
    # content of conftest.py
    import pytest

    @pytest.fixture
    def username():
        return 'username'

README.rst
    Our project
    -----------

    .. doctest::

        # content of tests/test_something.py
        def test_username(username):
            assert username == 'username'

Now you can do:

$ pytest README.rst

Examples: docs/

Let’s assume .md and .rst files in docs/, this means you need to import conftest.py

docs/
	conftest.py
		# import conftest of project
		import pytest

		@pytest.fixture
		def username():
			return 'username'

	usage.rst
		Our project
		-----------

		.. doctest::

			# content of tests/test_something.py
			def test_username(username):
				assert username == 'username'

Now you can do:

$ pytest docs/

File support

reStructuredText (.rst)

$ pytest README.rst
$ pytest docs/

Markdown (.md)

If you install myst-parser, doctest will run on .md files.

$ pytest README.md
$ pytest docs/

Scanning python files

You can retain pytest’s standard doctest plugin of running doctests in .py by passing --doctest-docutils-modules:

$ py.test src/ --doctest-docutils-modules

You can disable it via --no-doctest-docutils-modules:

$ py.test src/ --no-doctest-docutils-modules

Case examples

API

pytest plugin for doctest w/ reStructuredText and markdown.

pytest_doctest_docutils.pytest_addoption(parser)
function[source]

Add options to py.test for doctest_docutils.

Parameters:

parser (Parser)

Return type:

None

pytest_doctest_docutils.pytest_configure(config)
function[source]

Disable pytest.doctest to prevent running tests twice.

Todo: Find a way to make these plugins cooperate without collecting twice.

Parameters:

config (Config)

Return type:

None

pytest_doctest_docutils._unblock_doctest(config)
function[source]

Unblock doctest plugin (pytest 8.1+ only).

Re-enables the built-in doctest plugin after it was blocked by pytest_configure. Uses the public unblock() API introduced in pytest 8.1.0.

Parameters:

config (pytest.Config) – The pytest configuration object

Returns:

True if unblocked successfully, False if API not available

Return type:

bool

pytest_doctest_docutils.pytest_unconfigure()
function[source]

Unconfigure hook for pytest-doctest-docutils.

Return type:

None

pytest_doctest_docutils.pytest_collect_file(file_path, parent)
function[source]

Test collector for pytest-doctest-docutils.

Parameters:
Return type:

DocTestDocutilsFile | DoctestModule | None

pytest_doctest_docutils._is_doctest(config, path, parent)
function[source]
Parameters:
Return type:

bool

pytest_doctest_docutils._init_runner_class()
function[source]
Return type:

type[DocTestRunner]

pytest_doctest_docutils._get_allow_unicode_flag()
function[source]

Register and return the ALLOW_UNICODE flag.

Return type:

int

pytest_doctest_docutils._get_allow_bytes_flag()
function[source]

Register and return the ALLOW_BYTES flag.

Return type:

int

pytest_doctest_docutils._get_number_flag()
function[source]

Register and return the NUMBER flag.

Return type:

int

pytest_doctest_docutils._get_flag_lookup()
function[source]
Return type:

dict[str, int]

pytest_doctest_docutils.get_optionflags(config)
function[source]

Fetch optionflags from pytest configuration.

Extracted from pytest.doctest 8.0 (license: MIT).

Parameters:

config (Config)

Return type:

int

pytest_doctest_docutils._get_runner(checker=None, verbose=None, optionflags=0, continue_on_failure=True)
function[source]
Parameters:
Return type:

DocTestRunner

class pytest_doctest_docutils.DocutilsDocTestRunner

Bases: DocTestRunner

DocTestRunner for doctest_docutils.

class pytest_doctest_docutils.DocTestDocutilsFile

Bases: Module

Pytest module for doctest_docutils.