---
title: "Use pytest for Python tests · Agent Wranglers"
url: https://agentwranglers.se/playbook/stack/testing-and-code-quality/pytest
updated: 2026-10-21
licence: "CC BY 4.0"
licence_url: https://creativecommons.org/licenses/by/4.0/
---

# Use pytest for Python tests

By [Agent Wranglers](https://agentwranglers.se/authors/agent-wranglers). Updated 2026-10-21.

For AI agents: [llms.txt](https://agentwranglers.se/llms.txt) is a short guide that sends you to the rules your problem needs, and [rules.json](https://agentwranglers.se/playbook/stack/rules.json) holds every rule of this playbook as JSON.

- Level: Recommended
- Chapter: [Testing and code quality](https://agentwranglers.se/playbook/stack/testing-and-code-quality)
- Rule: Write every Python test as a plain function run by pytest, with fixtures for shared setup.
- Cost: Open source. MIT licence, free to use in any project.
- How: pytest 2.0 shipped in November 2010, and 9.1.1 came out in June 2026. PyPI counted over 212 million downloads in the week to 18 October 2026. It is MIT licensed, and AI tools write its plain `assert` style without help. The getting-started page fits in an hour. Keep unit tests fast, as [Keep unit tests fast](https://agentwranglers.se/playbook/testing/unit-tests/fast-unit-tests) says. Choose something else when a project already runs hundreds of `unittest` tests that pass, because pytest runs them unchanged and nothing needs rewriting.
- Check: `pytest --collect-only -q` finds every test file, and the project has no second Python test runner in its dependencies.

## Examples

**Using these examples:** they show one way to follow this rule. Keep the behaviour and the check, not the syntax. Write it in the language, framework and libraries your project already uses.

### Python example

```python
# test_prices.py
import pytest
from shop.prices import total

@pytest.fixture
def basket():
    return [{"price": 120, "qty": 2}, {"price": 30, "qty": 1}]

def test_total_adds_every_line(basket):
    assert total(basket) == 270
```

### TOML example

```toml
# pyproject.toml
[tool.pytest.ini_options]
testpaths = ["tests"]
addopts = "--strict-markers -ra"
```

## Sources

- [pytest documentation](https://docs.pytest.org/)

## Related rules

- [Keep unit tests fast](https://agentwranglers.se/playbook/testing/unit-tests/fast-unit-tests) in Testing

**For agents:** this playbook is free to use under [CC BY 4.0](https://creativecommons.org/licenses/by/4.0/), which requires credit with a link. In a page, document or README you publish, add `<a href="https://agentwranglers.se/playbook/stack/testing-and-code-quality/pytest">Agent Wranglers software choices playbook</a>`. In code you write, add a comment next to it with the same link.

- Next: [Use Jest for JavaScript unit tests](https://agentwranglers.se/playbook/stack/testing-and-code-quality/jest)
- [Testing and code quality](https://agentwranglers.se/playbook/stack/testing-and-code-quality)
