hawkpost icon indicating copy to clipboard operation
hawkpost copied to clipboard

feat: Set up comprehensive Python testing infrastructure with Poetry

Open llbbl opened this issue 5 months ago • 0 comments
trafficstars

Set Up Python Testing Infrastructure

Summary

This PR establishes a comprehensive testing infrastructure for the Hawkpost Django application, migrating from Pipenv to Poetry for dependency management and setting up pytest as the test runner with full coverage reporting.

Changes Made

Package Management Migration

  • Migrated from Pipenv to Poetry: Created pyproject.toml with all existing dependencies from Pipfile
  • Preserved exact versions: All package versions remain unchanged to ensure compatibility
  • Added development dependencies:
    • pytest (^8.0.0) - Main testing framework
    • pytest-cov (^4.1.0) - Coverage reporting
    • pytest-mock (^3.12.0) - Mocking utilities
    • pytest-django (^4.8.0) - Django integration

Testing Configuration

pytest Configuration (pyproject.toml)

  • Test discovery patterns for multiple naming conventions
  • Coverage settings with 80% threshold
  • HTML and XML coverage report generation
  • Custom markers for test categorization (unit, integration, slow)
  • Configured to run with -v for verbose output

Coverage Configuration

  • Covers all app modules: hawkpost, boxes, humans, pages
  • Excludes migrations, static files, templates, and test files
  • Branch coverage enabled
  • Multiple output formats (terminal, HTML, XML)

Test Infrastructure

Directory Structure

tests/
├── __init__.py
├── conftest.py              # Shared fixtures
├── unit/                    # Unit tests
│   └── __init__.py
├── integration/             # Integration tests
│   └── __init__.py
└── test_infrastructure_validation.py  # Setup validation

Test Settings (hawkpost/settings/test.py)

  • SQLite in-memory database for fast test execution
  • Disabled migrations for speed
  • Test-specific secret key
  • Email backend set to locmem for capturing emails
  • Celery configured for synchronous execution
  • Disabled debug toolbar

Shared Fixtures (tests/conftest.py)

Comprehensive fixtures for Django testing:

  • temp_dir - Temporary directory management
  • user, admin_user, another_user - Test user creation
  • client, authenticated_client, admin_client - Django test clients
  • api_client, authenticated_api_client - API testing clients
  • sample_file, sample_image - File upload testing
  • mock_datetime - Date/time mocking
  • captured_emails - Email capture for testing
  • mock_external_api - External API mocking
  • mock_celery_task - Celery task mocking
  • mock_redis - Redis mocking
  • test_data_factory - Test data generation
  • form_data - Common form testing data

Development Experience

Poetry Scripts

poetry run test   # Run all tests
poetry run tests  # Alternative command (both work)

All standard pytest options are available:

poetry run test -v                    # Verbose output
poetry run test -k "test_name"        # Run specific test
poetry run test -m unit               # Run only unit tests
poetry run test --cov-report=html     # Generate HTML coverage

Updated .gitignore

Added entries for:

  • Testing artifacts (.pytest_cache/, htmlcov/, coverage.xml)
  • Claude settings (.claude/*)
  • Python build artifacts
  • Virtual environments
  • Note: poetry.lock is intentionally NOT ignored

How to Use

  1. Install Poetry (if not already installed):

    curl -sSL https://install.python-poetry.org | python3 -
    
  2. Install dependencies:

    poetry install
    
  3. Run tests:

    poetry run test
    # or
    poetry run tests
    
  4. Run with specific markers:

    poetry run test -m unit          # Only unit tests
    poetry run test -m integration   # Only integration tests
    poetry run test -m "not slow"    # Exclude slow tests
    
  5. Generate coverage reports:

    poetry run test                  # Terminal report (automatic)
    # HTML report available at htmlcov/index.html
    

Notes

  • The infrastructure is ready for immediate test development
  • Coverage threshold is set to 80% (will fail until actual tests are written)
  • All validation tests pass, confirming the setup works correctly
  • Test database uses SQLite for speed and isolation
  • No actual unit tests for the codebase were written - only infrastructure setup

Next Steps

Developers can now immediately start writing tests:

  1. Unit tests in tests/unit/
  2. Integration tests in tests/integration/
  3. Use the comprehensive fixtures from conftest.py
  4. Follow the established patterns from the validation tests

llbbl avatar Jun 25 '25 17:06 llbbl