camera-live-streaming icon indicating copy to clipboard operation
camera-live-streaming copied to clipboard

feat: Set up complete Python testing infrastructure with Poetry

Open llbbl opened this issue 3 months ago • 0 comments

Set up Complete Python Testing Infrastructure with Poetry

Summary

This PR establishes a comprehensive testing infrastructure for the Python project, migrating from a basic requirements.txt setup to a modern Poetry-managed environment with robust testing capabilities.

Changes Made

Package Management

  • Migrated to Poetry: Created pyproject.toml with Poetry configuration
  • Dependency Migration: Moved dependencies from requirements.txt to Poetry format
  • Updated OpenCV: Upgraded opencv-python to ^4.8.0 for better compatibility

Testing Dependencies Added

  • pytest ^7.0.0 - Main testing framework
  • pytest-cov ^4.0.0 - Coverage reporting
  • pytest-mock ^3.10.0 - Mocking utilities

Testing Configuration

  • pytest Configuration: Comprehensive setup in pyproject.toml with:
    • 80% coverage threshold requirement
    • HTML and XML coverage report generation
    • Custom markers: unit, integration, slow
    • Strict configuration and output formatting
  • Coverage Configuration: Detailed coverage settings with source inclusion/exclusion rules

Directory Structure

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

Shared Testing Fixtures

Created comprehensive fixtures in tests/conftest.py:

  • temp_dir, temp_file - Temporary filesystem resources
  • sample_data - Common test data structures
  • mock_config, mock_database, mock_logger - Mock objects
  • json_file, text_file - Temporary files with content
  • mock_environment - Environment variable setup
  • capture_logs - Log capture utilities

Development Workflow

  • Poetry Scripts: Added poetry run test and poetry run tests commands
  • Coverage Reports: Generates htmlcov/ directory and coverage.xml file
  • Git Integration: Updated .gitignore with testing artifacts and development files

Validation

  • 21 Validation Tests: Comprehensive test suite verifying:
    • Infrastructure setup and configuration
    • All fixtures functionality
    • Project structure integrity
    • Pytest markers and parametrization
  • All Tests Passing: Infrastructure validated and ready for development

Instructions for Running Tests

Installation

poetry install

Running Tests

# Both commands work identically
poetry run test
poetry run tests

# Direct pytest with all configured options
poetry run pytest

Coverage Reports

  • Terminal: Coverage summary displayed after test run
  • HTML Report: Open htmlcov/index.html in browser
  • XML Report: coverage.xml for CI/CD integration

Using Custom 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

Notes

  • Coverage Threshold: Set to 80% - currently failing as expected since no application tests exist yet
  • Poetry Lock: poetry.lock file created and should be committed for dependency locking
  • Ready for Development: Infrastructure is fully functional and ready for writing actual application tests
  • Fixtures Available: Comprehensive set of reusable fixtures for common testing scenarios

Next Steps

Developers can now immediately start writing tests by:

  1. Creating test files in tests/unit/ or tests/integration/
  2. Using the provided fixtures from conftest.py
  3. Running tests with poetry run test
  4. Viewing coverage reports in htmlcov/

🧪 Testing infrastructure is now complete and validated!

llbbl avatar Sep 04 '25 12:09 llbbl