Skip to main content

Pytest plugin for testing examples in docstrings and markdown files.

Project description

pytest-examples

CI pypi versions license

Pytest plugin for testing Python code examples in docstrings and markdown files.

pytest-examples can:

  • lint code examples using ruff and black
  • run code examples
  • run code examples and check print statements are inlined correctly in the code

It can also update code examples in place to format them and insert or update print statements.

Installation

pip install -U pytest-examples

Usage

Basic usage

Here's an example basic usage - lint then run examples in the foo_dir directory and the bar_file.py file.

import pytest
from pytest_examples import find_examples, CodeExample, EvalExample


@pytest.mark.parametrize('example', find_examples('foo_dir', 'bar_file.py'), ids=str)
def test_docstrings(example: CodeExample, eval_example: EvalExample):
    eval_example.lint(example)
    eval_example.run(example)

Check print statements

pytest-examples can also check print statements are inserted correctly.

There's the expected format of prints statemints in docstrings:

def add_two_things(a, b):
    """
    ```py
    from my_lib import add_two_things

    print(add_two_things(1, 2))
    #> 3
    ```
    """
    return a + b

And here's an example of a markdown file, again documenting add_two_things:

# How `add_two_things` works

```py
from my_lib import add_two_things

print(add_two_things(1, 2))
#> 3
```

pytest-examples can then run the code and check the print statements are correct:

import pytest
from pytest_examples import find_examples, CodeExample, EvalExample


@pytest.mark.parametrize('example', find_examples('foo_dir'), ids=str)
def test_docstrings(example: CodeExample, eval_example: EvalExample):
    eval_example.run_print_check(example)

Updating files

As well as checking linting and print statements, are correct, we can also update files.

This requires the --update-examples flags AND use of the format() and run_print_update() methods.

Here's a full example of a unit test that checks code when called normally, but can update it when the flag is set:

import pytest
from pytest_examples import find_examples, CodeExample, EvalExample


@pytest.mark.parametrize('example', find_examples('README.md'), ids=str)
def test_readme(example: CodeExample, eval_example: EvalExample):
    if eval_example.update_examples:
        eval_example.format(example)
        eval_example.run_print_update(example)
    else:
        eval_example.lint(example)
        eval_example.run_print_check(example)

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_examples-0.0.10.tar.gz (14.9 kB view details)

Uploaded Source

Built Distribution

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

pytest_examples-0.0.10-py3-none-any.whl (17.2 kB view details)

Uploaded Python 3

File details

Details for the file pytest_examples-0.0.10.tar.gz.

File metadata

  • Download URL: pytest_examples-0.0.10.tar.gz
  • Upload date:
  • Size: 14.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.12

File hashes

Hashes for pytest_examples-0.0.10.tar.gz
Algorithm Hash digest
SHA256 5d34d22e689aca2bbad8dd6b7cdcc9d0107e2942853b3154f3a3c68d145d91c5
MD5 ded118f6ff01e594c0f4863a9834e89b
BLAKE2b-256 0a4b1aa9afeed3029ea53591c4dcd0a5104512487415e5d62d2797882d880e1e

See more details on using hashes here.

File details

Details for the file pytest_examples-0.0.10-py3-none-any.whl.

File metadata

File hashes

Hashes for pytest_examples-0.0.10-py3-none-any.whl
Algorithm Hash digest
SHA256 3d0b52424e454846beed8621a12b85db88c6c17049f65c2f417211372c20dc9e
MD5 2b4dc7d48d9f4e7b8e3994f23e1b7cd4
BLAKE2b-256 85744c09d847e082f4f779800c2a200336419804f444298b26045d3bd25ce152

See more details on using hashes here.

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