OpenOOD icon indicating copy to clipboard operation
OpenOOD copied to clipboard

feat: Add comprehensive Python testing infrastructure with Poetry

Open llbbl opened this issue 6 months ago • 0 comments

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.toml with Poetry configuration
  • Dependency Migration: Migrated all dependencies from setup.py to Poetry
  • Lock File: Generated poetry.lock for reproducible builds
  • Compatibility Fix: Replaced faiss-gpu with faiss-cpu for 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 management
  • mock_config: Configuration dictionaries
  • sample_tensor, sample_labels, sample_features: PyTorch tensors
  • mock_model: Simple neural network for testing
  • yaml_config_file, json_config_file: Config file generation
  • mock_checkpoint: Model checkpoint files
  • mock_dataloader: DataLoader instances
  • sample_ood_scores: OOD detection scores
  • And many more...

Development Experience

  • Poetry Scripts: Added both poetry run test and poetry run tests commands
  • Git Ignore: Updated .gitignore with Claude settings and clarified that poetry.lock should be committed
  • Validation Tests: Created comprehensive validation tests to verify the infrastructure setup

Testing Instructions

  1. Install dependencies:

    poetry install
    
  2. Run validation tests:

    poetry run pytest tests/test_infrastructure_validation.py -v
    
  3. Run all tests with coverage:

    poetry run test
    # or
    poetry run tests
    
  4. 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"
    
  5. View coverage reports:

    • Terminal: Automatically shown when running tests
    • HTML: Open htmlcov/index.html in a browser
    • XML: Available at coverage.xml for CI integration

Next Steps

  1. Write Unit Tests: Start adding unit tests for individual components in tests/unit/
  2. Write Integration Tests: Add integration tests in tests/integration/
  3. Increase Coverage Threshold: Once tests are written, update --cov-fail-under from 0 to 80 in pyproject.toml
  4. CI Integration: Use the generated coverage.xml for 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

llbbl avatar Jun 23 '25 01:06 llbbl