feat: Set up Python testing infrastructure with Poetry
Set up Python Testing Infrastructure
Summary
This PR establishes a comprehensive testing infrastructure for the CrocodileHunter project using Poetry as the package manager and pytest as the testing framework. The setup provides a solid foundation for developers to write and run tests with proper coverage reporting and organization.
Changes Made
Package Management
- Migrated to Poetry: Set up Poetry as the modern Python package manager
- Created pyproject.toml: Comprehensive configuration file with project metadata and dependencies
- Migrated dependencies: Moved all dependencies from requirements.txt to Poetry's dependency management
Testing Framework
- Added testing dependencies:
pytest(^7.4.0) - Main testing frameworkpytest-cov(^4.1.0) - Coverage reportingpytest-mock(^3.11.1) - Mocking utilities
Testing Configuration
-
Pytest configuration in pyproject.toml:
- Test discovery patterns for
test_*.pyand*_test.py - Coverage reporting with HTML and XML outputs
- Coverage threshold set to 80%
- Custom markers:
@pytest.mark.unit,@pytest.mark.integration,@pytest.mark.slow - Strict mode with verbose output
- Test discovery patterns for
-
Coverage configuration:
- Source tracking for the
src/directory - Exclusions for test files, migrations, virtual environments, and cache directories
- HTML report output to
htmlcov/ - XML report output to
coverage.xml
- Source tracking for the
Directory Structure
tests/
├── __init__.py
├── conftest.py # Shared fixtures and configuration
├── unit/ # Unit tests directory
│ ├── __init__.py
│ └── test_example.py # Example test file
├── integration/ # Integration tests directory
│ └── __init__.py
└── test_infrastructure_validation.py # Validation tests
Test Fixtures (conftest.py)
Created comprehensive fixtures for testing:
temp_dir: Temporary directory for test filesmock_config: Mock configuration dictionarymock_database_session: Mock database session for testingflask_test_client: Placeholder for Flask test clientsample_tower_data: Sample tower data for testingsample_gps_data: Sample GPS data for testingreset_environment: Reset environment variables between testsmock_requests: Mock requests library for API testingisolated_filesystem: Create isolated filesystem for file operations
Poetry Scripts
- Added command shortcuts:
poetry run test- Run all testspoetry run tests- Alternative command (both work)
How to Use
Install Dependencies
poetry install
Run Tests
# Run all tests with coverage
poetry run test
# Run specific test file
poetry run pytest tests/test_infrastructure_validation.py
# Run tests with specific marker
poetry run pytest -m unit
poetry run pytest -m integration
# Run with verbose output
poetry run pytest -v
# Run without coverage
poetry run pytest --no-cov
Coverage Reports
After running tests, coverage reports are available in:
- Terminal output (with missing lines)
htmlcov/index.html- HTML coverage reportcoverage.xml- XML report for CI/CD integration
Validation
The setup has been validated with:
- All dependencies successfully installed via Poetry
- Validation tests passing (14/14 tests)
- Test discovery working correctly
- Fixtures functioning as expected
- Coverage reporting generating properly
Notes
-
Existing .gitignore: The project already has comprehensive gitignore entries for Python development, including test artifacts.
-
Poetry Lock File: The
poetry.lockfile has been generated and should be committed to ensure reproducible builds. -
Virtual Environment: Poetry creates a virtual environment in
.venv/which is properly ignored by git. -
Coverage Threshold: Set to 80% as requested. The validation tests alone won't meet this threshold - actual unit tests for the codebase are needed.
Next Steps
- Write unit tests for the existing codebase modules in
src/ - Add integration tests for API endpoints and database operations
- Configure CI/CD to run tests automatically
- Consider adding pre-commit hooks for test execution
- Add linting and type checking commands to pyproject.toml
The testing infrastructure is now ready for developers to start writing comprehensive tests for the CrocodileHunter project