ConsistentID icon indicating copy to clipboard operation
ConsistentID copied to clipboard

feat: Set up complete Python testing infrastructure with Poetry

Open llbbl opened this issue 6 months ago • 0 comments

Set Up Python Testing Infrastructure

Summary

This PR establishes a complete testing infrastructure for the AI image generation project, migrating from a basic requirements.txt setup to a modern Poetry-based package management system with comprehensive pytest configuration.

Changes Made

Package Management

  • Migrated to Poetry: Converted all 211 dependencies from requirements.txt to pyproject.toml
  • Organized dependencies: Separated development dependencies into a dedicated group
  • Fixed version conflicts: Adjusted numpy version constraint to resolve dependency conflicts

Testing Framework Setup

  • Added testing dependencies:
    • pytest - Core testing framework
    • pytest-cov - Coverage reporting
    • pytest-mock - Mocking utilities
  • Configured pytest in pyproject.toml with:
    • Test discovery patterns
    • Coverage settings (80% threshold)
    • Multiple coverage report formats (terminal, HTML, XML)
    • Custom markers for test organization
    • Strict configuration options

Directory Structure

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

Test Configuration

  • Coverage configuration:
    • Source directories included
    • Exclusion patterns for non-application code
    • 80% coverage threshold with branch coverage
    • HTML reports in htmlcov/, XML in coverage.xml
  • Test markers:
    • @pytest.mark.unit - Unit tests
    • @pytest.mark.integration - Integration tests
    • @pytest.mark.slow - Slow-running tests

Shared Fixtures (conftest.py)

Created comprehensive fixtures for common testing needs:

  • temp_dir - Temporary directory management
  • temp_file - Temporary file creation
  • mock_config - Mock configuration dictionary
  • mock_env_vars - Environment variable mocking
  • sample_image_path - Sample image file creation
  • sample_json_data - Sample JSON structures
  • reset_environment - Auto-cleanup of test environment
  • mock_model_weights - Mock ML model files
  • capture_logs - Log capture and assertion

Poetry Commands

Configured Poetry scripts for easy test execution:

  • poetry run test - Run all tests
  • poetry run tests - Alternative command (both work)
  • Standard pytest options remain available

Additional Files

  • .gitignore: Updated with comprehensive Python testing entries
  • CLAUDE.md: Project memory file with commands and configuration details
  • Validation tests: Created test_setup_validation.py to verify the infrastructure

How to Use

  1. Install Poetry (if not already installed):

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

    poetry install  # Install all dependencies
    # or
    poetry install --only dev  # Install only dev dependencies
    
  3. Run tests:

    poetry run test           # Run all tests
    poetry run pytest -v      # Run with verbose output
    poetry run pytest -m unit # Run only unit tests
    
  4. View coverage reports:

    • Terminal: Automatically shown after test run
    • HTML: Open htmlcov/index.html in a browser
    • XML: Available at coverage.xml for CI integration

Notes

  • The numpy dependency was adjusted to >=1.24.3,<2.0 to resolve version conflicts with other packages
  • Poetry lock file is intentionally not gitignored to ensure reproducible builds
  • The infrastructure is ready for immediate test development - no actual application tests were written as per requirements
  • All validation tests pass, confirming the infrastructure is properly configured

Next Steps

Developers can now:

  1. Write unit tests in tests/unit/
  2. Write integration tests in tests/integration/
  3. Use the provided fixtures from conftest.py
  4. Run tests with coverage reporting
  5. Use markers to organize and selectively run tests

llbbl avatar Jun 24 '25 05:06 llbbl