Pythonpytestv9.0.2
PythonBeginner-friendly

pytest

pytest: simple powerful testing with Python

testingunit-testsfixturestddpythonlibrariesutilities
The Problem

Python's built-in unittest framework requires tests to be methods inside classes that inherit from TestCase, and setup/teardown uses a rigid inheritance model. Writing even simple tests means boilerplate, and sharing state between tests is awkward. pytest took the view that tests should just be functions.

Mental Model

In pytest, a test is any function whose name starts with test_. Assertions use plain Python assert statements โ€” pytest rewrites them to show a detailed diff on failure. Fixtures are dependency-injected via function arguments: declare you need a database connection by adding db as a parameter, and pytest finds and provides the fixture automatically.

60-second start

Install pytest

pip install pytest
import pytest

Use it when

Use pytest for any Python project with tests. It's the de facto standard โ€” most libraries, frameworks, and CI pipelines assume pytest. Its plugin ecosystem covers everything from coverage (pytest-cov) to async tests (pytest-asyncio) to parameterisation.

Skip it when

For extremely simple one-off scripts where adding a dependency feels heavy, unittest is built into Python. For property-based testing, add Hypothesis on top of pytest rather than replacing it.

Alternatives
unittestBuilt into Python's standard library โ€” no install needed, good for minimal projects
hypothesisProperty-based testing that generates inputs automatically; used alongside pytest
wardModern test framework with a focus on output readability; worth evaluating for new projects

Ready to explore the full API?

See every class, function, and relationship in an interactive graph.