python-demo icon indicating copy to clipboard operation
python-demo copied to clipboard

feat: Set up comprehensive Python testing infrastructure

Open llbbl opened this issue 3 months ago • 0 comments

Set up comprehensive Python testing infrastructure

Summary

This PR establishes a complete testing infrastructure for the Python API demonstration project using Poetry as the package manager and pytest as the testing framework. The setup provides developers with a ready-to-use testing environment that includes coverage reporting, mocking capabilities, and organized test structures.

Changes Made

Package Management & Dependencies

  • Poetry Configuration: Set up pyproject.toml with Poetry configuration for project management
  • Testing Dependencies: Added pytest, pytest-cov, and pytest-mock as development dependencies
  • Python Version: Configured for Python 3.8+ compatibility

Testing Configuration

  • pytest Configuration: Comprehensive pytest settings including:
    • Test discovery patterns for files, classes, and functions
    • Coverage reporting with 80% threshold requirement
    • HTML and XML coverage report generation
    • Custom markers for unit, integration, and slow tests
    • Strict configuration for consistent behavior

Project Structure

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

Shared Testing Fixtures

Created comprehensive fixtures in tests/conftest.py:

  • temp_dir and temp_file for file system testing
  • mock_env_vars for environment variable mocking
  • sample_config for configuration testing
  • mock_api_response and mock_error_response for API testing
  • mock_datetime for time-dependent testing
  • Additional utility fixtures for common testing scenarios

Coverage Configuration

  • Source Coverage: Tracks coverage for the code/ directory
  • Exclusions: Properly excludes test files, virtual environments, and build artifacts
  • Reporting: Generates term, HTML, and XML coverage reports
  • Thresholds: Enforces 80% minimum coverage requirement

Development Environment

  • .gitignore: Added comprehensive exclusions for:
    • Python artifacts (__pycache__/, *.pyc, dist/, etc.)
    • Testing artifacts (.pytest_cache/, .coverage, htmlcov/)
    • Virtual environments and IDE files
    • Claude Code settings (.claude/*)

Testing Infrastructure Validation

The setup includes validation tests (tests/test_infrastructure.py) that verify:

  • ✅ pytest functionality and Python version compatibility
  • ✅ Project structure and required directories
  • ✅ Shared fixtures availability and functionality
  • ✅ Environment variable mocking capabilities
  • ✅ Custom markers for test categorization
  • ✅ pytest-mock integration
  • ✅ Code directory accessibility for imports

Usage Instructions

Running Tests

# Install dependencies
poetry install

# Run all tests
poetry run pytest

# Run tests with coverage
poetry run pytest --cov=code

# Run only unit tests
poetry run pytest -m unit

# Run tests excluding slow ones
poetry run pytest -m "not slow"

# Run specific test file
poetry run pytest tests/test_infrastructure.py

Coverage Reports

  • Terminal: Coverage summary displayed after test runs
  • HTML: Detailed coverage report in htmlcov/index.html
  • XML: Machine-readable report in coverage.xml for CI integration

Adding New Tests

  1. Create test files in appropriate directories (tests/unit/ or tests/integration/)
  2. Use shared fixtures from conftest.py
  3. Apply appropriate markers (@pytest.mark.unit, @pytest.mark.integration, @pytest.mark.slow)
  4. Follow naming conventions (test_*.py, Test* classes, test_* functions)

Notes

  • No Lock File Exclusion: The poetry.lock file is intentionally not in .gitignore to ensure consistent dependency versions across environments
  • Coverage Threshold: Set to 80% minimum - adjust in pyproject.toml if needed
  • Ready for CI: Configuration supports common CI/CD systems with XML coverage reporting
  • Extensible: Easy to add additional testing dependencies or configuration as needed

🤖 Generated with Claude Code

llbbl avatar Sep 03 '25 01:09 llbbl