DAM
DAM copied to clipboard
feat: Add comprehensive Python testing infrastructure with Poetry
Add Python Testing Infrastructure
Summary
This PR establishes a comprehensive testing infrastructure for the Python 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 coverage reporting, mocking capabilities, and organized test structure.
Changes Made
Package Management
- Poetry Setup: Configured Poetry as the package manager with
pyproject.toml - Dependency Migration: Migrated existing dependencies to Poetry format
- Lock File: Generated
poetry.lockfor reproducible installations
Testing Framework
- pytest: Main testing framework with comprehensive configuration
- pytest-cov: Coverage reporting with HTML and XML output formats
- pytest-mock: Mocking utilities for unit testing
Testing Configuration (pyproject.toml)
- pytest settings: Test discovery patterns, output formatting, and strict options
- Coverage settings:
- Source directories configuration
- Exclusion patterns for non-testable code
- HTML and XML report generation
- Coverage thresholds (configurable, currently set to 80%)
- Custom markers:
unit,integration, andslowfor test categorization
Project Structure
tests/
├── __init__.py
├── conftest.py # Shared fixtures
├── test_setup_validation.py # Infrastructure validation
├── unit/
│ └── __init__.py
└── integration/
└── __init__.py
Shared Fixtures (conftest.py)
temp_dir: Temporary directory managementsample_config: Mock configuration dictionarymock_config_file: YAML config file fixturesample_image_tensor: PyTorch tensor fixturessample_batch: Batch data fixturessample_keypoints: Keypoint data fixturesmock_checkpoint: Model checkpoint fixturesample_video_frames: Video frame data fixturemock_dataset_path: Dataset directory structuremock_logger: Logging fixturereset_random_seeds: Reproducible random seeds- Device and GPU availability fixtures
Development Tools
- Makefile: Convenient commands for common tasks
make install: Install dependenciesmake test/make tests: Run all testsmake test-unit: Run unit tests onlymake test-integration: Run integration tests onlymake coverage: Generate coverage reportmake lint: Run code lintingmake format: Format code with black and isortmake typecheck: Run type checkingmake clean: Clean cache and build files
Additional Configuration
- Code formatting: Black and isort configuration
- Type checking: mypy configuration
- Linting: flake8 configuration
- .gitignore: Updated with testing artifacts and Poetry files
How to Use
Installation
# Install Poetry (if not already installed)
curl -sSL https://install.python-poetry.org | python3 -
# Install project dependencies
poetry install --no-root
# Or using make
make install
Running Tests
# Run all tests
poetry run pytest
# or
make test
# Run specific test categories
poetry run pytest -m unit # Unit tests only
poetry run pytest -m integration # Integration tests only
poetry run pytest -m slow # Slow tests only
# Run with coverage
poetry run pytest --cov=. --cov-report=html
# Generate coverage report
make coverage
Writing Tests
- Place unit tests in
tests/unit/ - Place integration tests in
tests/integration/ - Use fixtures from
conftest.pyfor common test data - Mark tests appropriately:
@pytest.mark.unit def test_my_unit_test(): assert True @pytest.mark.integration def test_my_integration_test(): assert True
Validation
The setup includes test_setup_validation.py which verifies:
- Python version compatibility (3.8+)
- All testing packages are installed correctly
- All fixtures work as expected
- Project imports are accessible
- Test markers are properly configured
Run validation: poetry run pytest tests/test_setup_validation.py -v
Dependencies Added
Testing Dependencies (dev)
- pytest ^7.4.0
- pytest-cov ^4.1.0
- pytest-mock ^3.11.1
Code Quality Tools (dev)
- black ^23.7.0
- flake8 ^6.0.0
- mypy ^1.4.0
- isort ^5.12.0
Notes
- The testing infrastructure is ready for immediate use
- No actual unit tests for the codebase were written - only infrastructure setup
- Coverage is configured but will show 0% until actual tests are added
- All tools are configured with sensible defaults that can be customized as needed
- The setup supports both local development and CI/CD integration