pytest-best-practices
Skill17 starsChanged 9 months agoMITView on GitHub
Expert guidance for writing high-quality pytest tests. Use when writing tests, setting up fixtures, parametrizing, mocking, or reviewing test code.
- Installs packages
In this repository19 other files it gives its agents.
- CLAUDE.mdCLAUDE.md
- docs/llms.txtllms.txt
- manage-skillsSkill
- manage-hooksSkill
- create-meta-promptsSkill
- create-plansSkill
- create-subagentsSkill
- damage-controlSkill
- debug-like-expertSkill
- docs-masterSkill
- file-searchSkill
- generate-imagesSkill
- generate-prdSkill
- gitSkill
- manage-slash-commandsSkill
- ralph-convert-prdSkill
- ralph-orchestratorSkill
- spec-interviewSkill
- sqlalchemy-postgresSkill
The file
We split this skill into the rules it gives an agent
4 rules. About 0 are common, 3 are distinctive, and 1 is very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈3
Distinctive · estimate
This author's own choices. Worth a look.
1
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Distinctive: line 14
Use fixtures for setup/teardown, never class-level mutable state
- Very specific: line 35
Use plain assert statements (pytest provides detailed failure messages)
- Distinctive: line 37
Test edge cases: empty inputs, boundaries, invalid data, errors
- Distinctive: line 38
Keep tests focused and readable
≈ “Common” is an estimate: at least 1 in 5 of the 213,895 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
<objective>
Provide pytest best practices and patterns for writing maintainable, efficient tests.
</objective><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 filesCore 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>
<routing>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 |
</routing><dependencies>
```bash
pip install pytest pytest-asyncio pytest-mock pytest-cov pytest-xdist
```
</dependencies>Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

