My-lecture-slides-and-code icon indicating copy to clipboard operation
My-lecture-slides-and-code copied to clipboard

feat: Set up comprehensive Python testing infrastructure with Poetry

Open llbbl opened this issue 9 months ago • 0 comments

Set up Python Testing Infrastructure

Summary

This PR establishes a comprehensive testing infrastructure for the Python project using Poetry as the package manager and pytest as the testing framework. The setup provides a solid foundation for test-driven development with proper configuration, coverage reporting, and shared fixtures.

Changes Made

Package Management

  • Poetry Configuration: Created pyproject.toml with Poetry as the package manager
  • Dependencies Migration: Migrated dependencies from the codebase (pandas, numpy, scikit-learn, matplotlib, xlrd, scipy)
  • Development Dependencies: Added pytest (^7.4.0), pytest-cov (^4.1.0), and pytest-mock (^3.11.0)

Testing Configuration

  • pytest Settings: Configured in pyproject.toml with:
    • Test discovery patterns for test_*.py and *_test.py
    • Coverage settings with 80% threshold
    • HTML and XML coverage report generation
    • Custom markers: unit, integration, slow
    • Strict mode and detailed output options

Directory Structure

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

Shared Fixtures (conftest.py)

  • temp_dir: Temporary directory creation and cleanup
  • sample_csv_file: CSV file generation for testing
  • sample_excel_file: Excel file generation for testing
  • sample_iris_data: Mock iris dataset
  • sample_boston_data: Mock Boston housing dataset
  • mock_sklearn_model: Mock scikit-learn model
  • sample_config: Configuration dictionary
  • capture_stdout: Stdout capture utility
  • mock_file_operations: File operation tracking
  • reset_random_seed: Reproducibility helper

Additional Setup

  • Code Package: Added __init__.py to Code directory for proper packaging
  • .gitignore: Comprehensive Python gitignore with testing artifacts and Claude settings
  • Poetry Scripts: Configured poetry run test and poetry run tests commands

Testing the Infrastructure

Installation

poetry install

Running Tests

# Run all tests
poetry run test

# Run with coverage
poetry run pytest --cov

# Run specific markers
poetry run pytest -m unit
poetry run pytest -m integration
poetry run pytest -m slow

# Run validation tests
poetry run pytest tests/test_validation.py -v

Validation Results

All 17 validation tests pass successfully, confirming:

  • pytest installation and functionality
  • All dependencies are properly installed
  • Fixtures are accessible and working
  • Custom markers are registered
  • Coverage configuration is active
  • Test discovery is functioning

Notes

  • Python version requirement set to ^3.11 for compatibility with latest scipy
  • The existing random_forest.py file has some syntax issues causing coverage warnings, but this doesn't affect the testing infrastructure
  • Coverage is currently at 0% as no production code is being tested (only infrastructure validation)
  • The poetry.lock file is committed to ensure reproducible builds

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 with coverage to ensure code quality
  5. Use custom markers to organize and run specific test suites

llbbl avatar Jun 29 '25 18:06 llbbl