pytest: Python Testing Framework
pytest is a mature, feature-rich testing framework for Python that simplifies test writing and execution through a clear, concise syntax. Unlike Python’s built-in unittest module, pytest minimizes boilerplate code and offers powerful fixture capabilities, parameterized testing, and a comprehensive plugin ecosystem.
When to use pytest
Use pytest when you need to write and run tests for a Python project. It is the standard choice for most Python testing, from small scripts to large applications, because its assert-based syntax requires less code than unittest and its fixture system scales well as projects grow. Prefer pytest over unittest unless you are maintaining a codebase already committed to unittest conventions or working in an environment where third-party packages are restricted.
Core Features
Test Discovery and Execution
- Automatic discovery of test modules, functions, and methods following naming conventions
- Simple assertion syntax using Python’s built-in
assert - Detailed failure reports with intelligent introspection and value comparison
- Flexible test selection via command-line options, markers, and path specifications
- Parallel test execution for faster test runs
Fixtures
Fixtures provide a modular way to manage test dependencies and setup/teardown:
@pytest.fixture
def api_client():
client = APIClient()
yield client
client.close()
def test_api_call(api_client):
# The fixture is automatically injected
response = api_client.get_data()
assert response.status_code == 200Fixtures can be scoped to function, class, module, or session levels.
Parameterization
Tests can be run with multiple sets of inputs:
@pytest.mark.parametrize("input,expected", [
(1, 1),
(2, 4),
(3, 9),
(4, 16),
])
def test_square(input, expected):
assert input * input == expectedCommand-Line Interface
# Run all tests
pytest
# Run tests in a specific file
pytest test_file.py
# Run a specific test
pytest test_file.py::test_function
# Run tests matching a pattern
pytest -k "pattern"
# Run tests with a specific marker
pytest -m slow
# Generate coverage report
pytest --cov=mypackagePros
- Minimal boilerplate compared to unittest
- Powerful fixture system for dependency injection
- Extensive plugin ecosystem
- Detailed error reporting
- Compatible with other testing tools
Cons
- Learning curve for advanced features
- Plugin dependencies can complicate maintenance
- Fixture complexity can grow in large projects
- Some patterns differ from unittest paradigms
Learn More
- Setting up testing with pytest and uv (Tutorial)
- How to run tests using uv
- How to test against multiple Python versions using uv
- How to fix common pytest errors with uv
- How to run tests in parallel with pytest-xdist
- tox and nox for multi-version test automation
- pytest Documentation
- Plugin Directory
- Best Practices
Also Mentioned In
- uv: A Complete Guide to Python's Fastest Package Manager
- Getting Started with Python Using Claude Code
- uvx.sh: Install Python tools without uv or Python
- Simple, Modern Python
- How to configure Cursor for a uv project
- How to configure VS Code for a uv project
- How to Fix Common pytest Errors with uv
- How to run tests in parallel with pytest-xdist
- How to Run Tests Using uv
- How to Test Against Multiple Python Versions Using uv
- How to use the pydevtools CLAUDE.md template for Python projects
- Modern Python Project Setup Guide for AI Assistants
- nox: Python Test Automation Tool
- pyproject.toml: Python Project Configuration File
- Setting up GitHub Actions with uv
- Setting up testing with pytest and uv
- tox: Python Test Automation Tool
Get Python tooling updates
Subscribe to the newsletter