Skip to content

Conversation

@llbbl
Copy link

@llbbl llbbl commented Jun 26, 2025

Add Python Testing Infrastructure

Summary

This PR sets up a complete testing infrastructure for the PyTorch Nested U-Net project using Poetry as the package manager and pytest as the testing framework.

Changes Made

Package Management

  • Poetry Setup: Configured Poetry as the project's package manager via pyproject.toml
  • Dependency Migration: Migrated all identified dependencies from the codebase analysis (torch, numpy, opencv-python, pandas, PyYAML, scikit-learn, albumentations, tqdm)
  • Development Dependencies: Added pytest, pytest-cov, and pytest-mock as development dependencies

Testing Configuration

  • pytest Configuration: Set up comprehensive pytest configuration in pyproject.toml including:

    • Test discovery patterns for test_*.py and *_test.py files
    • Coverage reporting with HTML and XML output formats
    • Custom markers for unit, integration, and slow tests
    • Strict marker enforcement and verbose output
  • Coverage Configuration: Configured coverage.py with:

    • Source directory tracking
    • Exclusion patterns for test files, virtual environments, and build artifacts
    • Detailed reporting with line-level precision

Directory Structure

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

Shared Fixtures (conftest.py)

Created comprehensive fixtures for common testing needs:

  • temp_dir: Temporary directory management
  • sample_config: Configuration dictionary for testing
  • config_file: YAML configuration file fixture
  • sample_image / sample_mask: NumPy array fixtures
  • sample_tensor_image / sample_tensor_mask: PyTorch tensor fixtures
  • sample_batch: Batch data fixture
  • mock_model: Simple neural network for testing
  • device: GPU/CPU device detection
  • reset_random_seeds: Reproducible test runs
  • sample_dataset_dir: Mock dataset directory structure
  • Additional utility fixtures for stdout capture and mock dataloaders

Development Scripts

Added Poetry scripts for test execution:

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

Additional Setup

  • Updated .gitignore with testing-related patterns and Claude settings
  • Created validation tests to verify the infrastructure works correctly

Instructions for Running Tests

  1. Install Poetry (if not already installed):

    curl -sSL https://install.python-poetry.org | python3 -
  2. Install dependencies:

    poetry install
  3. Run tests:

    poetry run test
    # or
    poetry run tests
  4. Run specific test files:

    poetry run pytest tests/test_infrastructure.py
  5. Run tests with specific markers:

    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
  6. View coverage report:

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

Notes

  • The infrastructure is set up but does not include actual unit tests for the codebase
  • Coverage threshold is currently set to 0% to allow the infrastructure validation tests to pass
  • The project dependencies were inferred from import statements as the original requirements.txt only listed albumentations
  • One validation test may fail due to missing system library (libGL.so.1) which is an environment issue, not a testing infrastructure issue

Next Steps

With this infrastructure in place, developers can now:

  1. Write unit tests in the tests/unit/ directory
  2. Write integration tests in the tests/integration/ directory
  3. Use the provided fixtures for common testing scenarios
  4. Adjust the coverage threshold in pyproject.toml as more tests are added

- Add Poetry as package manager with pyproject.toml configuration
- Configure pytest with coverage reporting and custom markers
- Create test directory structure with comprehensive fixtures
- Add testing scripts for easy test execution
- Update .gitignore with testing and Claude-specific entries
- Add validation tests to verify infrastructure setup
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant