Test Performance Improvements
Overview
This document outlines the performance improvements achieved by introducing pytest-xdist for parallel test execution in the Gyrinx project.
Current State Analysis
Total tests: 451 test cases
Test distribution:
Core app: 45 test files
Content app: 14 test files
Pages app: 1 test file
API app: 1 test file
Current execution: Sequential (one test at a time)
Parallelization: None configured
Changes Implemented
1. Added pytest-xdist to requirements.txt
pytest-xdist==3.6.12. Parallel Test Execution
With pytest-xdist installed, you can now run tests in parallel:
Expected Performance Improvements
Based on typical Django test suite characteristics:
Sequential execution: All 451 tests run one after another
Parallel execution (4 cores): ~3-4x speedup expected
Parallel execution (8 cores): ~5-6x speedup expected
The actual speedup depends on:
Number of CPU cores available
Test isolation and database transaction overhead
Amount of I/O-bound vs CPU-bound operations
Additional Optimizations
1. Database Reuse (pytest-django feature)
Since pytest-django==4.11.1 is already installed, you can also use:
2. Combined Usage
For maximum performance:
CI/CD Configuration
For GitHub Actions or other CI systems:
Monitoring Test Performance
Use pytest's built-in duration reporting:
Best Practices
Ensure test isolation: Tests must not depend on execution order
Use transactions: Django's TestCase handles this automatically
Avoid shared state: Don't use module-level variables that tests modify
Monitor resource usage: More workers isn't always better
Troubleshooting
If tests fail with parallel execution:
Run tests sequentially to isolate the issue:
Run specific test in isolation:
Check for test interdependencies or shared state issues
Conclusion
Adding pytest-xdist enables significant test performance improvements through parallel execution. Combined with database reuse, this can reduce test execution time by 60-80% on multi-core systems.
Last updated