{"id":14064889,"url":"https://github.com/pydantic/pytest-examples","last_synced_at":"2025-05-16T04:05:46.139Z","repository":{"id":147241718,"uuid":"616991540","full_name":"pydantic/pytest-examples","owner":"pydantic","description":"Pytest plugin for testing examples in docstrings and markdown files.","archived":false,"fork":false,"pushed_at":"2025-05-06T07:44:59.000Z","size":173,"stargazers_count":122,"open_issues_count":12,"forks_count":16,"subscribers_count":8,"default_branch":"main","last_synced_at":"2025-05-06T08:08:30.637Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"https://pypi.org/project/pytest-examples/","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/pydantic.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-03-21T13:43:52.000Z","updated_at":"2025-05-06T07:44:21.000Z","dependencies_parsed_at":null,"dependency_job_id":"3e1b9da6-b6fd-4065-a9ce-97271db77b19","html_url":"https://github.com/pydantic/pytest-examples","commit_stats":null,"previous_names":[],"tags_count":16,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pydantic%2Fpytest-examples","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pydantic%2Fpytest-examples/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pydantic%2Fpytest-examples/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pydantic%2Fpytest-examples/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/pydantic","download_url":"https://codeload.github.com/pydantic/pytest-examples/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254464895,"owners_count":22075570,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-13T07:04:09.507Z","updated_at":"2025-05-16T04:05:41.130Z","avatar_url":"https://github.com/pydantic.png","language":"Python","readme":"# pytest-examples\n\n[![CI](https://github.com/pydantic/pytest-examples/actions/workflows/ci.yml/badge.svg)](https://github.com/pydantic/pytest-examples/actions/workflows/ci.yml?query=event%3Apush+branch%3Amain)\n[![PyPI](https://img.shields.io/pypi/v/pytest-examples.svg)](https://pypi.python.org/pypi/pytest-examples)\n[![versions](https://img.shields.io/pypi/pyversions/pytest-examples.svg)](https://github.com/pydantic/pytest-examples)\n[![license](https://img.shields.io/github/license/pydantic/pytest-examples.svg)](https://github.com/pydantic/pytest-examples/blob/main/LICENSE)\n\nPytest plugin for testing Python code examples in docstrings and markdown files.\n\n`pytest-examples` can:\n* lint code examples using `ruff` and `black`\n* run code examples\n* run code examples and check print statements are inlined correctly in the code\n\nIt can also update code examples in place to format them and insert or update print statements.\n\n## Installation\n\n```bash\npip install -U pytest-examples\n```\n\n## Usage\n\n### Basic usage\n\nHere's an example basic usage - lint then run examples in the `foo_dir` directory and the `bar_file.py` file.\n\n```py\nimport pytest\nfrom pytest_examples import find_examples, CodeExample, EvalExample\n\n\n@pytest.mark.parametrize('example', find_examples('foo_dir', 'bar_file.py'), ids=str)\ndef test_docstrings(example: CodeExample, eval_example: EvalExample):\n    eval_example.lint(example)\n    eval_example.run(example)\n```\n\n### Check print statements\n\n`pytest-examples` can also check print statements are inserted correctly.\n\nThere's the expected format of prints statemints in docstrings:\n\n```py\ndef add_two_things(a, b):\n    \"\"\"\n    ```py\n    from my_lib import add_two_things\n\n    print(add_two_things(1, 2))\n    #\u003e 3\n    ```\n    \"\"\"\n    return a + b\n```\n\nAnd here's an example of a markdown file, again documenting `add_two_things`:\n\n````markdown\n# How `add_two_things` works\n\n```py\nfrom my_lib import add_two_things\n\nprint(add_two_things(1, 2))\n#\u003e 3\n```\n````\n\n`pytest-examples` can then run the code and check the print statements are correct:\n\n```py\nimport pytest\nfrom pytest_examples import find_examples, CodeExample, EvalExample\n\n\n@pytest.mark.parametrize('example', find_examples('foo_dir'), ids=str)\ndef test_docstrings(example: CodeExample, eval_example: EvalExample):\n    eval_example.run_print_check(example)\n```\n\n### Updating files\n\nAs well as checking linting and print statements, are correct, we can also update files.\n\nThis requires the `--update-examples` flags **AND** use of the `format()` and `run_print_update()` methods.\n\nHere's a full example of a unit test that checks code when called normally, but can update it\nwhen the flag is set:\n\n```py\nimport pytest\nfrom pytest_examples import find_examples, CodeExample, EvalExample\n\n\n@pytest.mark.parametrize('example', find_examples('README.md'), ids=str)\ndef test_readme(example: CodeExample, eval_example: EvalExample):\n    if eval_example.update_examples:\n        eval_example.format(example)\n        eval_example.run_print_update(example)\n    else:\n        eval_example.lint(example)\n        eval_example.run_print_check(example)\n```\n","funding_links":[],"categories":["Python"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpydantic%2Fpytest-examples","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpydantic%2Fpytest-examples","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpydantic%2Fpytest-examples/lists"}