Skip to main content

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.

For example:

```console
$ uv run myapp.py
Hello, world!
```

This creates a passing test case if your app actually prints "Hello, world!".

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
```

Filter by platform

To restrict a test to specific platforms, use the platform: directive:

```console platform:linux,macos
$ echo "This will generate a test case on Linux and macOS"
```

To exclude a platform, prefix its name with !:

```console platform:!windows
$ 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:

```console cwd:../
$ uv run myapp.py
...
```

Relative paths are resolved relative to the Markdown file's location.

Controlling which tests run

By default, all tests run including those generated by this plugin. To exclude them:

pytest -p no:markdown-console

To run only the plugin's tests:

pytest -m markdown_console

Why this extension?

This plugin makes your documentation testable — specifically console blocks — within the same pytest suite you use for the rest of your code.

It is similar to pytest-markdown-docs, which works on Python code blocks, and follows similar conventions.

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


Download files

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

Source Distribution

pytest_markdown_console-0.0.1.dev1.tar.gz (6.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pytest_markdown_console-0.0.1.dev1-py3-none-any.whl (9.2 kB view details)

Uploaded Python 3

File details

Details for the file pytest_markdown_console-0.0.1.dev1.tar.gz.

File metadata

File hashes

Hashes for pytest_markdown_console-0.0.1.dev1.tar.gz
Algorithm Hash digest
SHA256 a665d91e05a5f142739b8faf8434bb75442d937e1f8764df2bd6bed09e1bbdb3
MD5 a71f41a6510ff5dddb8e077c426e61ba
BLAKE2b-256 83832fc192fea2f2b7fe83d6d427beccf7c43cbbef69cbe129cc99314545368d

See more details on using hashes here.

Provenance

The following attestation bundles were made for pytest_markdown_console-0.0.1.dev1.tar.gz:

Publisher: publish.yml on confarg/pytest-markdown-console

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pytest_markdown_console-0.0.1.dev1-py3-none-any.whl.

File metadata

File hashes

Hashes for pytest_markdown_console-0.0.1.dev1-py3-none-any.whl
Algorithm Hash digest
SHA256 dec1decaabcee3092b13c33a942ac54b503ccde8b556c1708c227290b7c83be4
MD5 76243ed2b522919e483e0df5175c4132
BLAKE2b-256 700a512d09bb7eaa321fae8b0dfd04ce14ccf59019b446e1b91f726f8a1c5057

See more details on using hashes here.

Provenance

The following attestation bundles were made for pytest_markdown_console-0.0.1.dev1-py3-none-any.whl:

Publisher: publish.yml on confarg/pytest-markdown-console

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

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