OpenOOD
OpenOOD copied to clipboard
feat: Add comprehensive Python testing infrastructure with Poetry
Add Python Testing Infrastructure
Summary
This PR establishes a comprehensive testing infrastructure for the OpenOOD project using Poetry as the package manager and pytest as the testing framework. The setup provides a solid foundation for writing and running tests with proper coverage reporting and development tooling.
Changes Made
Package Management
- Poetry Setup: Created
pyproject.tomlwith Poetry configuration - Dependency Migration: Migrated all dependencies from
setup.pyto Poetry - Lock File: Generated
poetry.lockfor reproducible builds - Compatibility Fix: Replaced
faiss-gpuwithfaiss-cpufor better cross-platform compatibility
Testing Framework
- Testing Dependencies: Added pytest (^7.4.0), pytest-cov (^4.1.0), and pytest-mock (^3.11.1)
- Coverage Configuration:
- Configured coverage reporting with HTML, XML, and terminal output
- Set coverage threshold to 0% temporarily (TODO: increase to 80% once tests are written)
- Excluded test files and init.py from coverage
- Test Discovery: Configured proper test discovery patterns
- Custom Markers: Added unit, integration, and slow test markers
Project Structure
tests/
├── __init__.py
├── conftest.py # Shared pytest fixtures
├── test_infrastructure_validation.py # Validation tests
├── unit/
│ └── __init__.py
└── integration/
└── __init__.py
Fixtures (conftest.py)
Created comprehensive fixtures for common testing needs:
temp_dir: Temporary directory managementmock_config: Configuration dictionariessample_tensor,sample_labels,sample_features: PyTorch tensorsmock_model: Simple neural network for testingyaml_config_file,json_config_file: Config file generationmock_checkpoint: Model checkpoint filesmock_dataloader: DataLoader instancessample_ood_scores: OOD detection scores- And many more...
Development Experience
- Poetry Scripts: Added both
poetry run testandpoetry run testscommands - Git Ignore: Updated
.gitignorewith Claude settings and clarified thatpoetry.lockshould be committed - Validation Tests: Created comprehensive validation tests to verify the infrastructure setup
Testing Instructions
-
Install dependencies:
poetry install -
Run validation tests:
poetry run pytest tests/test_infrastructure_validation.py -v -
Run all tests with coverage:
poetry run test # or poetry run tests -
Run specific test types:
# Unit tests only poetry run pytest -m unit # Integration tests only poetry run pytest -m integration # Exclude slow tests poetry run pytest -m "not slow" -
View coverage reports:
- Terminal: Automatically shown when running tests
- HTML: Open
htmlcov/index.htmlin a browser - XML: Available at
coverage.xmlfor CI integration
Next Steps
- Write Unit Tests: Start adding unit tests for individual components in
tests/unit/ - Write Integration Tests: Add integration tests in
tests/integration/ - Increase Coverage Threshold: Once tests are written, update
--cov-fail-underfrom 0 to 80 inpyproject.toml - CI Integration: Use the generated
coverage.xmlfor CI coverage reporting
Notes
- The infrastructure is fully set up and validated - developers can immediately start writing tests
- All pytest features are available including fixtures, markers, parametrization, etc.
- Coverage is configured but threshold is set to 0% until actual tests are written
- The validation test file demonstrates proper test structure and fixture usage