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.15.tar.gz (20.8 kB view details)

Uploaded Source

Built Distribution

pytest_examples-0.0.15-py3-none-any.whl (17.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pytest_examples-0.0.15.tar.gz
  • Upload date:
  • Size: 20.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for pytest_examples-0.0.15.tar.gz
Algorithm Hash digest
SHA256 2d6ced2d1f0d59863f81a4d2f193737464b8004a7670907c3bedef6306a5d660
MD5 fa1698e9ea1d7349d29360c2dfe02047
BLAKE2b-256 c382fa4fb866d52934de97b90c84c89896faebfb984436804fc3baa55d48d511

See more details on using hashes here.

Provenance

The following attestation bundles were made for pytest_examples-0.0.15.tar.gz:

Publisher: ci.yml on pydantic/pytest-examples

Attestations:

File details

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

File metadata

File hashes

Hashes for pytest_examples-0.0.15-py3-none-any.whl
Algorithm Hash digest
SHA256 6e4adc522bf2e3f93cae3b37a4add76fcc2c1ada29d8988b2ea15b236233ec0f
MD5 52d1d4fd6650b5fc7faa745e901abb8f
BLAKE2b-256 ba813f727a7d2f9c1ff36f581453949a4af314629108642d5140298476e90902

See more details on using hashes here.

Provenance

The following attestation bundles were made for pytest_examples-0.0.15-py3-none-any.whl:

Publisher: ci.yml on pydantic/pytest-examples

Attestations:

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