BackgroundMattingV2-TensorFlow icon indicating copy to clipboard operation
BackgroundMattingV2-TensorFlow copied to clipboard

feat: Set up comprehensive Python testing infrastructure with Poetry

Open llbbl opened this issue 3 months ago • 0 comments

Set Up Python Testing Infrastructure

Summary

This PR establishes a comprehensive testing infrastructure for the Background Matting TensorFlow project, providing a complete foundation for test-driven development and quality assurance.

Changes Made

Package Management

  • Poetry Configuration: Set up Poetry as the primary package manager with pyproject.toml
  • Dependency Management: Migrated to Poetry with TensorFlow and PyTorch dependencies
  • Development Dependencies: Added pytest ecosystem packages (pytest, pytest-cov, pytest-mock)

Testing Structure

  • Directory Structure: Created organized testing directories:
    • tests/ - Root testing directory
    • tests/unit/ - Unit tests for individual components
    • tests/integration/ - Integration tests for component interactions
    • All directories include proper __init__.py files

Testing Configuration

  • pytest Configuration: Comprehensive setup in pyproject.toml including:

    • Test discovery patterns for files, classes, and functions
    • Coverage reporting (HTML, XML, terminal with missing lines)
    • Custom markers: unit, integration, slow
    • Strict configuration with verbose output
    • Warning filters for cleaner test output
  • Coverage Settings:

    • Source code tracking for the model package
    • Exclusion patterns for test files, cache, and common code patterns
    • Coverage threshold set to 10% (adjustable as needed)
    • Multiple report formats (HTML in htmlcov/, XML for CI/CD)

Shared Testing Utilities

  • Comprehensive Fixtures (tests/conftest.py):
    • Temporary directory creation and cleanup
    • TensorFlow tensor generation with standard shapes
    • Mock TensorFlow models and PyTorch weights
    • Configuration dictionaries for testing
    • TensorFlow state management and random seeding
    • Training mode contexts and batch testing utilities

Quality Assurance

  • Infrastructure Validation: Complete test suite (test_infrastructure_validation.py) that verifies:

    • pytest functionality and marker configuration
    • Project structure and file existence
    • Fixture availability and functionality
    • Package imports (TensorFlow, model components)
    • Coverage configuration
    • Environment setup validation
  • Git Configuration: Updated .gitignore with comprehensive exclusions:

    • Testing artifacts (.pytest_cache/, coverage reports)
    • Python build artifacts and virtual environments
    • IDE files and OS-specific files
    • Claude Code settings and TensorFlow/PyTorch model files

Running Tests

Basic Test Execution

# Activate virtual environment
source .venv/bin/activate

# Run all tests
python -m pytest

# Run with verbose output and coverage
python -m pytest -v

# Run specific test types
python -m pytest -m unit        # Unit tests only
python -m pytest -m integration # Integration tests only
python -m pytest -m "not slow"  # Skip slow tests

Alternative Commands

# Using Poetry scripts (when Poetry setup is complete)
poetry run test    # Standard test execution
poetry run tests   # Alternative command

Coverage Reports

  • Terminal: Coverage summary displayed after each test run
  • HTML Report: Detailed coverage report in htmlcov/index.html
  • XML Report: Machine-readable coverage in coverage.xml (for CI/CD)

Development Workflow

  1. Writing Tests: Place tests in appropriate directories:

    • tests/unit/ for component-specific tests
    • tests/integration/ for cross-component tests
  2. Using Fixtures: Leverage shared fixtures from conftest.py:

    • temp_dir - Temporary directory for file operations
    • sample_tensor - Standard TensorFlow tensors
    • mock_tensorflow_model - Mock models for testing
    • config_dict - Standard configuration for model testing
  3. Test Markers: Use markers to categorize tests:

    @pytest.mark.unit
    @pytest.mark.integration  
    @pytest.mark.slow
    

Validation

All infrastructure components have been validated:

  • ✅ 12/12 validation tests passing
  • ✅ Coverage reporting functional
  • ✅ All fixtures working correctly
  • ✅ TensorFlow and model imports successful
  • ✅ Package structure verified

Notes

  • Virtual Environment: Uses standard Python venv due to Poetry CUDA dependency conflicts
  • Dependencies: TensorFlow and PyTorch installed for CPU-only usage (suitable for testing)
  • Coverage Threshold: Set to 10% initially, can be increased as test coverage grows
  • Extensible: Infrastructure designed to easily accommodate additional testing tools and frameworks

This testing infrastructure provides a solid foundation for implementing comprehensive test suites across the Background Matting codebase.

llbbl avatar Sep 04 '25 18:09 llbbl