Overview
Comprehensive testing ensures reliability, maintainability, and confidence in deployments. This guide covers unit testing, integration testing, property-based testing, load testing, and security testing.We aim for >80% code coverage with a multi-layered testing approach including unit, integration, property-based, and end-to-end tests.
Testing Pyramid
Unit Tests
- Fast (< 1s)
- Isolated
- Mocked dependencies
- 70-80% of tests
Integration Tests
- Real services
- Database, Redis, APIs
- 15-20% of tests
- Slower (seconds)
E2E Tests
- Full system
- User workflows
- 5-10% of tests
- Slowest (minutes)
Test Organization
Directory Structure
Test Markers
Unit Testing
Writing Unit Tests
Mocking Dependencies
Testing Async Code
Integration Testing
Setup Test Environment
Integration Tests
API Integration Tests
Property-Based Testing
Using Hypothesis
Load Testing
Using Locust
Using k6
Security Testing
SQL Injection Tests
Authentication Tests
Continuous Integration
GitHub Actions Workflow
Test Coverage
Measuring Coverage
Coverage Configuration
Best Practices
Test Naming
Test Naming
AAA Pattern
AAA Pattern
Fixtures Over Setup
Fixtures Over Setup
One Assert Per Test
One Assert Per Test
Test Dependency Management
The Problem
On 2025-11-12, 10 CI jobs failed withModuleNotFoundError: docker.errors despite tests passing locally:
- Root cause:
dockerandkubernetespackages were incode-executionextras, notdevextras - Impact: CI workflows install
devextras but tests import docker modules - Result: Tests pass locally (all extras installed) but fail in CI (only
devinstalled)
Prevention Strategy
Three layers of protection prevent this issue:1
Regression Test
File: Runs: On every commit in CI, catches missing dependencies immediately.
tests/regression/test_dev_dependencies.pyValidates all test imports against project dependencies:2
Pre-commit Hook
Hook ID: Fast: Only runs affected test, typically < 5 seconds.
validate-test-dependenciesRuns dependency validation before each commit:3
Workflow Validation
Script: Hook ID:
scripts/validation/validate_workflow_test_deps.pyValidates GitHub Actions workflows:validate-workflow-test-deps (runs on workflow file changes)Dependency Rules
Rule 1: Test Imports → Dev Extras
Rule 1: Test Imports → Dev Extras
If a test file imports a package, that package must be in project dependencies.Why
dev extras?- All test workflows install
devextras by default - Keeps test dependencies separate from production code
- Matches pytest, pytest-cov, pytest-mock pattern
Rule 2: Handle Package Name Mismatches
Rule 2: Handle Package Name Mismatches
Some packages have different import names:
The regression test handles these automatically via
get_import_name_mapping().Rule 3: Stdlib Modules
Rule 3: Stdlib Modules
Python 3.11+ includes The
tomllib in stdlib. Since the project minimum is Python 3.11,
use tomllib directly:tomli backport is no longer needed or allowed.Rule 4: Exclude Third-Party Helpers
Rule 4: Exclude Third-Party Helpers
Third-party test tools (e.g., bats-core) have their own dependencies:The regression test skips
test_helper/ directories automatically.Quick Reference
Troubleshooting
Test fails in CI but passes locally
Test fails in CI but passes locally
Symptom:
ModuleNotFoundError in CI, test passes on your machine.Cause: Package installed locally but not in dev extras.Solution:- Run dependency validation:
pytest tests/regression/test_dev_dependencies.py -v - Add missing package to
[project.optional-dependencies.dev] - Run
uv sync --extra devto update lockfile - Re-run tests locally to verify
Pre-commit hook fails
Pre-commit hook fails
Symptom:
validate-test-dependencies hook fails on commit.Cause: New test import without corresponding dependency.Solution:- Check hook output for missing packages
- Add packages to appropriate extras in
pyproject.toml:- Test dependencies →
dev - Code execution →
code-execution - CLI tools →
cli
- Test dependencies →
- Run
uv sync --extra dev - Re-run pre-commit:
pre-commit run validate-test-dependencies --all-files
Workflow validation fails
Workflow validation fails
Symptom:
validate-workflow-test-deps hook fails.Cause: Workflow runs pytest but doesn’t install dev extras.Solution:
Update workflow to include dev in extras:Historical Context: This protection system was implemented after commit
7b51437 (2025-11-12) to prevent the recurrence of 10 simultaneous CI failures. The regression test, pre-commit hooks, and workflow validation ensure this class of error can never happen again.Next Steps
Development Setup
Development environment guide
Contributing
Contributing guidelines
CI/CD
Continuous integration
Monitoring
Production monitoring
Testing Complete: Comprehensive test coverage for reliable deployments!