msdat icon indicating copy to clipboard operation
msdat copied to clipboard

feat: Add comprehensive Python testing infrastructure with Poetry

Open llbbl opened this issue 5 months ago • 0 comments

Add Python Testing Infrastructure

Summary

This PR sets up a comprehensive testing infrastructure for the MSDAT project using Poetry as the package manager and pytest as the testing framework. The setup provides a solid foundation for writing and running tests with coverage reporting.

Changes Made

Package Management

  • Poetry Setup: Added pyproject.toml with Poetry configuration
  • Dependency Migration: Migrated all dependencies from requirements.txt to Poetry
  • Development Dependencies: Added pytest, pytest-cov, and pytest-mock as dev dependencies

Testing Configuration

  • pytest Configuration:

    • Configured test discovery patterns
    • Added custom markers (unit, integration, slow)
    • Set up coverage reporting with HTML and XML outputs
    • Enabled strict markers and configuration
  • Coverage Configuration:

    • Source directory coverage tracking
    • Excluded test files, cache directories, and virtual environments
    • Configured coverage report formats and exclusion patterns

Directory Structure

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

Shared Fixtures (conftest.py)

Created comprehensive fixtures for common testing needs:

  • temp_dir - Temporary directory management
  • temp_file - Temporary file creation
  • mock_config - Mock configuration dictionary
  • mock_database_connection - Mock database connection and cursor
  • mock_argparse_args - Mock command line arguments
  • mock_logger - Mock logger object
  • sample_sql_results - Sample SQL query results
  • mock_file_system - Mock file system structure
  • mock_network_response - Mock network responses
  • reset_modules - Module reset between tests
  • capture_output - Stdout/stderr capture
  • mock_progressbar - Mock progress bar
  • environment_variables - Temporary environment variable management
  • mock_color_output - Mock colored terminal output

Other Updates

  • gitignore: Updated with testing artifacts (.pytest_cache/, coverage files) and Claude settings
  • Poetry Scripts: Added poetry run test and poetry run tests commands

How to Use

  1. Install dependencies:

    poetry install
    
  2. Run all tests:

    poetry run test
    # or
    poetry run tests
    
  3. Run specific test files:

    poetry run pytest tests/test_setup_validation.py
    
  4. Run tests with specific markers:

    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
    
  5. View coverage report:

    • HTML report: Open htmlcov/index.html in a browser
    • XML report: Available at coverage.xml

Notes

  • The testing infrastructure is ready to use - developers can immediately start writing tests
  • Coverage threshold is currently not enforced to allow gradual test adoption
  • The argparse package was removed from dependencies as it conflicts with pytest (argparse is built-in to Python 3)
  • All pytest options are available when using the poetry run test/tests commands

Validation

The setup has been validated with a comprehensive test file (test_setup_validation.py) that verifies:

  • All testing dependencies are properly installed
  • All fixtures are working correctly
  • Test markers are configured
  • Coverage reporting is functional

llbbl avatar Jun 27 '25 13:06 llbbl