rcg icon indicating copy to clipboard operation
rcg copied to clipboard

feat: Set up comprehensive Python testing infrastructure with Poetry

Open llbbl opened this issue 5 months ago • 0 comments

Set Up Python Testing Infrastructure

Summary

This PR establishes a comprehensive testing infrastructure for the RCG (Representation-Conditioned image Generation) project using Poetry as the package manager and pytest as the testing framework.

Changes Made

Package Management

  • Migrated to Poetry: Created pyproject.toml with Poetry configuration
  • Dependency Migration: Transferred all dependencies from setup.py and environment.yaml
  • Lock File: Generated poetry.lock to ensure reproducible builds

Testing Framework

  • pytest: Main testing framework with comprehensive configuration
  • pytest-cov: Coverage reporting with HTML and XML outputs
  • pytest-mock: Mocking utilities for unit tests
  • pytest-xdist: Parallel test execution support
  • pytest-timeout: Test timeout management

Testing Configuration

  • Coverage Settings: Configured with detailed exclusions and reporting formats
  • Test Markers: Added unit, integration, and slow markers for test categorization
  • Strict Mode: Enabled strict markers and configuration for better test quality
  • Coverage Reports: HTML reports in htmlcov/ and XML in coverage.xml

Directory Structure

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

Shared Fixtures (conftest.py)

  • temp_dir: Temporary directory for test files
  • mock_config: Mock configuration using OmegaConf
  • sample_tensor: Sample PyTorch tensors
  • sample_batch: Sample data batches
  • numpy_random_state: Reproducible numpy random state
  • torch_random_state: Reproducible PyTorch random state
  • mock_model_checkpoint: Mock checkpoint files
  • mock_image_file: Mock image files for testing
  • device: Automatic CPU/GPU device selection
  • cleanup_cuda: Automatic CUDA cache cleanup
  • mock_environment_variables: Temporary environment variable management
  • capture_logs: Log capture for assertions

Additional Tools

  • black: Code formatting (configured)
  • isort: Import sorting (configured)
  • flake8: Linting
  • mypy: Type checking (configured)

Updated Files

  • .gitignore: Added testing, coverage, Claude settings, and build artifact entries
  • Removed old setup.py in favor of Poetry

How to Use

Installation

# Install Poetry (if not already installed)
curl -sSL https://install.python-poetry.org | python3 -

# Install dependencies
poetry install

Running Tests

# Run all tests
poetry run test

# Alternative command (both work)
poetry run tests

# Run specific test file
poetry run pytest tests/test_file.py

# Run tests with specific markers
poetry run pytest -m unit           # Only unit tests
poetry run pytest -m "not slow"     # Skip slow tests
poetry run pytest -m integration    # Only integration tests

# Run tests in parallel
poetry run pytest -n auto

# Run with verbose output
poetry run pytest -v

Coverage Reports

  • Terminal: Coverage summary shown after each test run
  • HTML Report: Open htmlcov/index.html in a browser
  • XML Report: coverage.xml for CI/CD integration

Writing Tests

  1. Place unit tests in tests/unit/
  2. Place integration tests in tests/integration/
  3. Use fixtures from conftest.py for common test needs
  4. Mark tests appropriately (@pytest.mark.unit, @pytest.mark.slow, etc.)

Notes

  • Coverage threshold is currently disabled in the configuration. Enable it by uncommenting --cov-fail-under=80 in pyproject.toml once tests are added
  • The validation test file (test_setup_validation.py) verifies the infrastructure is working correctly
  • All testing dependencies are in the dev group and won't be installed in production

Next Steps

  1. Write unit tests for existing modules
  2. Add integration tests for key workflows
  3. Enable coverage threshold once baseline coverage is established
  4. Consider adding pre-commit hooks for automated testing

llbbl avatar Jun 27 '25 19:06 llbbl