TopicalChange icon indicating copy to clipboard operation
TopicalChange copied to clipboard

feat: Set up comprehensive Python testing infrastructure with Poetry

Open llbbl opened this issue 4 months ago • 0 comments

Python Testing Infrastructure Setup

Summary

This PR establishes a comprehensive testing infrastructure for the Python project, migrating from a simple requirements.txt to a modern Poetry-based dependency management system with full pytest support.

Changes Made

Package Management

  • Poetry Configuration: Set up Poetry as the primary package manager
  • Dependency Migration: Migrated all dependencies from requirements.txt to pyproject.toml
  • Updated Versions: Updated package versions to ensure Python 3.11 compatibility
  • Lock File: Generated poetry.lock for reproducible builds

Testing Framework

  • pytest: Configured as the primary testing framework
  • Coverage: Set up pytest-cov for code coverage reporting
  • Mocking: Added pytest-mock for test mocking utilities
  • Parallel Testing: Included pytest-xdist for parallel test execution
  • Timeout Support: Added pytest-timeout for test timeout management

Directory Structure

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

Configuration (pyproject.toml)

  • pytest Settings:

    • Test discovery patterns configured
    • Coverage thresholds and reporting formats
    • Custom markers: unit, integration, slow, skip_ci
    • Strict mode enabled for better error detection
  • Coverage Settings:

    • Source directories: baselines, dataset, our_model
    • HTML and XML report generation
    • Branch coverage enabled
    • Exclusion patterns for non-testable code

Development Tools

  • Black: Code formatter (line length: 88)
  • isort: Import sorter (compatible with Black)
  • flake8: Linting tool
  • mypy: Type checking support

Shared Fixtures (conftest.py)

  • temp_dir: Temporary directory management
  • sample_text_file, sample_csv_file, sample_json_file: Test file generation
  • mock_config: Configuration mocking
  • mock_model: Model object mocking
  • capture_logs: Log capture during tests
  • mock_requests: HTTP request mocking
  • benchmark_timer: Simple performance timing

How to Use

Installation

# Install all dependencies including dev dependencies
poetry install

Running Tests

# Run all tests with coverage
poetry run test

# Or use the alternative command
poetry run tests

# Run specific test categories
poetry run pytest -m unit        # Run only unit tests
poetry run pytest -m integration # Run only integration tests
poetry run pytest -m "not slow"  # Skip slow tests

# Run tests in parallel
poetry run pytest -n auto

# Run with different verbosity
poetry run pytest -v   # Verbose
poetry run pytest -q   # Quiet

Coverage Reports

After running tests, coverage reports are available in:

  • Terminal: Displayed automatically after test run
  • HTML Report: htmlcov/index.html
  • XML Report: coverage.xml (for CI/CD integration)

Writing New Tests

  1. Place unit tests in tests/unit/
  2. Place integration tests in tests/integration/
  3. Use shared fixtures from conftest.py
  4. Mark tests appropriately:
    @pytest.mark.unit
    def test_example():
        pass
    
    @pytest.mark.slow
    def test_slow_operation():
        pass
    

Validation

The infrastructure has been validated with 16 passing tests that verify:

  • Python version compatibility
  • Project structure integrity
  • Test directory organization
  • Fixture availability and functionality
  • Configuration correctness
  • Marker definitions
  • Coverage setup

Notes

  • The poetry.lock file should be committed to ensure reproducible builds
  • Coverage threshold is currently set to 0% to allow gradual test addition
  • All test tools and dependencies are isolated in the development group
  • The .gitignore has been updated to exclude all testing artifacts

Next Steps

Developers can now immediately start writing tests for the codebase modules using this infrastructure. The validation tests serve as examples of how to use the fixtures and test patterns.

llbbl avatar Aug 22 '25 23:08 llbbl