pytest-examples
pytest-examples is a pytest plugin designed for testing Python code examples found within docstrings and markdown files. It can lint these code examples using tools like ruff and black, run them to verify their correctness, and even check if print statements are inlined as expected. The library is currently at version 0.0.18 and follows an active development and release cadence, often aligning with its upstream `pytest` and `pydantic` projects.
Warnings
- gotcha pytest-examples's features (running, linting, updating examples) are enabled via command-line flags (e.g., `--py-examples`, `--update-examples`). Simply installing the plugin does not automatically activate these checks; you must explicitly include the relevant flags when running `pytest`.
- gotcha When using the `--update-examples` flag, `pytest-examples` can modify your source files (e.g., to format code or insert/update print statement outputs). It's crucial to understand this behavior to avoid unintended changes, especially in CI/CD environments or when working with version control.
- gotcha pytest-examples relies on `pytest`'s test discovery mechanism. If your docstrings or markdown files are not discovered, or if the code within them cannot be correctly parsed or executed by Python, `pytest-examples` might not report issues as expected or might encounter its own errors during collection.
Install
-
pip install pytest-examples
Imports
- pytest
import pytest
Quickstart
import pytest
# my_lib.py (example code to be tested)
def add_two_things(a, b):
"""
Adds two things.
>>> add_two_things(1, 2)
3
"""
return a + b
# test_my_lib.py (pytest test file)
def test_add_two_things_example():
# This test primarily ensures pytest-examples is installed and discoverable.
# The actual testing of examples is done by running `pytest`.
pass
# To run tests and check examples:
# 1. Save the above code into 'my_lib.py' and 'test_my_lib.py'
# 2. Run from your terminal in the same directory: pytest --py-examples
# 3. To lint and run, and optionally update: pytest --py-examples --update-examples