swa
swa copied to clipboard
feat: Set up complete Python testing infrastructure with Poetry
Set Up Python Testing Infrastructure
Summary
This PR establishes a complete testing infrastructure for the PyTorch SWA-Gaussian project using Poetry as the package manager and pytest as the testing framework.
Changes Made
Package Management
-
Poetry Setup: Created
pyproject.tomlwith Poetry configuration as the project's package manager - Dependencies: Added core project dependencies (torch, torchvision, tabulate)
- Dev Dependencies: Added testing tools (pytest, pytest-cov, pytest-mock) as development dependencies
Testing Configuration
-
pytest Configuration:
- Configured test discovery patterns
- Set up coverage reporting with HTML and XML outputs
- Added custom markers for test categorization (unit, integration, slow)
- Enabled strict mode and comprehensive error reporting
-
Coverage Settings:
- Source coverage for
models,utils, andtrainmodules - Branch coverage enabled
- Exclusion patterns for test files and virtual environments
- Coverage threshold set to 20% (adjustable based on project needs)
- Source coverage for
Directory Structure
tests/
├── __init__.py
├── conftest.py # Shared pytest fixtures
├── test_infrastructure_validation.py
├── unit/
│ └── __init__.py
└── integration/
└── __init__.py
Testing Utilities
Created comprehensive fixtures in conftest.py:
-
temp_dir: Temporary directory management -
mock_model: Mock PyTorch model for testing -
mock_optimizer: Mock optimizer -
mock_dataloader: Mock data loader with sample data -
sample_checkpoint_data: Sample checkpoint structure -
mock_args: Mock command-line arguments -
cuda_available: CUDA availability check -
small_model: Simple neural network for testing -
reset_random_seeds: Reproducible random seeds -
capture_stdout: Output capture utilities -
mock_time: Time mocking for consistent measurements
Additional Updates
-
Code Fixes: Updated deprecated
async=Truetonon_blocking=Trueinutils.pyfor Python 3.8+ compatibility - .gitignore Updates: Added entries for testing artifacts, Claude settings, and common development files
Usage Instructions
Install Dependencies
poetry install
Run Tests
Two equivalent commands are available:
poetry run test # Run all tests with coverage
poetry run tests # Alternative command
Run Tests Without Coverage
poetry run test --no-cov
Run Specific Test Markers
poetry run test -m unit # Run only unit tests
poetry run test -m integration # Run only integration tests
poetry run test -m "not slow" # Skip slow tests
Run Specific Test Files
poetry run test tests/test_infrastructure_validation.py
Generate Coverage Reports
After running tests, coverage reports are available in:
- Terminal output (with missing lines)
-
htmlcov/index.html(HTML report) -
coverage.xml(XML report for CI/CD)
Notes
- The testing infrastructure is now ready for developers to add unit and integration tests
- Coverage threshold is currently set to 20% to allow for gradual test implementation
- All pytest standard options are available through the Poetry commands
- The validation test file demonstrates various testing patterns and fixture usage