Skip to main content

Pytest Markdown Docs

A plugin for pytest that uses markdown code snippets from markdown files and docstrings as tests.

Detects Python code fences (triple backtick escaped blocks) in markdown files as well as inline Python docstrings (similar to doctests) and runs them as tests.

Python file example:

# mymodule.py
class Foo:
    def bar(self):
        """Bar the foo
        
        This is a sample docstring for the bar method 

        Usage:
        ```python
        import mymodule
        result = mymodule.Foo().bar()
        assert result == "hello"
        ```
        """
        return "hello"

Markdown file examples:

# Title

Lorem ipsum yada yada yada

```python
import mymodule
result = mymodule.Foo().bar()
assert result == "hello"
```

Usage

First, make sure to install the plugin, e.g. pip install pytest-markdown-docs

To enable markdown python tests, pass the --markdown-docs flag to pytest:

pytest --markdown-docs

You can also use the markdown-docs flag to filter only markdown-docs tests:

pytest --markdown-docs -m markdown-docs

Detection conditions

Fence blocks (```) starting with the python, python3 or py language definitions are detected as tests in:

  • Python (.py) files, within docstrings of classes and functions
  • .md, .mdx and .svx files

Skipping tests

To exclude a Python code fence from testing, add a notest info string to the code fence, e.g:

```python notest
print("this will not be run")
```

Code block dependencies

Sometimes you might wish to run code blocks that depend on entities to already be declared in the scope of the code, without explicitly declaring them. There are currently two ways you can do this with pytest-markdown:

Injecting global/local variables

If you have some common imports or other common variables that you want to make use of in snippets, you can add them by creating a pytest_markdown_docs_globals hook in your conftest.py:

def pytest_markdown_docs_globals():
    import math
    return {"math": math, "myvar": "hello"}

With this conftest, you would be able to run the following markdown snippet as a test, without causing an error:

```python
print(myvar, math.pi)
```

Depending on previous snippets

If you have multiple snippets following each other and want to keep the side effects from the previous snippets, you can do so by adding the continuation info string to your code fence:

```python
a = "hello"
```

```python continuation
assert a + " world" == "hello world"
```

Testing of this plugin

You can test this module itself (sadly not using markdown tests at the moment) using pytest:

> poetry run pytest

Or for fun, you can use this plugin to include testing of the validity of snippets in this README.md file:

> poetry run pytest --markdown-docs

Known issues

  • Only tested with pytest 6.2.5. There seem to be some minor issue with pytest >7 due to changes of some internal functions in pytest, but that should be relatively easy to fix. Contributions are welcome :)
  • Code for docstring-inlined test discovery can probably be done better (similar to how doctest does it). Currently, seems to sometimes traverse into Python's standard library which isn't great...

Metadata

Release files for pytest-markdown-docs 0.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pytest-markdown-docs 0.1.1
File Size Uploaded
pytest-markdown-docs-0.1.1.tar.gz 5.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pytest-markdown-docs 0.1.1
File Interpreter ABI Platform
pytest_markdown_docs-0.1.1-py3-none-any.whl Python 3 none any Details

Total release size: 11.6 kB

Release files / pytest-markdown-docs-0.1.1.tar.gz

Download URL pytest-markdown-docs-0.1.1.tar.gz
Size 5.2 kB
Tags Source
SHA-256 checksum
How to use checksums
b7e01c545a3adeb0062a24c66b1d2a2894e7ff555fa59b6b19bc04ab9671aaae
BLAKE2b-256 checksum
How to use checksums
40f97a8d7469400f2de5aa1e2a7ebc133aa9f017a06ee37ae4879b5099d76899
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.12 CPython/3.8.10 Linux/5.13.0-39-generic

Release files / pytest_markdown_docs-0.1.1-py3-none-any.whl

Download URL pytest_markdown_docs-0.1.1-py3-none-any.whl
Size 6.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
d10bf475a059f554efdde5cde7e6c433d11003cb46cb5c995f7b786bcfa18e66
BLAKE2b-256 checksum
How to use checksums
8f94313e094b0e046e30898b71143679e72b4e6359be2e452c31208d21a22c22
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.12 CPython/3.8.10 Linux/5.13.0-39-generic

Release history Release notifications | RSS feed

0.9.2

2 release files

0.9.1

2 release files

0.9.0

2 release files

0.8.0

2 release files

0.7.1

2 release files

0.7.0

2 release files

0.6.0

2 release files

0.5.1

2 release files

0.5.0

2 release files

0.4.3

2 release files

0.4.1

2 release files

0.4.0

2 release files

0.3.0

2 release files

0.2.0

2 release files

This release

0.1.1 This release

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page