feat: Set up complete Python testing infrastructure with Poetry
Python Testing Infrastructure Setup
Summary
This PR establishes a complete testing infrastructure for the Python project using Poetry as the package manager and pytest as the testing framework. The setup provides a ready-to-use testing environment with comprehensive fixtures, coverage reporting, and organized test structure.
Changes Made
Package Management
- ✅ Utilized existing Poetry configuration in
pyproject.toml - ✅ Added testing dependencies as dev dependencies:
pytest ^8.3.2- Core testing frameworkpytest-cov ^5.0.0- Coverage reporting pluginpytest-mock ^3.14.0- Mocking utilities
Testing Configuration
Added comprehensive testing configuration to pyproject.toml:
pytest settings:
- Test discovery patterns for
test_*.pyand*_test.pyfiles - Custom markers:
unit,integration,slow - Strict configuration and markers enforcement
- Verbose output by default
- Test paths set to
tests/directory
Coverage settings:
- 80% coverage threshold requirement
- Multiple report formats: terminal, HTML (
htmlcov/), XML (coverage.xml) - Proper exclusions for test files, virtual environments, and build artifacts
- Branch coverage analysis enabled
- Intelligent line exclusions for pragma statements and abstract methods
Directory Structure
tests/
├── __init__.py
├── conftest.py # Shared fixtures and configuration
├── test_infrastructure_validation.py # Infrastructure validation tests
├── unit/ # Unit tests directory
│ └── __init__.py
└── integration/ # Integration tests directory
└── __init__.py
Shared Fixtures (conftest.py)
Comprehensive set of reusable fixtures for common testing needs:
temp_dir- Temporary directory creation and cleanuptemp_file- Temporary file with sample contentmock_config- Mock configuration dictionary with common settingsmock_env_vars- Environment variable mockingmock_logger- Pre-configured mock logger with all standard methodssample_data- Sample data structures (users, products, nested data)mock_file_system- Creates mock directory structure with sample filesmock_api_response- Mock HTTP response objectmock_database_connection- Mock database connection with cursorcapture_logs- Log capturing utilityreset_singleton_instances- Automatic singleton reset between tests
Auto-configuration features:
- Automatic test marking based on directory location (unit/integration)
- Project root automatically added to Python path
Additional Files
.gitignore - Comprehensive Python gitignore including:
- Python artifacts (
__pycache__/,*.pyc,*.egg-info/) - Test artifacts (
.pytest_cache/,.coverage,htmlcov/,coverage.xml) - Virtual environments (
venv/,.venv/,virtualenv/) - IDE files (
.vscode/,.idea/) - Claude settings (
.claude/) - Database files, backups, and temporary files
README.md - Basic project documentation
Example source structure (src/) - Sample module to demonstrate coverage
Validation Tests
Created comprehensive validation suite (test_infrastructure_validation.py) that verifies:
- All testing dependencies are properly installed
- Directory structure is correctly created
- All fixtures work as expected (18 different fixtures tested)
- Custom markers are properly configured
- Coverage configuration is set up correctly
- Python path includes project root
- pytest-mock integration works
How to Use
Install Dependencies
poetry install --no-root
Run Tests
# Run all tests
poetry run pytest
# Run with verbose output
poetry run pytest -v
# Run without coverage
poetry run pytest --no-cov
# Run specific test file
poetry run pytest tests/test_infrastructure_validation.py
# Run tests quietly
poetry run pytest -q
Run Tests by Category
# Run only unit tests
poetry run pytest -m unit
# Run only integration tests
poetry run pytest -m integration
# Skip slow tests
poetry run pytest -m "not slow"
Coverage Reports
# Run with coverage for source code
poetry run pytest --cov=src --cov-report=term-missing
# Generate HTML report
poetry run pytest --cov=src --cov-report=html
# Open htmlcov/index.html in browser
# Check coverage threshold
poetry run pytest --cov=src --cov-fail-under=80
Writing Tests
Example Test Structure
import pytest
class TestMyFeature:
@pytest.mark.unit
def test_basic_functionality(self):
"""Test basic feature functionality."""
assert my_function(2, 3) == 5
@pytest.mark.integration
def test_with_external_service(self, mock_api_response):
"""Test integration with external service."""
service = MyService()
service.client = mock_api_response
assert service.fetch_data() == {"status": "success", "data": []}
def test_with_temp_files(self, mock_file_system):
"""Test file operations using mock file system."""
assert (mock_file_system["data"] / "data.json").exists()
data = process_file(mock_file_system["data"] / "data.json")
assert data == {"test": "data"}
Validation Results
✅ All 18 validation tests pass successfully:
- Infrastructure setup validation
- Fixture functionality tests
- Directory structure verification
- Configuration validation
- Mock utilities verification
Notes for Developers
- The testing infrastructure is ready for immediate use
- Add your source code tests in the appropriate directories (
unit/orintegration/) - Use the provided fixtures to avoid boilerplate code
- Coverage is optional and configured to run with
--covflag - Poetry scripts are configured but optional (can use
poetry run pytestdirectly) - When adding source code, coverage will automatically track it based on configuration
Configuration Choices
- Poetry was used as it was already configured in the project
- Coverage source is configured for common directories (src, app, lib) to avoid warnings
- Package mode is disabled in Poetry since this is for testing infrastructure only
- Markers are pre-configured but also auto-applied based on test location
- Fixtures cover the most common testing scenarios to maximize reusability
Dependencies Added
- pytest ^8.3.2: Main testing framework
- pytest-cov ^5.0.0: Coverage reporting integration
- pytest-mock ^3.14.0: Enhanced mocking capabilities
All dependencies are specified with compatible version ranges to ensure stability while allowing minor updates.