ml_code icon indicating copy to clipboard operation
ml_code copied to clipboard

feat: Set up comprehensive Python testing infrastructure

Open llbbl opened this issue 3 months ago • 0 comments

Set up Python Testing Infrastructure

Summary

This PR establishes a comprehensive testing infrastructure for the Python machine learning algorithms project. The setup provides a ready-to-use testing environment where developers can immediately start writing and running tests.

Changes Made

Package Management

  • Set up Poetry as the default package manager with pyproject.toml configuration
  • Python version: Configured for Python >=3.8,<3.12 for optimal compatibility
  • Dependencies: Added core ML dependencies (numpy, pandas, matplotlib, scikit-learn, tensorflow, opencv-python, nltk)

Testing Dependencies

  • pytest - Main testing framework with strict configuration
  • pytest-cov - Coverage reporting with 80% threshold requirement
  • pytest-mock - Mocking utilities for test isolation

Testing Configuration

  • Test discovery: Configured patterns for test_*.py and *_test.py files
  • Coverage reporting: HTML, XML, and terminal output formats
  • Custom markers: unit, integration, slow for test categorization
  • Strict mode: Enabled strict markers and config validation

Directory Structure

tests/
├── __init__.py
├── conftest.py          # Shared fixtures and test utilities
├── unit/
│   └── __init__.py
├── integration/  
│   └── __init__.py
└── test_infrastructure.py  # Validation tests

Shared Test Fixtures

The conftest.py includes commonly needed fixtures:

  • temp_dir - Temporary directory for file operations
  • mock_config - Mock configuration objects
  • sample_data - Sample training datasets
  • sample_regression_data - Regression test data
  • mock_model - Mock ML model for testing
  • sample_image - Computer vision test data
  • sample_text_data - NLP test samples

Updated Configuration

  • .gitignore: Added testing artifacts, Claude Code settings, and IDE files
  • Coverage exclusions: Proper exclusions for test files and common patterns

Running Tests

Basic Commands

# Install dependencies
poetry install

# Run all tests
poetry run pytest

# Run with coverage
poetry run pytest --cov

# Run specific test categories
poetry run pytest -m unit
poetry run pytest -m integration  
poetry run pytest -m slow

Advanced Usage

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

# Run with verbose output
poetry run pytest -v

# Skip coverage for faster runs
poetry run pytest --no-cov

# Generate HTML coverage report
poetry run pytest --cov --cov-report=html

Validation

The setup includes validation tests in tests/test_infrastructure.py that verify:

  • ✅ pytest is working correctly
  • ✅ Python version compatibility
  • ✅ Project structure exists
  • ✅ Required imports function
  • ✅ Custom markers work properly
  • ✅ Shared fixtures are available
  • ✅ Coverage reporting functions

All validation tests pass, confirming the infrastructure is ready for use.

Notes

  • Poetry lock file: The poetry.lock file is intentionally tracked for reproducible builds
  • Python compatibility: Configured for Python 3.8-3.11 to ensure TensorFlow compatibility
  • Coverage threshold: Set to 80% minimum - can be adjusted in pyproject.toml
  • Dependency versions: Selected stable versions compatible with the existing ML codebase

Next Steps

Developers can now:

  1. Run poetry install to set up the environment
  2. Write tests in the tests/unit/ and tests/integration/ directories
  3. Use the shared fixtures in conftest.py for common test setup
  4. Execute tests with poetry run pytest and view coverage reports

The testing infrastructure is ready for immediate use with no additional setup required.

llbbl avatar Sep 01 '25 20:09 llbbl