Skip to content

Conversation

llbbl
Copy link

@llbbl llbbl commented Jul 1, 2025

Set Up Python Testing Infrastructure

Summary

This PR establishes a comprehensive testing infrastructure for the drymail project using Poetry as the package manager and pytest as the testing framework.

Changes Made

Package Management

  • Migrated to Poetry: Created pyproject.toml with all project metadata and dependencies
  • Preserved existing dependencies: Migrated mistune and beautifulsoup4 from requirements.txt
  • Added development dependencies:
    • pytest - Core testing framework
    • pytest-cov - Coverage reporting plugin
    • pytest-mock - Mocking utilities

Testing Configuration

  • Pytest configuration in pyproject.toml:

    • Test discovery patterns for test_*.py and *_test.py files
    • Strict markers and configuration
    • Coverage reporting with 80% threshold
    • Output formats: terminal, HTML, and XML reports
    • Custom markers: unit, integration, and slow
  • Coverage configuration:

    • Source set to drymail module
    • Exclusions for test files and common patterns
    • Branch coverage enabled

Directory Structure

tests/
├── __init__.py
├── conftest.py
├── test_infrastructure_validation.py
├── unit/
│   └── __init__.py
└── integration/
    └── __init__.py

Shared Fixtures (conftest.py)

  • temp_dir - Temporary directory for test files
  • mock_smtp_server - Mock SMTP server for email testing
  • mock_email_config - Sample email configuration
  • sample_email_data - Test email data
  • mock_attachment - File attachment mock
  • temp_file - Helper to create temporary files
  • reset_environment - Auto-cleanup of environment variables
  • capture_logs - Log capture utility

Additional Updates

  • Updated .gitignore with:
    • Testing artifacts (.pytest_cache/, coverage files)
    • Claude settings (.claude/*)
    • IDE and OS files
    • Note: poetry.lock is NOT ignored (as per best practices)

How to Use

Install Dependencies

# Install Poetry (if not already installed)
curl -sSL https://install.python-poetry.org | python3 -

# Install project dependencies
poetry install

Running Tests

# Run all tests
poetry run test
# or
poetry run tests

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

# Run with coverage report
poetry run pytest --cov-report=html
# Coverage report will be in htmlcov/index.html

Writing Tests

  1. Place unit tests in tests/unit/
  2. Place integration tests in tests/integration/
  3. Use the provided fixtures from conftest.py
  4. Mark tests appropriately: @pytest.mark.unit, @pytest.mark.integration, @pytest.mark.slow

Notes

  • The coverage threshold is set to 80%. Tests will fail if coverage drops below this
  • The existing requirements.txt and setup.py are preserved for backward compatibility
  • Poetry will create a virtual environment in .venv/ automatically
  • All pytest standard options are available when running tests

Validation

The infrastructure has been validated with a comprehensive test suite (test_infrastructure_validation.py) that verifies:

  • Basic pytest functionality
  • All fixtures are accessible and working
  • Markers are properly configured
  • Mocking capabilities work
  • Coverage tracking is functional
  • Parametrized tests work correctly

- Migrated from requirements.txt/setup.py to Poetry package manager
- Added pytest, pytest-cov, and pytest-mock as dev dependencies
- Configured pytest with coverage reporting (80% threshold)
- Created testing directory structure with unit/integration folders
- Added shared fixtures in conftest.py for common testing patterns
- Updated .gitignore with testing and Claude-specific entries
- Added validation tests to verify infrastructure functionality
- Set up Poetry scripts for running tests (poetry run test/tests)
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