PMG-Progressive-Multi-Granularity-Training icon indicating copy to clipboard operation
PMG-Progressive-Multi-Granularity-Training copied to clipboard

feat: Add complete Python testing infrastructure with Poetry

Open llbbl opened this issue 5 months ago • 0 comments

Add Complete Python Testing Infrastructure

Summary

This PR sets up a comprehensive testing infrastructure for the ResNet project using Poetry as the package manager and pytest as the testing framework. The setup provides a solid foundation for writing and maintaining tests with proper coverage reporting and test organization.

Changes Made

Package Management

  • Poetry Setup: Created pyproject.toml with Poetry configuration as the project's package manager
  • Dependencies: Added core dependencies (torch, torchvision, pillow, numpy) and development dependencies (pytest, pytest-cov, pytest-mock)
  • Script Commands: Configured both poetry run test and poetry run tests commands for running tests

Testing Configuration

  • pytest Configuration:

    • Set up test discovery patterns for test_*.py and *_test.py files
    • Configured strict markers and error handling
    • Added custom markers: unit, integration, and slow for test categorization
  • Coverage Configuration:

    • Set 80% coverage threshold
    • Configured HTML, XML, and terminal coverage reports
    • Excluded test files, cache directories, and virtual environments from coverage

Directory Structure

tests/
├── __init__.py
├── conftest.py           # Shared pytest fixtures
├── unit/                 # Unit tests directory
│   └── __init__.py
├── integration/          # Integration tests directory
│   └── __init__.py
└── test_setup_validation.py  # Validation tests

Fixtures and Testing Utilities

Created comprehensive fixtures in conftest.py:

  • temp_dir: Temporary directory management
  • sample_image_path: Mock image file creation
  • mock_model_config: Model configuration for testing
  • mock_dataset_config: Dataset configuration for testing
  • device: PyTorch device selection
  • sample_tensor & sample_batch: Test data generation
  • reset_random_seeds: Reproducibility fixture
  • mock_checkpoint: Model checkpoint creation
  • environment_setup: Environment variable management

Additional Files

  • .gitignore: Comprehensive Python gitignore with testing artifacts, IDE files, and project-specific patterns
  • CLAUDE.md: Documentation for Claude AI with testing commands and project structure
  • Validation Tests: Created tests to verify the infrastructure setup works correctly

How to Use

  1. Install dependencies:

    poetry install
    
  2. Run tests:

    # Run all tests
    poetry run test
    
    # Run with coverage
    poetry run pytest --cov=. --cov-report=term --cov-report=html
    
    # Run specific test categories
    poetry run pytest -m unit
    poetry run pytest -m integration
    poetry run pytest -m "not slow"
    
  3. View coverage reports:

    • Terminal: Shown after test run
    • HTML: Open htmlcov/index.html in browser
    • XML: Available at coverage.xml for CI integration

Next Steps

With this infrastructure in place, developers can now:

  1. Write unit tests for individual functions and classes
  2. Create integration tests for end-to-end workflows
  3. Add more specialized fixtures as needed
  4. Integrate with CI/CD pipelines using the coverage reports

Notes

  • Poetry was chosen as the package manager since no existing package manager was detected
  • The 80% coverage threshold is configured but can be adjusted in pyproject.toml
  • All testing dependencies are properly isolated as development dependencies
  • The validation test suite confirms that all components are working correctly

llbbl avatar Jul 01 '25 04:07 llbbl