python-demo
python-demo copied to clipboard
feat: Set up comprehensive Python testing infrastructure
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.tomlwith 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_dirandtemp_filefor file system testingmock_env_varsfor environment variable mockingsample_configfor configuration testingmock_api_responseandmock_error_responsefor API testingmock_datetimefor 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/*)
- Python artifacts (
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.xmlfor CI integration
Adding New Tests
- Create test files in appropriate directories (
tests/unit/ortests/integration/) - Use shared fixtures from
conftest.py - Apply appropriate markers (
@pytest.mark.unit,@pytest.mark.integration,@pytest.mark.slow) - Follow naming conventions (
test_*.py,Test*classes,test_*functions)
Notes
- No Lock File Exclusion: The
poetry.lockfile is intentionally not in.gitignoreto ensure consistent dependency versions across environments - Coverage Threshold: Set to 80% minimum - adjust in
pyproject.tomlif 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