# pytest — card for AI agents

Simple, powerful Python testing framework with fixtures and plugins.

Category: Testing · PyPI package `pytest` · latest version 9.2.0 (checked 2026-11-01)
Install: `pip install pytest`
License: MIT (permissive)

In your own product (generator, app, hosted service): **yes**. Permissive license: ship it inside your own product, closed or open source, and keep its license and copyright notice.

## Numbers (AgentGid data)
- Gid Score: 80.8 (#85 of all tools, 6 of 122 in its category)
- Used in 257 public projects set up for AI coding agents (4.1%)
- Downloads last week: 238,731,176 (as of 2026-11-24), +14.2% over 4 weeks
- GitHub stars: 14,742 (as of 2026-11-24)
- Last push: 2026-11-24

## Often used with
python-dotenv 55.6%, Pydantic 51.4%, Uvicorn 49.8%, FastAPI 46.7%, Requests 42.8%, NumPy 40.5%

## Links
- AgentGid page: https://agentgid.com/tools/pytest/
- Homepage: https://docs.pytest.org/en/latest/
- Repository: https://github.com/pytest-dev/pytest


## pytest: automatic checks for your Python code

pytest is a free tool that runs small checks, called tests, on your Python code and tells you which ones fail and why. Your AI assistant writes the tests next to the code, and one command shows if a change broke something. It works for a single script as well as a large app.

### Where to use it
- Check prices before each release: Your shop or booking app works out totals and discounts; one test table runs every coupon case, and a wrong number stops the release.
- Test file exports without mess: Code that writes receipts, reports or uploads gets a fresh empty folder for each test, so no test leaves files in your project.
- Test settings without real services: Point the app at a local address or a fake payment service for one test only; pytest puts the real setting back afterwards.
- Check that bad input is refused: Make sure a sign-up form or an import rejects wrong data with a clear error instead of saving it.
- Run old unittest suites as they are: An older project with tests in the built-in unittest style can switch to pytest without rewriting them first.
- Let the assistant check its own edits: After each change your AI assistant runs the tests and reads the failure report, which shows the values that did not match, before it says the work is done. (idea, not from the official pages)

### How to start
- Install pytest into your project's virtual environment, not into the system Python.

```
python3 -m venv .venv
source .venv/bin/activate   # on Windows: .venv\Scripts\activate
pip install -U pytest
```

- Write a file whose name starts with test_ and put plain assert lines in functions whose names start with test_.

```
# content of test_sample.py
def func(x):
    return x + 1


def test_answer():
    assert func(3) == 5
```

- Run pytest in the project folder; it finds the test files and prints a report for each failure.

```
$ pytest
```


### License
Free for any use, including paid and closed products, under the MIT license. Keep the license notice if you ship pytest's own files.

### Good to know
- The current version needs Python 3.10 or newer.
- Tests written with async def do not run on their own; you add a helper plugin such as pytest-asyncio or anyio.
- pytest only picks up files named test_*.py or *_test.py, and classes whose names start with Test; other names are skipped without a warning.
- Its result code tells a build server what happened, for example 0 when all tests passed and 5 when it found no tests at all.
- Major versions remove old behaviour; 9.0 dropped Python 3.9 and 9.2 made some parametrize options name-only.

Checked 2026-11-11 against: https://github.com/pytest-dev/pytest/blob/main/README.rst, https://github.com/pytest-dev/pytest/blob/main/LICENSE, https://pypi.org/pypi/pytest/json, https://docs.pytest.org/en/stable/, https://docs.pytest.org/en/stable/getting-started.html, https://docs.pytest.org/en/stable/how-to/fixtures.html, https://docs.pytest.org/en/stable/how-to/parametrize.html, https://docs.pytest.org/en/stable/how-to/tmp_path.html, https://docs.pytest.org/en/stable/how-to/monkeypatch.html, https://docs.pytest.org/en/stable/how-to/assert.html, https://docs.pytest.org/en/stable/reference/exit-codes.html, https://docs.pytest.org/en/stable/explanation/goodpractices.html, https://docs.pytest.org/en/stable/deprecations.html, https://docs.pytest.org/en/stable/changelog.html, https://docs.pytest.org/en/stable/backwards-compatibility.html

### Mistakes to avoid
- Don't: async def test_fetch(): ... with plain pytest → Do: Install an async plugin (pytest-asyncio or anyio) and mark the test as that plugin's docs say (In our run with pytest 9.2.0 the test failed with "async def functions are not natively supported" and a list of plugins.)
- Don't: @pytest.mark.parametrize("a", [1, 2], True, ["one", "two"])  # indirect and ids passed by position → Do: @pytest.mark.parametrize("a", [1, 2], indirect=True, ids=["one", "two"]) (Since pytest 9.2.0 indirect, ids and scope are keyword-only and positional use raises a TypeError.)
- Don't: Tests in tests/ fail with ModuleNotFoundError for the app's own module when the code lives in src/ → Do: Install the project in editable mode, or set pythonpath = ["src"] in the pytest config (Without an editable install pytest does not see the src folder; our demo used pythonpath = ["."] for a flat folder.)
- Don't: Adding [tool.pytest] to pyproject.toml while [tool.pytest.ini_options] is still there → Do: Keep one of the two tables; the native [tool.pytest] table needs pytest 9.0 or newer (pytest 9.0 added the native TOML table but does not accept both tables at once.)
- Don't: A test class named CheckCart or a file named cart_tests.py → Do: Name files test_*.py or *_test.py and classes Test*, with test_* functions inside (Other names are not collected, so the tests silently never run.)

### Questions
- Do I need special check functions like assertEqual? No. You write plain Python assert lines, and pytest shows the values on both sides when one fails.
- Can I use it in a commercial project? Yes. It is under the MIT license, which allows commercial use.
- How do tests get shared setup like a sample cart or a database? Through fixtures. A test names a fixture as an argument and pytest passes in what the fixture returns.
- Will my old unittest tests run? Yes. pytest runs unittest suites without changes.
- Is it still maintained? Yes. Version 9.2.0 came out on 27 October 2026.

---
AgentGid's catalog is curated: every entry was checked against the maker's official pages (registry, repository, license and pricing pages) on the date shown; a license value shows the quote it stands on wherever AgentGid has one. Usage numbers come from AgentGid's own scan of public repositories set up for AI coding agents. Claims by makers are reported, not tested by AgentGid.
