hawkpost
hawkpost copied to clipboard
feat: Set up comprehensive Python testing infrastructure with Poetry
trafficstars
Set Up Python Testing Infrastructure
Summary
This PR establishes a comprehensive testing infrastructure for the Hawkpost Django application, migrating from Pipenv to Poetry for dependency management and setting up pytest as the test runner with full coverage reporting.
Changes Made
Package Management Migration
- Migrated from Pipenv to Poetry: Created
pyproject.tomlwith all existing dependencies fromPipfile - Preserved exact versions: All package versions remain unchanged to ensure compatibility
- Added development dependencies:
pytest(^8.0.0) - Main testing frameworkpytest-cov(^4.1.0) - Coverage reportingpytest-mock(^3.12.0) - Mocking utilitiespytest-django(^4.8.0) - Django integration
Testing Configuration
pytest Configuration (pyproject.toml)
- Test discovery patterns for multiple naming conventions
- Coverage settings with 80% threshold
- HTML and XML coverage report generation
- Custom markers for test categorization (
unit,integration,slow) - Configured to run with
-vfor verbose output
Coverage Configuration
- Covers all app modules:
hawkpost,boxes,humans,pages - Excludes migrations, static files, templates, and test files
- Branch coverage enabled
- Multiple output formats (terminal, HTML, XML)
Test Infrastructure
Directory Structure
tests/
├── __init__.py
├── conftest.py # Shared fixtures
├── unit/ # Unit tests
│ └── __init__.py
├── integration/ # Integration tests
│ └── __init__.py
└── test_infrastructure_validation.py # Setup validation
Test Settings (hawkpost/settings/test.py)
- SQLite in-memory database for fast test execution
- Disabled migrations for speed
- Test-specific secret key
- Email backend set to locmem for capturing emails
- Celery configured for synchronous execution
- Disabled debug toolbar
Shared Fixtures (tests/conftest.py)
Comprehensive fixtures for Django testing:
temp_dir- Temporary directory managementuser,admin_user,another_user- Test user creationclient,authenticated_client,admin_client- Django test clientsapi_client,authenticated_api_client- API testing clientssample_file,sample_image- File upload testingmock_datetime- Date/time mockingcaptured_emails- Email capture for testingmock_external_api- External API mockingmock_celery_task- Celery task mockingmock_redis- Redis mockingtest_data_factory- Test data generationform_data- Common form testing data
Development Experience
Poetry Scripts
poetry run test # Run all tests
poetry run tests # Alternative command (both work)
All standard pytest options are available:
poetry run test -v # Verbose output
poetry run test -k "test_name" # Run specific test
poetry run test -m unit # Run only unit tests
poetry run test --cov-report=html # Generate HTML coverage
Updated .gitignore
Added entries for:
- Testing artifacts (
.pytest_cache/,htmlcov/,coverage.xml) - Claude settings (
.claude/*) - Python build artifacts
- Virtual environments
- Note:
poetry.lockis intentionally NOT ignored
How to Use
-
Install Poetry (if not already installed):
curl -sSL https://install.python-poetry.org | python3 - -
Install dependencies:
poetry install -
Run tests:
poetry run test # or poetry run tests -
Run with specific markers:
poetry run test -m unit # Only unit tests poetry run test -m integration # Only integration tests poetry run test -m "not slow" # Exclude slow tests -
Generate coverage reports:
poetry run test # Terminal report (automatic) # HTML report available at htmlcov/index.html
Notes
- The infrastructure is ready for immediate test development
- Coverage threshold is set to 80% (will fail until actual tests are written)
- All validation tests pass, confirming the setup works correctly
- Test database uses SQLite for speed and isolation
- No actual unit tests for the codebase were written - only infrastructure setup
Next Steps
Developers can now immediately start writing tests:
- Unit tests in
tests/unit/ - Integration tests in
tests/integration/ - Use the comprehensive fixtures from
conftest.py - Follow the established patterns from the validation tests