awesome-interpretable-machine-learning icon indicating copy to clipboard operation
awesome-interpretable-machine-learning copied to clipboard

feat: set up Python testing infrastructure with Poetry and pytest

Open llbbl opened this issue 5 months ago • 0 comments

Set Up Python Testing Infrastructure

Summary

This PR establishes a complete testing infrastructure for the ML Interpretability Papers project using Poetry as the package manager and pytest as the testing framework.

Changes Made

Package Management

  • Initialized Poetry as the package manager with pyproject.toml configuration
  • Added project metadata and package configuration for the sbin module

Testing Dependencies

Added the following development dependencies:

  • pytest (^8.0.0) - Core testing framework
  • pytest-cov (^5.0.0) - Coverage reporting plugin
  • pytest-mock (^3.14.0) - Mocking utilities

Testing Configuration

  • Configured pytest in pyproject.toml with:
    • Test discovery patterns for test_*.py and *_test.py files
    • Custom markers: unit, integration, and slow
    • Strict mode for markers and configuration
  • Added .coveragerc for detailed coverage configuration:
    • 80% coverage threshold (commented out for initial setup)
    • HTML and XML report generation
    • Exclusion patterns for non-testable code

Directory Structure

Created organized testing structure:

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

Fixtures and Configuration

Added comprehensive fixtures in conftest.py:

  • temp_dir - Temporary directory for testing
  • sample_cache_data - Sample data for cache testing
  • sample_cache_file - Test cache file creation
  • sample_readme_template - Test template file
  • mock_http_response - HTTP response mocking helper
  • mock_env_vars - Environment variable management
  • isolated_filesystem - Isolated test environment

Development Commands

Configured Poetry scripts for consistent test execution:

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

Additional Setup

  • Updated .gitignore with comprehensive Python, testing, and Claude-specific entries
  • Added sbin/__init__.py to make it a proper Python package
  • Created validation tests to verify the infrastructure works correctly

How to Use

  1. Install dependencies:

    poetry install
    
  2. Run tests:

    # Run all tests
    poetry run test
    
    # Run with coverage
    poetry run pytest --cov=sbin --cov-report=html
    
    # Run specific test markers
    poetry run pytest -m unit
    poetry run pytest -m integration
    
  3. Write new tests:

    • Unit tests go in tests/unit/
    • Integration tests go in tests/integration/
    • Use fixtures from conftest.py for common test needs
    • Mark tests appropriately with @pytest.mark.unit, etc.

Notes

  • The coverage threshold is currently commented out in .coveragerc since no actual code tests exist yet
  • The validation tests confirm that all testing infrastructure is properly installed and configured
  • Poetry lock file is tracked in git for reproducible environments
  • All pytest options and plugins are available for use

llbbl avatar Jun 27 '25 19:06 llbbl