speechmetrics icon indicating copy to clipboard operation
speechmetrics copied to clipboard

feat: Set up comprehensive Python testing infrastructure with Poetry

Open llbbl opened this issue 8 months ago • 0 comments

Set Up Python Testing Infrastructure

Summary

This PR establishes a comprehensive testing infrastructure for the speechmetrics project, migrating from setup.py to Poetry for modern dependency management and adding pytest with coverage reporting.

Changes Made

Package Management Migration

  • Created pyproject.toml with Poetry configuration
  • Migrated all dependencies from setup.py to Poetry format
  • Added development dependencies group for testing tools

Testing Setup

  • Testing Framework: pytest (^7.4.0)
  • Coverage Tool: pytest-cov (^4.1.0)
  • Mocking Library: pytest-mock (^3.11.0)

Configuration

  • pytest configuration in pyproject.toml:

    • Automatic test discovery in tests/ directory
    • Coverage reporting with 80% threshold
    • Multiple output formats (terminal, HTML, XML)
    • Custom markers: unit, integration, slow
    • Strict marker enforcement
  • Coverage configuration:

    • Source directory: speechmetrics
    • Excluded: test files, __init__.py, setup files
    • Detailed branch coverage reporting

Directory Structure

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

Shared Fixtures (conftest.py)

  • temp_dir: Temporary directory management
  • sample_audio_data: Generate test audio arrays
  • sample_rate: Standard 16kHz sample rate
  • mock_config: Configuration dictionary for testing
  • example_wav_file: Create temporary WAV files
  • reset_environment: Environment variable isolation
  • mock_tensorflow: TensorFlow mocking for faster tests
  • noisy_audio_pair: Clean/noisy audio pair generation

Poetry Scripts

  • poetry run test - Run all tests
  • poetry run tests - Alternative command (both work)

.gitignore Updates

  • Added testing artifacts: .pytest_cache/, .coverage, htmlcov/, coverage.xml
  • Added Claude settings: .claude/*
  • Added IDE files and virtual environments
  • Note: poetry.lock is intentionally NOT ignored (should be committed)

How to Use

  1. Install Poetry (if not already installed):

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

    poetry install --with dev
    
  3. Run tests:

    poetry run test
    # or
    poetry run tests
    
  4. Run specific test types:

    poetry run pytest -m unit        # Unit tests only
    poetry run pytest -m integration # Integration tests only
    poetry run pytest -m "not slow"  # Exclude slow tests
    
  5. View coverage report:

    # Terminal report is shown automatically
    # HTML report available at: htmlcov/index.html
    

Notes

  • The infrastructure is ready for developers to start writing tests immediately
  • All pytest standard options are available through the Poetry scripts
  • Coverage threshold is set to 80% - builds will fail if coverage drops below this
  • Validation tests are included to verify the setup works correctly
  • Git dependencies in pyproject.toml may need adjustment based on the specific versions required

Next Steps

Developers can now:

  1. Write unit tests in tests/unit/
  2. Write integration tests in tests/integration/
  3. Use the provided fixtures for common testing scenarios
  4. Run tests locally before committing changes

llbbl avatar Jun 20 '25 18:06 llbbl