segmenter icon indicating copy to clipboard operation
segmenter 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 complete testing infrastructure for the Segmenter project using Poetry as the package manager and pytest as the testing framework. The infrastructure is now ready for developers to immediately start writing unit and integration tests.

Changes Made

Package Management

  • Poetry Setup: Created pyproject.toml with Poetry configuration
  • Dependency Migration: Migrated all dependencies from requirements.txt to Poetry
  • Development Dependencies: Added pytest, pytest-cov, and pytest-mock as dev dependencies

Testing Configuration

  • pytest Configuration:

    • Configured test discovery patterns
    • Set up coverage reporting with 80% threshold
    • Added HTML and XML coverage report generation
    • Created custom test markers: unit, integration, slow
  • Coverage Settings:

    • Source set to segm package
    • Excluded test files, scripts, and __init__.py files from coverage
    • Configured detailed coverage reports with line precision

Directory Structure

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

Test Fixtures (in conftest.py)

  • temp_dir: Temporary directory management
  • mock_config: Configuration dictionary for testing
  • mock_yaml_config: YAML config file generation
  • mock_model: PyTorch model mock
  • mock_dataset & mock_dataloader: Data loading mocks
  • mock_optimizer & mock_scheduler: Training component mocks
  • sample_image & sample_mask: Test tensors
  • device: CUDA/CPU device detection
  • mock_checkpoint: Checkpoint file generation
  • mock_logger: Logging mock
  • capture_stdout: Print statement capture
  • env_vars: Environment variable management

Additional Updates

  • Created segm/__init__.py: Added missing package init file
  • Updated .gitignore: Added testing artifacts, Claude settings, and IDE files

How to Use

Install Dependencies

poetry install

Run Tests

Both commands are available:

poetry run test
poetry run tests

Run Specific Test Categories

# Run only unit tests
poetry run pytest -m unit

# Run only integration tests  
poetry run pytest -m integration

# Exclude slow tests
poetry run pytest -m "not slow"

Generate Coverage Reports

Coverage reports are automatically generated when running tests:

  • Terminal output with missing lines
  • HTML report in htmlcov/ directory
  • XML report as coverage.xml

Notes

  • The project currently has validation tests only. The coverage threshold (80%) will fail until actual unit tests are added.
  • All 18 validation tests pass, confirming the infrastructure is properly set up.
  • Poetry lock file (poetry.lock) should be committed to ensure reproducible builds.
  • The testing infrastructure supports the project's deep learning nature with PyTorch-specific fixtures.

Next Steps

Developers can now:

  1. Write unit tests in tests/unit/
  2. Write integration tests in tests/integration/
  3. Use the provided fixtures in conftest.py for common testing scenarios
  4. Run tests with coverage to ensure code quality

llbbl avatar Jun 28 '25 00:06 llbbl