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 pytestVerified import paths — ran on the pinned version, not inferred.
Basic fixture and parametrize usage. Run with: pytest -v
To assert no warnings: use warnings.catch_warnings() or recwarn fixture and assert len(recwarn) == 0. To assert a specific warning: pytest.warns(UserWarning).
Replace @pytest.yield_fixture with @pytest.fixture. yield inside @pytest.fixture has been supported since pytest 3.0.
Rewrite nose-style tests using pytest fixtures and @pytest.mark.parametrize.
Pin pytest<9 for Python 3.9 environments.
Do not rely on implicit test collection order. Use pytest-ordering if explicit order is needed.
pip install pytest-asyncio. Add asyncio_mode = 'auto' to pytest.ini or mark individual tests with @pytest.mark.asyncio.
Use --strict-markers to fail on undeclared markers. Declare all markers in pytest.ini under [pytest] markers.
Define pytest_plugins only in the root conftest.py or pyproject.toml.
Install 'pytest' using pip: 'pip install pytest'.
Install the 'pytest_sharding' plugin using pip: 'pip install pytest_sharding'.
Ensure the module is in the Python path by adding '__init__.py' files to directories and adjusting the 'PYTHONPATH' environment variable if necessary.
Verify the import statements are correct and that '__init__.py' files are present in the necessary directories.
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`.