Skip to main content

Pytest plugin for runs tests directly from Markdown files

Project description

markdown-pytest

The markdown-pytest plugin is a pytest plugin that allows you to run tests directly from Markdown files.

With this plugin, you can write your tests inside Markdown files, making it easy to read, understand and maintain your documentation samples. The tests are executed just like any other Pytest tests.

Sample of markdown file content:

<!-- name: test_assert_true -->
```python
assert True
```
Will be shown as
assert True

Restrictions

Since there is no way to add attributes to a block of code in markdown, this module only runs those tests that are marked with a special comment.

The general format of this comment is as follows: parts separated by semicolons are a colon separated key-value pairs, the last semicolon is optional, and parts not containing a colon bill be ignored.

Example:

<!-- key1: value1; key2: value2 -->

Multiline example:

<!-- 
    key1: value1; 
    key2: value2;
-->

This comment should be placed right before the block of code, exactly upper the backticks, for example:

<!-- name: test_name -->
```python
```

The name key is required, and blocks that do not contain it will be ignored.

Some Markdown parsers support two or three dashes around comments, this module supports both variants. The case parameter is optional and might be used for subtests, see "Code split" section.

Common parsing rules

This module uses its own, very simple Markdown parser, which only supports code block parsing. In general, the parsing behavior of a file follows the following rules:

  • Code without <!-- name: test_name --> comment will not be executed.

  • Allowed two or three dashes in the comment symbols

    For example following line will be parsed identically:

    <!--  name: test_name -->
    <!--- name: test_name --->
    <!--  name: test_name --->
    <!--- name: test_name -->
    
  • Code blocks with same names will be merged in one code and executed once

  • The optional comment parameter case will execute the block as a subtest.

  • Indented code blocks will be shifted left.

    For example:

        <!-- name: test_name -->
        ```python
        assert True
        ```
    

    Is the same of:

    <!-- name: test_name -->
    ```python
    assert True
    ```
    

Code split

You can split a test into multiple blocks with the same test name:

Markdown:

This block performs import:

<!-- name: test_example -->
```python
from itertools import chain
```

`chain` usage example:

<!-- name: test_example -->
```python
assert list(chain(range(2), range(2))) == [0, 1, 0, 1]
```
Will be shown as

This block performs import:

from itertools import chain

chain usage example:

assert list(chain(range(2), range(2))) == [0, 1, 0, 1]

subtests support

Of course, you can break tests into subtests by simply adding case: case_name to the markdown comment.

<!-- name: test_counter -->
```python
from collections import Counter
```

<!-- 
    name: test_counter;
    case: initialize_counter
-->
```python
counter = Counter()
```

<!-- 
    name: test_counter;
    case: assign_counter_value
-->
```python
counter["foo"] += 1

assert counter["foo"] == 1
```
Will be shown as
from collections import Counter
counter = Counter()
counter["foo"] += 1

assert counter["foo"] == 1

Fictional Code Examples

Code without <!-- name: test_name --> comment will not be executed.

```python
from universe import antigravity, WrongPlanet

try:
    antigravity()
except WrongPlanet:
    print("You are on the wrong planet.")
    exit(1)
```
Will be shown as
from universe import antigravity, WrongPlanet

try:
    antigravity()
except WrongPlanet:
    print("You are on the wrong planet.")
    exit(1)

Usage example

This README.md file might be tested like this:

$ pytest -v README.md
======================= test session starts =======================
plugins: subtests, markdown-pytest
collected 3 items

README.md::test_assert_true PASSED                           [ 33%]
README.md::test_example PASSED                               [ 66%]
README.md::test_counter SUBPASS                              [100%]
README.md::test_counter SUBPASS                              [100%]
README.md::test_counter PASSED                               [100%]

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

markdown_pytest-0.3.0.tar.gz (9.4 kB view details)

Uploaded Source

Built Distribution

markdown_pytest-0.3.0-py3-none-any.whl (9.3 kB view details)

Uploaded Python 3

File details

Details for the file markdown_pytest-0.3.0.tar.gz.

File metadata

  • Download URL: markdown_pytest-0.3.0.tar.gz
  • Upload date:
  • Size: 9.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.3.1 CPython/3.11.0 Darwin/22.2.0

File hashes

Hashes for markdown_pytest-0.3.0.tar.gz
Algorithm Hash digest
SHA256 f3bfdbfc850e95cb00f357681aa0e8d0ff7c9656fb693f0815fcfa7762d8b798
MD5 d5ca8900cf8a4ae2ec9a2bcc3651f1ea
BLAKE2b-256 75548a7280fd16986ce6ffcd6f2d6440e2d8ec791d1224612db8e02fc4916679

See more details on using hashes here.

File details

Details for the file markdown_pytest-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: markdown_pytest-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 9.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.3.1 CPython/3.11.0 Darwin/22.2.0

File hashes

Hashes for markdown_pytest-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 931d1893e98451de5cdc0b99ec13a16169e563f15b00cd8c1db4b45a4d67d171
MD5 d1b84476a4d71a173bd15ce62d500802
BLAKE2b-256 e1c39cc3f74eddd5b72c06806e0aa6e922b7ca778cb8c92c8b6f8340543cb070

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