Python Unit Test Standards
Standards for writing meaningful, maintainable tests that verify behavior.
Test Naming Convention
# Unit tests: test__<what>__<expected>
def test__batch_allocation__reduces_available_quantity():
pass
# Integration tests: test__<component>__<behavior>
def test__repository__saves_and_retrieves_batch():
pass
# E2E tests: test__<use_case>__<scenario>
def test__order_allocation__happy_path():
pass
Quality Criteria
| Good (Behavioral) | Bad (Tautological) |
|---|---|
| Test outcomes and state changes | Assert mocked return values |
| Use real objects where possible | Test that assignment works |
| Verify domain invariants | Tests with no assertions |
| Test error conditions explicitly | Test implementation details |
Coverage Requirements
| Scope | Minimum | Target |
|---|---|---|
| New code | 80% | 90% |
| Critical paths | 95% | 100% |
| Overall project | 70% | 80% |
File Organization
tests/
├── unit/ # Fast, isolated (<100ms each)
├── integration/ # Database, API, external services
├── e2e/ # Full workflow tests
└── conftest.py # Shared fixtures
Tautological Test Detection
Tests that can't fail provide false confidence:
- Mock assertion - Verifying mock returns what you told it to
- Assignment test - Testing that
x = 5results inx == 5 - Empty test - No assertions or only
pass - Implementation test - Testing HOW not WHAT
See reference.md for detection patterns and examples.md for comparisons.
Converted and distributed by TomeVault — claim your Tome and manage your conversions.