pytest
The standard Python testing framework. Supports fixtures, parametrize, markers, plugins, and assertion rewriting. Current version is 9.0.2. Major versions (8.x, 9.x) have removed long-deprecated features that LLMs still generate.
Warnings
- breaking pytest.warns(None) removed in 8.0. It was used to assert no warnings were emitted, but its semantics were inverted — it actually asserted at least one warning was raised. This pattern is very common in LLM-generated test code.
- breaking @pytest.yield_fixture removed. LLMs frequently generate this decorator as it appeared in tutorials for years.
- breaking nose plugin support removed in 8.0. Tests written for nose (using setup/teardown at module level, nose-style generators) will fail to collect.
- breaking Python 3.9 support dropped in pytest 9.0 (following Python 3.9 EOL). Tests on Python 3.9 must pin to pytest<9.
- breaking File/directory collection order changed in pytest 8.x. Files and directories are now collected alphabetically together. Previously, files were collected before directories. Test suites with ordering assumptions may break.
- gotcha Async tests require pytest-asyncio. pytest does not natively run async def test_ functions — they will appear to pass silently without actually running the test body.
- gotcha --strict renamed to --strict-markers (for marker strictness) and --strict-config (for config strictness). The bare --strict still works in 9.x but now enables full strict mode.
- gotcha pytest_plugins defined in non-root conftest.py files is deprecated. It activates plugins globally, not just for tests below that conftest.
Install
-
pip install pytest -
pip install pytest pytest-cov pytest-asyncio
Imports
- pytest
import pytest
- pytest.fixture
@pytest.fixture def my_fixture(): ...
Quickstart
# test_example.py
import pytest
@pytest.fixture
def sample_data():
return {"key": "value"}
def test_basic(sample_data):
assert sample_data["key"] == "value"
@pytest.mark.parametrize("x,expected", [(1, 2), (2, 4)])
def test_double(x, expected):
assert x * 2 == expected
# Run: pytest -v