pytorch_active_learning
pytorch_active_learning copied to clipboard
feat: Add complete Python testing infrastructure with Poetry
Add Complete Python Testing Infrastructure
Summary
This PR sets up a comprehensive testing infrastructure for the Python active learning project using Poetry as the package manager and pytest as the testing framework. The setup provides a ready-to-use testing environment where developers can immediately start writing tests.
Changes Made
Package Management
- Poetry Setup: Created
pyproject.tomlwith Poetry configuration as the project's package manager - Dependency Migration: Migrated existing dependencies from
requirements.txtto Poetry - Development Dependencies: Added pytest, pytest-cov, and pytest-mock as development dependencies
Testing Configuration
-
pytest Configuration:
- Set up test discovery patterns for
test_*.pyand*_test.pyfiles - Configured coverage reporting with 80% threshold
- Added HTML and XML coverage report generation
- Enabled strict markers and comprehensive output formatting
- Set up test discovery patterns for
-
Coverage Configuration:
- Configured source directories and exclusion patterns
- Set up coverage thresholds and reporting formats
- Excluded test files and virtual environments from coverage
Project Structure
tests/
├── __init__.py
├── conftest.py # Shared fixtures and configuration
├── test_setup_validation.py # Validation tests
├── unit/
│ └── __init__.py
└── integration/
└── __init__.py
Shared Fixtures (conftest.py)
temp_dir: Creates temporary directories for test filesmock_model: Provides mock PyTorch modelssample_data: Generates sample data for testingmock_dataset: Creates mock datasetscsv_data: Sets up temporary CSV filesmock_config: Provides configuration objectsreset_random_seeds: Ensures reproducible testscapture_stdout: Captures print outputmock_file_operations: Mocks file I/Odevice: Provides PyTorch device configuration
Test Markers
@pytest.mark.unit: For fast, isolated unit tests@pytest.mark.integration: For integration tests with external dependencies@pytest.mark.slow: For slow-running tests
Additional Setup
- Updated
.gitignorewith testing artifacts and Claude settings - Created validation tests to verify the infrastructure works correctly
- Configured Poetry scripts for easy test execution
How to Use
Install Dependencies
poetry install
Run Tests
# Run all tests with coverage
poetry run pytest
# Or use the configured scripts
poetry run test
poetry run tests
# Run specific test types
poetry run pytest -m unit
poetry run pytest -m integration
poetry run pytest -m "not slow"
# Run tests in a specific directory
poetry run pytest tests/unit/
# Run with different verbosity
poetry run pytest -v # verbose
poetry run pytest -q # quiet
View Coverage Reports
- Terminal: Coverage is displayed after each test run
- HTML Report: Open
htmlcov/index.htmlin a browser - XML Report: Available at
coverage.xmlfor CI integration
Notes
- The existing Python scripts contain module-level code that executes on import, which is why they're excluded from coverage requirements
- The 80% coverage threshold applies to new test code added to the project
- All pytest standard options are available when running tests
- The infrastructure is designed to support both unit and integration testing patterns
Next Steps
Developers can now:
- Write unit tests in
tests/unit/ - Write integration tests in
tests/integration/ - Use the provided fixtures for common testing patterns
- Run tests with coverage reporting to ensure code quality