swa icon indicating copy to clipboard operation
swa copied to clipboard

feat: Set up complete Python testing infrastructure with Poetry

Open llbbl opened this issue 8 months ago • 2 comments

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.toml with 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, and train modules
    • Branch coverage enabled
    • Exclusion patterns for test files and virtual environments
    • Coverage threshold set to 20% (adjustable based on project needs)

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=True to non_blocking=True in utils.py for 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

llbbl avatar Jun 17 '25 17:06 llbbl