python-clean-architecture-example icon indicating copy to clipboard operation
python-clean-architecture-example copied to clipboard

feat: Set up comprehensive Python testing infrastructure with Poetry

Open llbbl opened this issue 3 months ago • 0 comments

Set up comprehensive Python testing infrastructure with Poetry

Summary

This PR establishes a complete, production-ready testing infrastructure for the Python project using Poetry as the package manager. The setup provides developers with all necessary tools and configurations to write, execute, and analyze tests effectively.

Changes Made

Package Management

  • Migrated from requirements.txt to Poetry: Created comprehensive pyproject.toml with all existing dependencies properly categorized
  • Set up Poetry as package manager: Installed Poetry 2.1.4 and configured project structure
  • Organized dependencies: Separated production dependencies from test dependencies using Poetry groups

Testing Framework Configuration

  • pytest Configuration: Comprehensive pytest settings in pyproject.toml including:

    • Test discovery patterns for both tests/ directory and existing *_test.py files in src/
    • Custom markers for unit, integration, and slow tests
    • Strict configuration options for better test reliability
    • Verbose output formatting
  • Coverage Reporting: Advanced coverage configuration with:

    • 80% coverage threshold requirement
    • Multiple output formats: HTML (htmlcov/), XML (coverage.xml), and terminal
    • Smart exclusion rules for test files, migrations, and boilerplate code
    • Detailed missing line reporting

Directory Structure

  • Organized test structure: Created tests/ directory with proper subdirectories:
    • tests/unit/ - for unit tests
    • tests/integration/ - for integration tests
    • Each directory includes proper __init__.py files

Shared Testing Utilities

  • Comprehensive fixtures in tests/conftest.py:
    • Temporary file and directory fixtures
    • Mock objects for common components (logger, repository, presenter)
    • Sample profession data fixtures for testing
    • Environment variable management
    • Flask app and database session mocks

Development Scripts

  • Poetry commands: Set up easy-to-use commands:
    • poetry run test - Execute all tests
    • poetry run tests - Alternative command (both work identically)
    • Full pytest command-line compatibility

Project Hygiene

  • Enhanced .gitignore: Added comprehensive exclusions for:
    • Testing artifacts (.pytest_cache/, htmlcov/, coverage.xml)
    • Virtual environments and build artifacts
    • IDE and OS-specific files
    • Claude Code settings

Validation

  • Infrastructure validation tests: Created tests/test_infrastructure_validation.py to verify:
    • pytest functionality
    • Custom fixtures availability
    • pytest-mock integration
    • Marker functionality
    • Coverage reporting

Testing Instructions

Running Tests

# Install dependencies
poetry install

# Run all tests
poetry run pytest

# Run with coverage
poetry run pytest --cov=src

# Run specific test categories
poetry run pytest -m unit
poetry run pytest -m integration

# Run validation tests
poetry run pytest tests/test_infrastructure_validation.py -v

Available Commands

  • poetry run test - Run tests using Poetry script
  • poetry run tests - Alternative command
  • poetry run pytest - Direct pytest execution

Dependencies Added

  • Testing frameworks: pytest, pytest-cov, pytest-mock, pytest-flask
  • Coverage tools: coverage with HTML/XML reporting
  • All existing dependencies: Successfully migrated from requirements.txt

Validation Results

✅ All infrastructure validation tests pass (9/9) ✅ Coverage reports generate successfully in multiple formats ✅ Both poetry run test and poetry run tests commands work ✅ Custom fixtures and markers function properly ✅ Poetry dependencies install and resolve correctly

Notes

  • poetry.lock file: Generated and should be committed to ensure consistent dependency versions
  • Existing tests: All existing *_test.py files in the src/ directory are automatically discovered
  • Coverage threshold: Set to 80% - can be adjusted in pyproject.toml if needed
  • Backward compatibility: Original requirements.txt preserved for reference

The testing infrastructure is now fully operational and ready for development teams to write comprehensive test suites.

🤖 Generated with Claude Code

llbbl avatar Sep 04 '25 18:09 llbbl