pytest-doctestplus
pytest-doctestplus is a pytest plugin that extends the standard Python doctest functionality with advanced features for testing example code in Python docstrings, reStructuredText (.rst), Markdown (.md), and TeX (.tex) files. It is currently at version 1.7.1 and maintains a consistent release cadence with new features, bug fixes, and compatibility updates for Python and pytest versions.
Warnings
- breaking Python 3.9 and older versions (3.8, 3.7) are no longer supported. Ensure your environment uses Python 3.10 or newer.
- breaking Compatibility with older pytest versions has been dropped. Specifically, pytest < 7 is no longer supported, and recent versions require pytest >= 8.
- gotcha Using pytest's built-in `--doctest-modules` option will override pytest-doctestplus's functionality, even if `doctest_plus = enabled` is configured. Always use `--doctest-plus` to activate this plugin's features.
- gotcha The `__doctest_requires__` module-level variable can cause unexpected test skipping if dependencies are not met or if the wildcard patterns are incorrectly defined. Ensure correct module availability and pattern matching.
- gotcha Doctest directives with content that should not have any (e.g., `.. doctest-skip::` without further indented content) previously caused crashing Sphinx builds. While fixed in 1.6.0, using such directives incorrectly will now result in an error.
Install
-
pip install pytest-doctestplus
Quickstart
import pytest
# my_module.py
def greet(name):
"""
Greets the given name.
>>> greet('World')
'Hello, World!'
>>> greet('pytest-doctestplus') # doctest: +SKIP
'Hello, pytest-doctestplus!'
"""
return f'Hello, {name}!'
# To run the doctests from the command line, save the above as my_module.py
# and run pytest:
# pytest --doctest-plus my_module.py