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¶
-
Documentation w/ docutils:
-
Note:
pytest README.mdrequires you have aconftest.pydirectly in the project root
-
Configuration:
Doctests support pytest fixtures through
doctest_namespace
API¶
pytest plugin for doctest w/ reStructuredText and markdown.
See also
This is a derivative of my PR https://github.com/thisch/pytest-sphinx/pull/38 to pytest-sphinx (BSD 3-clause), 2022-09-03.
-
pytest_doctest_docutils.pytest_addoption(parser)¶
Add options to py.test for doctest_docutils.
-
pytest_doctest_docutils.pytest_configure(config)¶
Disable pytest.doctest to prevent running tests twice.
Todo: Find a way to make these plugins cooperate without collecting twice.
-
pytest_doctest_docutils._unblock_doctest(config)¶
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:
-
pytest_doctest_docutils.pytest_unconfigure()¶
Unconfigure hook for pytest-doctest-docutils.
- Return type:
-
pytest_doctest_docutils.pytest_collect_file(file_path, parent)¶
Test collector for pytest-doctest-docutils.
- Parameters:
- Return type:
DocTestDocutilsFile | DoctestModule | None
-
pytest_doctest_docutils._is_doctest(config, path, parent)¶
-
pytest_doctest_docutils._init_runner_class()¶
- Return type:
-
pytest_doctest_docutils._get_allow_unicode_flag()¶
Register and return the ALLOW_UNICODE flag.
- Return type:
-
pytest_doctest_docutils._get_allow_bytes_flag()¶
Register and return the ALLOW_BYTES flag.
- Return type:
-
pytest_doctest_docutils._get_flag_lookup()¶
-
pytest_doctest_docutils.get_optionflags(config)¶
Fetch optionflags from pytest configuration.
Extracted from pytest.doctest 8.0 (license: MIT).
-
pytest_doctest_docutils._get_runner(checker=None, verbose=None, optionflags=0, continue_on_failure=True)¶
- Parameters:
- Return type:
-
class pytest_doctest_docutils.DocutilsDocTestRunner¶
Bases:
DocTestRunnerDocTestRunner for doctest_docutils.