PROVIDER Β· the de facto standard of Python testing
pytest
pytest is the Python testing standard where plain asserts and function-based tests lower authoring cost while fixture injection and plugins cover coverage, parallelism, async, and databases as a zero-cost default.
Pros
Cons
Product Information
Detailed Notes
Overview
pytest was started by Holger Krekel in 2004 as Python's dominant test framework, using plain assert statements, functions as tests, and fixtures for dependency injection with a plugin mechanism extending coverage, parallelism, async, and database testing as needed.
For Programming Languages & Frameworks work the value is the balance of readability and extensibility, since fixtures inject database connections, temporary files, and client instances with controllable scope, parametrisation runs many cases from one dataset, and conftest.py organises shared fixtures.
Recognise the boundaries, because dynamic typing weakens type checking versus static languages, async tests need plugins like pytest-asyncio, deeply nested fixtures in large projects obscure dependencies, and big suites need pytest-xdist parallelism in CI.
Core Strengths
- Plain assert statements keep test code concise and readable
- Fixture injection with controllable scopes
- Parametrisation covers many input combinations in one line
- Rich plugins for coverage, parallelism, async, mocking, and benchmarks
Product Ecosystem
Core
Function and class collection, assert rewriting, fixture injection, and conftest.py.
Plugins
pytest-cov for coverage, pytest-xdist parallelism, pytest-asyncio, and pytest-mock.
Framework integration
pytest-django, FastAPI TestClient, and SQLAlchemy session fixtures.
Drawbacks
- Dynamic typing leaves type errors to tests
- Async testing needs extra plugins
- Deep fixture nesting obscures dependencies
- Large suites need parallelism plugins for fast CI
Use Cases
- Python unit and integration tests (β β β β β ) - The standard
- Data and algorithm validation (β β β β β ) - Strong parametrisation
- Web API testing (β β β β β ) - TestClient integration
- Performance benchmarks (β β β ) - Needs a benchmark plugin
Pricing
| Plan | Price | Scope |
|---|---|---|
| pytest and plugins | $0 | MIT licensed, free |
| CI runtime cost | Platform billing | Parallelism cuts duration |
| Commercial test platforms | Usage billing | Reporting and device clouds |
Free and the zero-cost standard for Python projects, where the real investment is test design since fixture organisation decides long-term maintainability, best layered as session-scoped connections with function-scoped rollbacks.
FAQ
-
pytest or unittest? Always pytest for new projects, while existing unittest suites run under pytest without rewrites.
-
How should fixtures be organised? Layer by scope with session connections, function-scoped transaction rollbacks, and shared fixtures in conftest.py.
-
How are tests sped up? Use pytest-xdist, avoid unnecessary database work, and separate slow integration from fast unit tests.
Code Examples
Fixtures with parametrisation
import pytest
@pytest.fixture
def plans():
return [{"name": "Hong Kong VPS", "price": 99}, {"name": "US VPS", "price": 59}]
@pytest.mark.parametrize("max_price, expected", [(80, 1), (200, 2)])
def test_filter_cheap(plans, max_price, expected):
assert len(filter_cheap(plans, max_price)) == expected
pytest -q --cov=src runs everything with coverage while -k filters by name.
Async and exception assertions
import pytest
@pytest.mark.asyncio
async def test_fetch_timeout():
with pytest.raises(TimeoutError):
await fetch_with_timeout("https://slow.example.com", timeout=0.01)
Community & Resources
Communities and official channels
- pytest Site β Docs and plugin index
- GitHub pytest-dev/pytest β Source, issues, and releases
- Stack Overflow #pytest β Fixture and plugin answers
Learning resources and tools
- Fixture Guide β Scope and dependency injection
- Parametrisation β Generate input combinations
- pytest-cov β Coverage plugin