Skip to content

Conversation

@llbbl
Copy link

@llbbl llbbl commented Jun 27, 2025

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

- Set up Poetry as package manager with pyproject.toml configuration
- Migrate dependencies from requirements.txt to Poetry
- Add pytest, pytest-cov, and pytest-mock as dev dependencies
- Configure pytest with coverage reporting (HTML/XML outputs)
- Create test directory structure with unit/integration subdirectories
- Add comprehensive shared fixtures in conftest.py
- Update .gitignore with testing and Claude-related entries
- Add poetry run test/tests commands for convenience
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant