# Python Testing Patterns

> Comprehensive pytest patterns for Python 3.14 / FastAPI — fixtures, parametrize, mocking, async testing, database fixtures, test markers, coverage config, and CI integration. Use when writing or structuring Python test suites beyond the basic TDD cycle.

- Skill: `kumaran-is/python-testing-patterns` (Agent Skill, multi-file: 3 files)
- Install (CLI): `npx skillmds@latest add kumaran-is/python-testing-patterns`
- Raw SKILL.md: https://api.skillmd.com/api/skills/kumaran-is/python-testing-patterns/raw
- Safety review: pending
- Works with: Claude Code, Claude.ai, OpenAI Codex
- Category: Integrations & APIs
- Author: kumaran-is (https://skillmd.com/u/kumaran-is)
- Updated: 2026-09-17
- Page: https://skillmd.com/skills/kumaran-is/python-testing-patterns

---


## Iron Law

**NO TEST INFRASTRUCTURE WITHOUT READING THE PLAYBOOK FIRST — fixtures, markers, and coverage config have workspace-specific patterns that prevent test debt**

# Python Testing Patterns

Deep reference for pytest test infrastructure, fixtures, async patterns, and CI integration for Python 3.14 / FastAPI services.

## When to Use

Load this skill when:
- Setting up a new test suite from scratch (conftest.py, pyproject.toml config)
- Writing parametrized tests for multiple input scenarios
- Mocking async dependencies (AsyncMock patterns)
- Testing database operations with session fixtures
- Configuring test markers (`slow`, `integration`) for selective execution
- Setting up coverage reporting gates
- Wiring GitHub Actions CI for Python tests

Load `test-driven-development` skill first for the Red-Green-Refactor process. This skill handles the *infrastructure* — not the TDD process.

## Quick Reference

| Need | Pattern |
|------|---------|
| Multiple inputs | `@pytest.mark.parametrize` |
| Async test | `@pytest.mark.asyncio` + `AsyncMock` |
| HTTP endpoint test | `httpx.AsyncClient` + `ASGITransport` |
| DB session in test | `AsyncSession` fixture + rollback teardown |
| Slow/integration gate | `@pytest.mark.slow` + `-m "not slow"` in CI |
| Coverage gate | `pytest --cov=src --cov-fail-under=80` |
| Property-based | `@given(st.text())` from Hypothesis |

## Reference File

Load `resources/implementation-playbook.md` for:
- 10 fundamental patterns (fixtures, parametrize, mocking, async, monkeypatch, tmp_path, Hypothesis)
- 10 advanced patterns (DB fixtures, test markers, coverage config, CI workflow, test organization)
- Full conftest.py templates
- GitHub Actions workflow for Python tests
- pytest.ini / pyproject.toml configuration

## Relationship to Other Skills

- `test-driven-development` — process (Red-Green-Refactor, when to write tests, naming)
- `python-testing-patterns` — infrastructure (how to structure, configure, and run tests)
- `agentic-ai-dev/reference/agentic-testing.md` — LangGraph agent-specific testing patterns

## Post-Test Checklist

- [ ] `pytest -q` — all tests pass
- [ ] `pytest --cov=src --cov-report=term-missing` — coverage visible
- [ ] No `print()` in test files — use `capfd` fixture or logging
- [ ] Async tests use `@pytest.mark.asyncio` or `asyncio_mode = "auto"` in config
- [ ] External services mocked — no real HTTP calls in unit tests

