pytest
Python testing framework with fixtures and plugins
TLDR
Run all tests
SYNOPSIS
pytest [-v] [-x] [-k expression] [-m marker] [--cov package] [options] [paths]
DESCRIPTION
pytest is Python's most popular testing framework. It discovers and runs tests automatically, with powerful fixtures, parameterization, and plugin ecosystem.
Test discovery finds files matching test*.py or *test.py, and functions/methods starting with test_. Classes starting with Test are also collected. This convention-based approach minimizes configuration.
Fixtures provide reusable test dependencies. Defined with @pytest.fixture, they can set up databases, create objects, or configure environments. Fixtures can be scoped (function, class, module, session) for efficient resource management.
Parameterization runs tests with multiple inputs using @pytest.mark.parametrize. Markers (@pytest.mark.X) tag tests for selective running or custom behavior.
The plugin ecosystem extends functionality: pytest-cov for coverage, pytest-xdist for parallel execution, pytest-mock for mocking, and hundreds more. Plugins install via pip and activate automatically.
Assertion introspection provides detailed failure messages without special assertion methods. Standard Python assert statements work with rich comparison displays.
PARAMETERS
-v, --verbose
Increase verbosity.-q, --quiet
Decrease verbosity.-x, --exitfirst
Stop on first failure.-s, --capture=no
Disable output capture (show print).-k EXPRESSION
Run tests matching keyword expression.-m MARKER
Run tests with specific marker.--collect-only
List tests without running.--lf, --last-failed
Run only last failed tests.--ff, --failed-first
Run failed tests first.-n NUM
Parallel workers (pytest-xdist).--cov PACKAGE
Measure coverage (pytest-cov).--cov-report TYPE
Coverage report format.--pdb
Drop into debugger on failure.--tb STYLE
Traceback style: short, long, line, native, no.--maxfail NUM
Stop after N failures.--ignore PATH
Ignore path during collection.--durations NUM
Show N slowest tests.
CONFIGURATION
pytest.ini
Primary configuration file for test discovery paths, markers, command-line defaults, and plugin settings.pyproject.toml
Project configuration with a `[tool.pytest.ini_options]` section supporting the same options as pytest.ini.setup.cfg
Alternative configuration file with a `[tool:pytest]` section for test settings.conftest.py
Per-directory fixture and plugin file automatically loaded by pytest, used to define shared fixtures, hooks, and test configuration.
CAVEATS
Large test suites need pytest-xdist for reasonable runtime. Fixture complexity can obscure test logic. Plugin conflicts occasionally occur. Coverage may miss some code patterns. Parameterized test output can be verbose.
HISTORY
pytest was created by Holger Krekel around 2004 as py.test, evolving from the py library. It grew as an alternative to unittest, emphasizing simplicity and minimal boilerplate. The project became pytest around 2016 with the 3.0 release. It's now the de facto standard for Python testing.
