pytest / Testing Skill
Test generation and coverage improvement - an Agent Skill for Python 3.14 and pytest.
What This Skill Does
Audits a module and adds tests: tests/ layout, conftest.py fixtures, parametrized edge cases, pytest.mark integration/slow, httpx.MockTransport or pytest-httpx for HTTP, database transaction rollbacks, and CI pytest --cov with sensible threshold.
When to Invoke
- New service with zero tests before first deploy
- PR adding complex branch logic without coverage
- Flaky test cleanup after CI noise
- Converting unittest suite to pytest idioms
Inputs
| Input | Why |
|---|---|
| Target package | Which modules to cover first |
| External deps | DB, Redis, HTTP to fake vs testcontainers |
| Coverage goal | Line vs branch; minimum percent |
| Markers | slow, integration, no_ci |
| Async code | pytest-asyncio mode auto |
Outputs
tests/conftest.pywith shared fixtures- Parametrized tests for edge cases from Gotchas docs
pyproject.toml[tool.pytest.ini_options]coverageomit patterns forif TYPE_CHECKING- CI snippet matching local
uv run pytestcommand
Guardrails
- Test behavior, not implementation - assert outputs and side effects.
- One assertion concept per test - clear failure messages.
- Freeze time with monkeypatch - not
sleepin tests. - No network in unit tests - mock or VCR cassettes explicit.
- Do not chase 100% coverage - focus risk-weighted modules.
- Mark integration tests - keep default
pytest -m "not integration"fast.
Recipe
uv sync --extra dev
uv run pytest -q
uv run pytest --cov=app --cov-report=term-missing --cov-fail-under=80
uv run ruff check tests/Working Example (parametrize)
import pytest
from app.pricing import discount
@pytest.mark.parametrize(
"amount,coupon,expected",
[
(100, "SAVE10", 90),
(100, "INVALID", 100),
(0, "SAVE10", 0),
],
)
def test_discount(amount, coupon, expected):
assert discount(amount, coupon) == expectedFAQs
pytest or unittest?
pytest default for new code - unittest compatibility OK during migration.
Testcontainers for Postgres?
Use for integration marker tests - unit tests use SQLite or mocked session.
Related
- pytest Setup - fundamentals
- Fixtures - fixture patterns
- Testing FastAPI - API tests
- BDD & Gherkin - acceptance layer
Stack versions: This page was written for Python 3.14.0 (stable 3.14, maintenance 3.13), FastAPI 0.115+, Django 5.2, Flask 3.1, Pydantic 2, PyTorch 2.6+, pandas 2.2+, Polars 1.x, ruff 0.9+, and uv 0.6+.