Agent skill
pytest-best-practices
Expert guidance for writing high-quality pytest tests. Use when writing tests, setting up fixtures, parametrizing, mocking, or reviewing test code.
Install this agent skill to your Project
npx add-skill https://github.com/cfircoo/claude-code-toolkit/tree/main/skills/pytest-best-practices
SKILL.md
<essential_principles>
Test Independence
- Each test must run in isolation - no shared state between tests
- Use fixtures for setup/teardown, never class-level mutable state
- Tests should pass regardless of execution order
Naming Conventions
- Files:
test_*.pyor*_test.py - Functions:
test_<description>() - Classes:
Test<ClassName> - Fixtures: descriptive
lowercase_with_underscores
Directory Structure
tests/
├── conftest.py # Shared fixtures
├── unit/
│ └── test_module.py
├── integration/
│ └── test_api.py
└── fixtures/ # Test data files
Core Testing Rules
- Use plain
assertstatements (pytest provides detailed failure messages) - One logical assertion per test when practical
- Test edge cases: empty inputs, boundaries, invalid data, errors
- Keep tests focused and readable
</essential_principles>
<quick_reference>
| Pattern | Use Case |
|---|---|
@pytest.fixture |
Setup/teardown, dependency injection |
@pytest.mark.parametrize |
Run test with multiple inputs |
@pytest.mark.skip |
Skip test temporarily |
@pytest.mark.xfail |
Expected failure (known bug) |
pytest.raises(Exception) |
Test exception raising |
pytest.approx(value) |
Float comparison |
mocker.patch() |
Mock dependencies |
conftest.py |
Share fixtures across modules |
Common Commands
pytest -v # Verbose
pytest -x # Stop on first failure
pytest --lf # Run last failed
pytest -k "pattern" # Match test names
pytest -m "marker" # Run marked tests
pytest --cov=src # Coverage report
</quick_reference>
Based on what you're doing, read the relevant reference:
| Task | Reference |
|---|---|
| Setting up fixtures, scopes, factories | references/fixtures.md |
| Parametrizing tests, multiple inputs | references/parametrization.md |
| Mocking, patching, faking dependencies | references/mocking.md |
| Markers, exceptions, assertions, async | references/patterns.md |
Recommended Agent Skills
Expand your agent's capabilities with these related and highly-rated skills.
sqlalchemy-postgres
Expert guidance for SQLAlchemy 2.0 + Pydantic + PostgreSQL. Use when setting up database layers, defining models, creating migrations, or any database-related work. Automatically activated for DB tasks.
ralph-convert-prd
Converts Product Requirements Documents into prd.json format for the Ralph autonomous agent system. Use when preparing PRDs for Ralph execution, breaking down features into atomic user stories, or when the user mentions Ralph, prd.json, or autonomous agent workflows.
ralph-orchestrator
Orchestrates the full Ralph autonomous agent pipeline from requirements gathering to execution. Use when building new features, platforms, or complex tasks that need structured development through spec-interview, PRD generation, and autonomous implementation.
generate-images
Generate and edit images using Nano Banana (Google Gemini image generation). Use whenever Claude Code needs to create new images, edit existing images, generate icons, diagrams, mockups, or any visual content.
create-subagents
Expert guidance for creating, building, and using Claude Code subagents and the Task tool. Use when working with subagents, setting up agent configurations, understanding how agents work, or using the Task tool to launch specialized agents.
manage-slash-commands
Expert guidance for creating and managing Claude Code slash commands. Use when working with slash commands, creating custom commands, understanding command structure, or learning YAML configuration. This skill should be used when the user asks to "create a slash command", "add a command", "manage commands", "build a command", or mentions slash commands, custom commands, or command configuration.
Didn't find tool you were looking for?