Test Optimization Guide
Quick Fixes Applied
- Reduced Test Verbosity
- Changed log level from
INFOtoWARNINGin pytest.ini - Added
--tb=shortfor shorter traceback output - Added 60-second timeout per test to prevent hanging
- Changed log level from
- Optimized test_memory_exhaustion_prevention
- Reduced file count from 1000 to 100
- Mocked logging to reduce output
- Limited batch processing in tests
Additional Optimization Strategies
1. Run Tests in Parallel
# Install pytest-xdist
uv pip install pytest-xdist
# Run tests with 4 workers
python -m pytest tests/ -n 4
# Auto-detect CPU count
python -m pytest tests/ -n auto
2. Run Specific Test Suites
# Run only unit tests (exclude integration)
python -m pytest tests/ -m "not integration"
# Run only fast tests
python -m pytest tests/ -m "not slow"
# Skip security robustness tests
python -m pytest tests/ --ignore=tests/test_extract_files_security_robustness.py
3. Use Test Markers
Add markers to slow tests:
@pytest.mark.slow
@pytest.mark.timeout(120) # 2 minute timeout
def test_memory_exhaustion_prevention():
...
Then run without slow tests:
python -m pytest tests/ -m "not slow"
4. Cache Test Results
# Use pytest cache
python -m pytest tests/ --lf # Run only last failed
python -m pytest tests/ --ff # Run failed first, then others
5. Disable Coverage During Development
# Run without coverage for faster execution
python -m pytest tests/ --no-cov
# Or temporarily disable in specific tests
python -m pytest tests/ -p no:cov
6. Environment Variables for Test Speed
# Disable logging during tests
export PYTEST_DISABLE_LOGGING=1
# Use minimal output
python -m pytest tests/ -q
# Only show failed tests
python -m pytest tests/ --tb=no -q
7. Test Profiling
# Profile slow tests
python -m pytest tests/ --durations=10
# Find slowest tests
python -m pytest tests/ --durations=0 | head -20
8. Optimized Test Configuration
Create a fast test configuration:
# pytest-fast.ini
[pytest]
testpaths = tests
addopts = -q --tb=short --no-cov -n auto
log_cli = false
timeout = 30
markers =
slow: marks tests as slow
integration: marks tests as integration tests
Run with: python -m pytest -c pytest-fast.ini
9. Skip Tests Conditionally
@pytest.mark.skipif(os.environ.get('SKIP_SLOW_TESTS'), reason="Skipping slow tests")
def test_large_batch_processing():
...
10. Mock Heavy Dependencies
For the problematic test, consider mocking:
- GenAI client initialization
- GCS operations
- Async task runners
Recommended Development Workflow
- During active development:
python -m pytest tests/ -x --lf --no-cov -q - Before committing:
python -m pytest tests/ -m "not slow" --cov=. --cov-report=term - Full CI simulation:
python -m pytest tests/ -v --cov=. --cov-report=term --cov-report=json
Test Organization
Consider splitting tests:
test_extract_files_unit.py- Fast unit teststest_extract_files_integration.py- Integration teststest_extract_files_security_robustness.py- Security tests (marked as slow)
Immediate Actions
- The optimized test file has been created:
test_extract_files_security_robustness_optimized.py - pytest.ini has been updated with timeout and reduced logging
- pytest-timeout has been installed
Run the optimized version:
python -m pytest tests/test_extract_files_security_robustness_optimized.py -v