pytest
pytest: simple powerful testing with Python
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.
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.
Install pytest
pip install pytest
import pytestUse 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.
Ready to explore the full API?
See every class, function, and relationship in an interactive graph.