pytorch-pruning icon indicating copy to clipboard operation
pytorch-pruning copied to clipboard

feat: Add comprehensive Python testing infrastructure with Poetry

Open llbbl opened this issue 5 months ago • 0 comments

Add Python Testing Infrastructure

Summary

This PR sets up a comprehensive testing infrastructure for the PyTorch pruning 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 organization.

Changes Made

Package Management

  • Poetry Setup: Created pyproject.toml with Poetry configuration as the primary package manager
  • Dependencies Migration: Migrated PyTorch and related dependencies to Poetry's dependency management
  • Development Dependencies: Added pytest, pytest-cov, and pytest-mock as development dependencies

Testing Configuration

  • pytest Configuration: Configured pytest in pyproject.toml with:

    • Test discovery patterns for finding test files
    • Coverage reporting with HTML and XML output formats
    • Strict configuration options for reliable test runs
    • Custom markers for categorizing tests (unit, integration, slow)
  • Coverage Configuration: Set up coverage.py with:

    • Source directory configuration
    • Exclusion patterns for non-test code
    • Multiple report formats (terminal, HTML, XML)

Directory Structure

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

Fixtures and Utilities

Created comprehensive fixtures in conftest.py:

  • temp_dir: Temporary directory management
  • mock_model: Mock PyTorch model for testing
  • sample_tensor: Sample tensor generation
  • sample_batch: Batch data generation
  • mock_dataset: Mock dataset for testing
  • mock_dataloader: Mock dataloader
  • device: Device selection (CPU/CUDA)
  • random_seed: Reproducibility fixture
  • mock_vgg_model: Mock VGG model
  • sample_checkpoint: Checkpoint file creation
  • cleanup_cuda_memory: CUDA memory cleanup
  • capture_stdout: Output capture for testing

Development Commands

Configured Poetry scripts for running tests:

poetry run test   # Run all tests
poetry run tests  # Alternative command (both work)

Both commands support all standard pytest options.

Additional Setup

  • Updated .gitignore with comprehensive Python and testing-related entries
  • Added validation tests to verify the infrastructure works correctly
  • Configured coverage to temporarily allow 0% threshold (should be updated when actual tests are written)

How to Use

  1. Install dependencies:

    poetry install
    
  2. Run tests:

    poetry run test
    # or
    poetry run tests
    
  3. Run specific test categories:

    poetry run pytest -m unit        # Run only unit tests
    poetry run pytest -m integration # Run only integration tests
    poetry run pytest -m "not slow"  # Skip slow tests
    
  4. View coverage reports:

    • Terminal: Included in test output
    • HTML: Open htmlcov/index.html in a browser
    • XML: Available at coverage.xml for CI integration

Notes

  • The coverage threshold is currently set to 0% to allow the infrastructure setup to pass. This should be increased (e.g., to 80%) once actual tests are written.
  • One validation test fails due to OpenCV requiring system libraries (libGL.so.1). This is expected in headless environments and doesn't affect the testing infrastructure itself.
  • The project uses Poetry's lock file (poetry.lock) which is committed to ensure reproducible builds across environments.
  • All testing tools and configurations follow Python testing best practices and are ready for immediate use.

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 performance benchmarks using the slow marker
  4. Integrate with CI/CD pipelines using the coverage reports

llbbl avatar Jun 28 '25 00:06 llbbl