Step right up! Just as Fred and George's joke shop offered magical products for every conceivable need, the pytest plugin ecosystem extends your testing toolkit without modifying pytest's core. Each plugin is a cleverly packaged invention — install it, configure it, and watch the magic happen.
Plugins are packages that hook into pytest's internal machinery. They can modify test collection, change reporting, add fixtures, or alter execution — all without touching pytest's source code. It's like enchanting your wand with new spells without reshaping the wood.
# Installing any plugin is as simple as:
pip install pytest-PLUGINNAME
# pytest auto-discovers installed plugins. No configuration needed!
# Check what's loaded:
pytest --co -q # shows collected tests with active plugins
A Sneakoscope spins when something untrustworthy is nearby. pytest-cov reveals untested code lurking in the shadows:
# Install
pip install pytest-cov
# Run with coverage report
pytest --cov=mypackage --cov-report=term-missing
# Output shows which lines are NOT covered:
# Name Stmts Miss Cover Missing
# mypackage/spells.py 42 7 83% 23-25, 31, 40-42
Hermione used a Time-Turner to attend multiple classes simultaneously. pytest-xdist runs tests in parallel across multiple CPUs:
# Install
pip install pytest-xdist
# Run tests on 4 parallel workers
pytest -n 4
# Auto-detect number of CPUs
pytest -n auto
# Distribute by file (faster for many small test files)
pytest -n 4 --dist=loadfile
Your CI pipeline just got a Time-Turner. A 10-minute suite might shrink to 3 minutes!
The Sorting Hat assigns houses unpredictably. pytest-randomly shuffles test execution order to catch hidden dependencies:
# Install
pip install pytest-randomly
# Tests now run in random order by default!
# To reproduce a specific order, use the seed from output:
pytest -p randomly --randomly-seed=12345
# Disable for a single run:
pytest -p no:randomly
Stupefy! If a test runs too long, stun it:
# Install
pip install pytest-timeout
# Set a global timeout of 30 seconds
pytest --timeout=30
# Or per-test with a marker:
import pytest
@pytest.mark.timeout(5)
def test_quick_spell():
"""This must complete in 5 seconds or be stunned."""
result = cast_complex_charm()
assert result is not None
Polyjuice Potion lets you impersonate anyone. pytest-mock provides the mocker fixture for easy mocking:
# Install
pip install pytest-mock
def test_send_owl(mocker):
"""Mock the owl delivery service."""
# Polyjuice: make a fake owl service
mock_owl = mocker.patch("hogwarts.mail.send_owl")
mock_owl.return_value = {"status": "delivered"}
from hogwarts.mail import deliver_letter
result = deliver_letter("Harry", "You're a wizard!")
mock_owl.assert_called_once_with("Harry", "You're a wizard!")
assert result["status"] == "delivered"
The Floo Network handles asynchronous travel. pytest-asyncio lets you test async code naturally:
# Install
pip install pytest-asyncio
import pytest
@pytest.mark.asyncio
async def test_floo_travel():
"""Test async fireplace transportation."""
destination = await floo_network.travel("Diagon Alley")
assert destination.name == "Diagon Alley"
assert destination.arrived is True
A Pensieve stores memories for comparison. pytest-snapshot stores expected outputs and compares against them:
# Install
pip install pytest-snapshot
def test_spell_catalog(snapshot):
"""Snapshot the spell catalog output."""
catalog = generate_spell_catalog()
snapshot.assert_match(catalog, "spell_catalog.txt")
# First run: creates the snapshot file
# Subsequent runs: compares against stored snapshot
# Update snapshots: pytest --snapshot-update
The Daily Prophet delivers news in a readable format. pytest-html generates beautiful HTML test reports:
# Install
pip install pytest-html
# Generate a report
pytest --html=report.html --self-contained-html
# The report includes:
# - Pass/fail summary
# - Duration per test
# - Captured logs and stdout
# - Environment metadata
| Plugin | Weasley Product | Purpose |
|---|---|---|
pytest-cov | Sneakoscope | Code coverage reports |
pytest-xdist | Time-Turner | Parallel test execution |
pytest-randomly | Sorting Hat | Randomize test order |
pytest-timeout | Stupefy | Kill slow tests |
pytest-mock | Polyjuice Potion | Easy mocking |
pytest-asyncio | Floo Network | Async test support |
pytest-snapshot | Pensieve | Snapshot/golden testing |
pytest-html | Daily Prophet | HTML report generation |
"Why are you worrying about You-Know-Who? You should be worrying about U-No-Poo — the constipation sensation that's gripping the nation!" — Weasleys' Wizard Wheezes advertisement (but also: don't worry about testing complexity when plugins handle it for you).