Skip to content

Pytest — Python Testing Framework

Standard testing framework for Python: fixtures, parametrize, markers, mocking, coverage.

Installation

uv add --dev pytest pytest-cov pytest-xdist pytest-asyncio pytest-mock pytest-randomly

Section Map

Folder Focus
01 Core Guides Fundamentals (fixtures, assertions, parametrize, markers) and advanced patterns (mocking, monkeypatch, async, coverage, plugins)
02 Practical Playbooks Real-world recipes, test-data factories, flaky-debug workflow, config template

Core Guides

File Topics
01 Fundamentals Discovery, assertions, fixtures, conftest, scope, parametrize, markers
02 Advanced Patterns Mocking, monkeypatch, tmp_path, capsys, async, coverage, plugins

Practical Playbooks

File Topics
01 Real-World Recipes FastAPI, service mock, DB rollback, CLI, retry, contract, time freeze
02 Test Data Factories Factory fixtures, deterministic faker, FactoryBoy
03 Flakiness Debugging Triage commands, order dependency, isolation checklist
04 Config Template pyproject.toml baseline, marker strategy, local vs CI

Quick Commands

Command Use
pytest Run all
pytest tests/test_api.py::test_login One test
pytest -k "login and not admin" Filter by name
pytest -m "not slow" Exclude marker
pytest -x --lf Stop + rerun last failed
pytest -n auto Parallel
pytest --cov=src --cov-fail-under=100 Coverage gate

Baseline Config

[tool.pytest.ini_options]
testpaths = ["tests"]
addopts = "-ra -q --strict-markers --strict-config"
markers = ["slow: long tests", "integration: external deps"]
filterwarnings = ["error"]

Standards

  • Deterministic, isolated tests.
  • Fixtures with yield for reliable teardown.
  • Mock external boundaries, not internal logic.
  • Strict markers/config and coverage threshold in CI.

See also