A pytest extension to test console code blocks in markdown files.
Project description
A pytest extension to test console code blocks in Markdown files
A minimal pytest extension to test console blocks in your Markdown documentation. Works on Linux, macOS, and Windows.
Installation
In your project, run:
pip install pytest-markdown-console
If you are using uv, install it as a development dependency:
uv add --dev pytest-markdown-console
The plugin self-registers and will run in each subsequent pytest invocation.
How does it work?
Each console code block in Markdown files collected by the plugin generates a test case.
The test runs each command in the block and compares its actual output to the expected output written in the block.
Take this Markdown file:
# README.md
Here is an example of running my app:
```console
$ uv run myapp.py
Hello, world!
```
Running pytest with this extension creates a test case that passes if the app actually prints "Hello, world!".
Match partial output
Use ... anywhere in the expected output to match any text in its place, including multiple lines:
```console
$ python -c "print(object())"
<object object at 0x...>
```
This is useful when part of the output is non-deterministic, such as memory addresses or timestamps, or when the output is very long.
Signal expected failures
Sometimes you want to illustrate expected failures. To convey this to both the reader and the plugin, place a # Error: comment at the end of the failing command, or on the line preceding it:
```console
$ uv run myapp.py --bad_option # Error: this will fail
...
$ # Error: this will also most definitely fail
$ uv run myapp.py --still_bad
...
```
To pass the test case, the command line must fail and the error message produced by the application, if any, must match.
Filter by platform
To restrict a test to specific platforms, use the platform: directive in an HTML comment immediately before the fence:
<!-- pytest-markdown-console: platform:linux,macos -->
```console
$ echo "This will generate a test case on Linux and macOS"
```
To exclude a platform, prefix its name with !:
<!-- pytest-markdown-console: platform:!windows -->
```console
$ echo "This will not generate a test case on Windows"
```
Change the working directory
By default, all commands run in the same directory as the Markdown file. To use a different directory, use the cwd: directive:
<!-- pytest-markdown-console: cwd:../ -->
```console
$ uv run myapp.py
...
```
Relative paths are resolved relative to the Markdown file's location.
Exclude a block from testing
To exclude a block from being collected as a test at all, use the notest directive:
<!-- pytest-markdown-console: notest -->
```console
$ echo "This block will not be tested"
```
Controlling test case runs globally
By default, test cases generated by this plugin are run whenever pytest is invoked. To exclude them entirely:
pytest -p no:markdown-console
To run only the plugin's tests:
pytest -m markdown_console
Customising the directive tag
By default, directive comments use the pytest-markdown-console tag:
<!-- pytest-markdown-console: notest -->
```console
$ echo "This block will not be tested"
```
You can change this tag via pyproject.toml, for example to keep your Markdown source shorter:
[tool.pytest.ini_options]
markdown_console_directive = "console-test"
With the above setting you would write <!-- console-test: notest --> instead.
Why this extension?
This plugin makes your documentation testable — specifically console blocks — within the same pytest suite you use for the rest of your Python code.
Other tools can test console blocks in Markdown files, but we couldn't find one that is simple, supports Windows, integrates with pytest, and requires no boilerplate.
Does it make sense to test console blocks?
Testing console blocks is admittedly niche. They often contain installation instructions or shell-specific commands that don't translate across platforms.
However, if you are building a CLI app, you likely already showcase commands and their output in your docs. Testing those snippets ensures they stay up-to-date.
Launching a Python app on Windows, Linux, or macOS is the same one-liner when using uv. That is the sweet spot motivating this small plugin.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pytest_markdown_console-0.0.1.dev2.tar.gz.
File metadata
- Download URL: pytest_markdown_console-0.0.1.dev2.tar.gz
- Upload date:
- Size: 7.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
83585552393bd778a2ecef09d6c3fe46ac2d6401ab32a4e1be743df9bd0f8d6a
|
|
| MD5 |
071841430f143ed5839b8d5827f104d8
|
|
| BLAKE2b-256 |
4b525bf8d5d5496e37e63101b9d898b71b60dee3ea06289575fcade154c700f5
|
Provenance
The following attestation bundles were made for pytest_markdown_console-0.0.1.dev2.tar.gz:
Publisher:
publish.yml on confarg/pytest-markdown-console
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pytest_markdown_console-0.0.1.dev2.tar.gz -
Subject digest:
83585552393bd778a2ecef09d6c3fe46ac2d6401ab32a4e1be743df9bd0f8d6a - Sigstore transparency entry: 1683361400
- Sigstore integration time:
-
Permalink:
confarg/pytest-markdown-console@5dd3046bb4397ce1a36d0aa3494156f0c2707d21 -
Branch / Tag:
refs/tags/v0.0.1.dev2 - Owner: https://github.com/confarg
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@5dd3046bb4397ce1a36d0aa3494156f0c2707d21 -
Trigger Event:
release
-
Statement type:
File details
Details for the file pytest_markdown_console-0.0.1.dev2-py3-none-any.whl.
File metadata
- Download URL: pytest_markdown_console-0.0.1.dev2-py3-none-any.whl
- Upload date:
- Size: 10.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2e356e5e0988f4b4efdaf8150b2c0ae1be864113573a8b8a7e9338370dcffae0
|
|
| MD5 |
a24e1ea4403c0845e2df53edaf91085d
|
|
| BLAKE2b-256 |
58da8fa6c7f711b8520cf02082585517b8fa68f64d93f6abb73255a80731ea00
|
Provenance
The following attestation bundles were made for pytest_markdown_console-0.0.1.dev2-py3-none-any.whl:
Publisher:
publish.yml on confarg/pytest-markdown-console
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pytest_markdown_console-0.0.1.dev2-py3-none-any.whl -
Subject digest:
2e356e5e0988f4b4efdaf8150b2c0ae1be864113573a8b8a7e9338370dcffae0 - Sigstore transparency entry: 1683361522
- Sigstore integration time:
-
Permalink:
confarg/pytest-markdown-console@5dd3046bb4397ce1a36d0aa3494156f0c2707d21 -
Branch / Tag:
refs/tags/v0.0.1.dev2 - Owner: https://github.com/confarg
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@5dd3046bb4397ce1a36d0aa3494156f0c2707d21 -
Trigger Event:
release
-
Statement type: