pytorch-pruning
pytorch-pruning copied to clipboard
feat: Add comprehensive Python testing infrastructure with Poetry
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.tomlwith 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.tomlwith:- 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 managementmock_model: Mock PyTorch model for testingsample_tensor: Sample tensor generationsample_batch: Batch data generationmock_dataset: Mock dataset for testingmock_dataloader: Mock dataloaderdevice: Device selection (CPU/CUDA)random_seed: Reproducibility fixturemock_vgg_model: Mock VGG modelsample_checkpoint: Checkpoint file creationcleanup_cuda_memory: CUDA memory cleanupcapture_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
.gitignorewith 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
-
Install dependencies:
poetry install -
Run tests:
poetry run test # or poetry run tests -
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 -
View coverage reports:
- Terminal: Included in test output
- HTML: Open
htmlcov/index.htmlin a browser - XML: Available at
coverage.xmlfor 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:
- Write unit tests for individual functions and classes
- Create integration tests for end-to-end workflows
- Add performance benchmarks using the
slowmarker - Integrate with CI/CD pipelines using the coverage reports