lightweight_mmm icon indicating copy to clipboard operation
lightweight_mmm 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 lightweight_mmm project using Poetry as the package manager and pytest as the testing framework.

Changes Made

Package Management

  • Migrated to Poetry: Created pyproject.toml with complete project configuration
  • Dependency migration: Transferred all dependencies from requirements.txt and setup.py to Poetry
  • Development dependencies: Added pytest, pytest-cov, and pytest-mock as dev dependencies

Testing Configuration

  • pytest configuration:
    • Set up test discovery patterns
    • Configured coverage reporting with 80% threshold
    • Added custom markers: unit, integration, and slow
    • Configured output formatting and strict options
  • Coverage settings:
    • HTML and XML report generation
    • Exclusion patterns for non-production code
    • Branch coverage enabled

Directory Structure

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

Shared Fixtures (conftest.py)

  • temp_dir: Temporary directory for test files
  • mock_data: Synthetic MMM data for testing
  • mock_config: Configuration dictionary
  • sample_media_data: Sample media spend data
  • sample_target_data: Sample sales data
  • mock_model_params: Model parameters for testing
  • reset_random_seed: Ensures reproducible tests
  • capture_logs: Log capturing fixture

Development Commands

  • poetry run test - Run all tests
  • poetry run tests - Alternative command (both work)
  • All standard pytest options are available

Running Tests

  1. Install dependencies:

    poetry install
    
  2. Run all tests:

    poetry run test
    
  3. Run with specific markers:

    poetry run pytest -m unit  # Unit tests only
    poetry run pytest -m "not slow"  # Skip slow tests
    
  4. Run with coverage report:

    poetry run pytest --cov-report=html
    

Notes

  • The infrastructure is ready for developers to start writing tests immediately
  • Coverage threshold is set to 80% to encourage comprehensive testing
  • The validation test file (test_setup_validation.py) verifies the infrastructure works correctly
  • Poetry lock file is NOT gitignored to ensure reproducible builds
  • The existing test files in the lightweight_mmm package remain unchanged

llbbl avatar Jun 20 '25 18:06 llbbl