MixNMatch icon indicating copy to clipboard operation
MixNMatch copied to clipboard

feat: Set up comprehensive 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 comprehensive testing infrastructure for the Python project using Poetry as the package manager and pytest as the testing framework. The setup provides a solid foundation for writing and running tests with proper coverage reporting and organization.

Changes Made

Package Management

  • Poetry Configuration: Set up Poetry as the package manager with a complete pyproject.toml configuration
  • No Migration Needed: No existing dependencies found in requirements.txt or setup.py

Testing Framework

  • Dependencies Added:
    • pytest (^7.4.0) - Core testing framework
    • pytest-cov (^4.1.0) - Coverage reporting plugin
    • pytest-mock (^3.11.0) - Mocking utilities

Configuration

  • pytest Settings:

    • Test discovery patterns for test_*.py and *_test.py files
    • Coverage reporting in HTML, XML, and terminal formats
    • Custom markers: unit, integration, and slow
    • Strict marker enforcement
    • Verbose output by default
  • Coverage Settings:

    • Source directory: code/
    • Excluded: tests, cache, virtual environments, migrations
    • 80% coverage threshold (informational only)
    • HTML reports in htmlcov/ directory
    • XML reports as coverage.xml

Project Structure

tests/
├── __init__.py
├── conftest.py          # Shared fixtures and configuration
├── unit/                # Unit tests directory
│   └── __init__.py
├── integration/         # Integration tests directory
│   └── __init__.py
└── test_setup_validation.py  # Validation tests

Fixtures (conftest.py)

  • temp_dir: Temporary directory with automatic cleanup
  • mock_config: Mock configuration object
  • sample_data: Sample data for testing
  • mock_model: Mock ML model with predict/train/evaluate methods
  • test_image_path: Creates temporary test image files
  • json_file: Helper for creating temporary JSON files
  • capture_logs: Log message capture utility
  • reset_environment: Auto-reset environment variables
  • mock_file_operations: Mock file system operations

.gitignore Updates

  • Python artifacts: __pycache__/, *.pyc, virtual environments
  • Testing artifacts: .pytest_cache/, .coverage, htmlcov/, coverage.xml
  • Claude artifacts: .claude/*
  • IDE and OS files

Running Tests

Basic Commands

# Install dependencies
poetry install

# Run all tests
poetry run test
# or
poetry run tests

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

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

# Run with coverage only
poetry run pytest --cov=code --cov-report=html

Validation Results

All 10 validation tests pass successfully:

  • ✅ pytest installation verified
  • ✅ Project structure validated
  • ✅ pyproject.toml configuration checked
  • ✅ Custom markers functional
  • ✅ Fixtures available and working
  • ✅ Coverage tools configured
  • ✅ Mock utilities available
  • ✅ Code package importable
  • ✅ Integration test setup verified
  • ✅ Slow marker functional

Notes

  • The coverage warning about "no data collected" is expected since we haven't written actual tests for the codebase yet
  • Both poetry run test and poetry run tests commands are configured and working
  • The poetry.lock file is NOT gitignored as per best practices
  • The 80% coverage threshold is set in the configuration but won't fail builds (removed --cov-fail-under from pytest args)

Next Steps

Developers can now immediately start writing tests by:

  1. Creating test files in tests/unit/ or tests/integration/
  2. Using the provided fixtures from conftest.py
  3. Running tests with poetry run test
  4. Viewing coverage reports in htmlcov/index.html

llbbl avatar Jun 17 '25 15:06 llbbl