Skip to main content

Run pytest --doctest-modules with markdown docstrings in code blocks (```)

Project description

pytest-doctest-mkdocstrings

A pytest plugin that allows you to use doctest and enclose your example code blocks in ``` codeblocks (e.g. when building documentation with mkdocs and mkdocstring).

v0.1.0 - Breaking change - you now need to specifically add the command line option --doctest-mdcodeblocks when calling pytest

Usually this docstring would fail doctest:

    """
    A function description

    Examples:
    --------
    ```
    >>> x = 1
    >>> x
    1
    ```
    """

As doctest looks for the output

Expected:
    1
    ```
Got:
    1

This plugin works by mockeypatching doctests parser when pytest begins each test session and including ``` as an identifier of the end of an expected result (a want).

It only works when doctest is invoked via pytest, not when invoking doctest directly.

If you have code examples or expected results which actually contain ``` then you'll need to re-write them.

Usage

TL;DR: invoke pytest with --doctest-mdcodeblocks

  1. Strongly recommended to set up a virtual environment first! (e.g.. python3 -m venv .venv, . .venv/bin/activate)
  2. Install with pip install pytest-doctest-mkdocstrings
  3. Run pytest: pytest --doctest-mdcodeblocks --doctest-modules --doctest-glob="*.md"

You can also add the following to your pyproject.toml so that pytest always runs doctests:

[tool.pytest.ini_options]
addopts = [
    "--doctest-modules",
    "--doctest-glob='*.md'",
    "--doctest-mdcodeblocks"
]

To temporarily override the setting use: pytest --no-doctest-mdcodeblocks

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pytest-doctest-mkdocstrings-0.1.1.tar.gz (5.6 kB view details)

Uploaded Source

Built Distribution

File details

Details for the file pytest-doctest-mkdocstrings-0.1.1.tar.gz.

File metadata

File hashes

Hashes for pytest-doctest-mkdocstrings-0.1.1.tar.gz
Algorithm Hash digest
SHA256 a3b361e016917a79e268dc4e120a50e216e7f67527d0a53cb454392a077f003a
MD5 aa349e8a9c088e13a41404161f8b16a6
BLAKE2b-256 86863f430d371c07ac7f1a391a853d9d675a4422af2337f59ac2431331749252

See more details on using hashes here.

File details

Details for the file pytest_doctest_mkdocstrings-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for pytest_doctest_mkdocstrings-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 2299264438723ad1b287a0ad6c2e0b30a99e80711381eb6e97284810237b71b2
MD5 0d4786110b2c07d5fd9cc7218c1cd00c
BLAKE2b-256 028e7cbab493901a48c0dc8e45e55f126c3bb100693c65c234ebcb9085d55075

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page