python-testing-patterns
Python Testing Patterns
Overview
Guide pytest-based testing for Python services. Covers fixture design, factory patterns, mocking strategies, async testing, and parametrize usage. Applies the testing pyramid: unit tests for services, integration tests for repos and DB, end-to-end tests for API endpoints. Stack-specific Tier 3 reference skill.
Workflow
-
Read project setup — Check
.chalk/docs/engineering/for architecture docs. Determine the testing stack: pytest version, async framework (asyncio, trio), HTTP client (httpx, requests, aiohttp), ORM (SQLAlchemy, Django ORM, Tortoise), and any existing test conventions. Readconftest.pyfiles to understand current fixture organization. -
Identify the scope — Parse
$ARGUMENTSfor the specific module, test file, or testing concern. Categories include: fixture design, factory patterns, mocking, async testing, parametrize, or test organization. -
Audit fixture design — Check for:
- Fixture scope:
function(default, safest),class,module,session(fastest but risk shared state). Use the narrowest scope that avoids unacceptable slowness. conftest.pyorganization: fixtures in the rightconftest.pylevel (project root for shared, per-directory for scoped). Avoid importing fixtures across test directories.- Fixture dependencies: fixtures that depend on other fixtures should form a clean DAG, not a tangled web.
yieldfixtures for setup/teardown: prefer overaddfinalizerfor readability.
- Fixture scope:
-
Audit factory patterns — Check for:
More from generaljerel/chalk-skills
python-clean-architecture
Clean architecture patterns for Python services — service layer, repository pattern, domain models, dependency injection, error hierarchy, and testing strategy
23create-handoff
Generate a handoff document after implementation work is complete — summarizes changes, risks, and review focus areas for the review pipeline. Use when done coding and ready to hand off for review.
16create-review
Bootstrap a local AI review pipeline and generate a paste-ready review prompt for any reviewer agent. Use after creating a handoff or when ready to get an AI code review.
15fix-findings
Fix findings from the active review session — reads reviewer findings files, applies fixes by priority, and updates the resolution log. Use after pasting reviewer output into findings files.
15fix-review
When the user asks to fix, address, or work on PR review comments — fetch review comments from a GitHub pull request and apply fixes to the local codebase. Requires gh CLI.
15review-changes
End-to-end review pipeline — creates a handoff, generates a review (self-review or paste-ready for another provider), then offers to fix findings. Use when you want to review your changes before pushing.
13