pytest
raw JSON → 9.0.2 verified Tue May 12 auth: no python install: verified quickstart: verified
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.
pip install pytest Common errors
error ModuleNotFoundError: No module named 'pytest' ↓
cause The 'pytest' package is not installed in the current Python environment.
fix
Install 'pytest' using pip: 'pip install pytest'.
error ModuleNotFoundError: No module named 'pytest_sharding' ↓
cause The 'pytest_sharding' plugin is not installed in the current Python environment.
fix
Install the 'pytest_sharding' plugin using pip: 'pip install pytest_sharding'.
error ModuleNotFoundError: No module named 'my_module' ↓
cause The module 'my_module' is not in the Python path, often due to incorrect project structure or missing '__init__.py' files.
fix
Ensure the module is in the Python path by adding '__init__.py' files to directories and adjusting the 'PYTHONPATH' environment variable if necessary.
error ImportError: No module named 'app' ↓
cause The 'app' module cannot be found, possibly due to incorrect import statements or missing '__init__.py' files.
fix
Verify the import statements are correct and that '__init__.py' files are present in the necessary directories.
error pytest: command not found ↓
cause The `pytest` executable is not found in your system's PATH, typically because it's not installed, or your virtual environment isn't activated, or the installation location isn't in PATH.
fix
First, ensure pytest is installed in your active Python environment:
pip install pytest. If using a virtual environment, activate it (source venv/bin/activate on Linux/macOS, venv\Scripts\activate on Windows) before running pytest. Alternatively, you can always invoke it as a Python module: python -m pytest. 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. ↓
fix To assert no warnings: use warnings.catch_warnings() or recwarn fixture and assert len(recwarn) == 0. To assert a specific warning: pytest.warns(UserWarning).
breaking @pytest.yield_fixture removed. LLMs frequently generate this decorator as it appeared in tutorials for years. ↓
fix Replace @pytest.yield_fixture with @pytest.fixture. yield inside @pytest.fixture has been supported since pytest 3.0.
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. ↓
fix Rewrite nose-style tests using pytest fixtures and @pytest.mark.parametrize.
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. ↓
fix Pin pytest<9 for Python 3.9 environments.
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. ↓
fix Do not rely on implicit test collection order. Use pytest-ordering if explicit order is needed.
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. ↓
fix pip install pytest-asyncio. Add asyncio_mode = 'auto' to pytest.ini or mark individual tests with @pytest.mark.asyncio.
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. ↓
fix Use --strict-markers to fail on undeclared markers. Declare all markers in pytest.ini under [pytest] markers.
gotcha pytest_plugins defined in non-root conftest.py files is deprecated. It activates plugins globally, not just for tests below that conftest. ↓
fix Define pytest_plugins only in the root conftest.py or pyproject.toml.
Install
pip install pytest pytest-cov pytest-asyncio Install compatibility verified last tested: 2026-05-12
python os / libc status wheel install import disk
3.10 alpine (musl) - - 0.46s 30.4M
3.10 alpine (musl) - - 0.43s 32.2M
3.10 slim (glibc) - - 0.29s 31M
3.10 slim (glibc) - - 0.29s 33M
3.11 alpine (musl) - - 0.58s 33.3M
3.11 alpine (musl) - - 0.61s 35.7M
3.11 slim (glibc) - - 0.47s 34M
3.11 slim (glibc) - - 0.47s 36M
3.12 alpine (musl) - - 0.53s 24.9M
3.12 alpine (musl) - - 0.52s 27.2M
3.12 slim (glibc) - - 0.51s 25M
3.12 slim (glibc) - - 0.56s 28M
3.13 alpine (musl) - - 0.47s 24.6M
3.13 alpine (musl) - - 0.46s 26.5M
3.13 slim (glibc) - - 0.43s 25M
3.13 slim (glibc) - - 0.44s 27M
3.9 alpine (musl) - - 0.37s 29.7M
3.9 alpine (musl) - - 0.36s 31.5M
3.9 slim (glibc) - - 0.34s 30M
3.9 slim (glibc) - - 0.34s 32M
Imports
- pytest wrong
from py.test import * # py.test CLI name only, not an importable packagecorrectimport pytest - pytest.fixture wrong
@pytest.yield_fixture def my_fixture(): yield value # yield_fixture decorator removed in 9.xcorrect@pytest.fixture def my_fixture(): ...
Quickstart verified last tested: 2026-04-23
# 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