crocodilehunter icon indicating copy to clipboard operation
crocodilehunter copied to clipboard

feat: Set up Python testing infrastructure with Poetry

Open llbbl opened this issue 6 months ago • 0 comments

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 framework
    • pytest-cov (^4.1.0) - Coverage reporting
    • pytest-mock (^3.11.1) - Mocking utilities

Testing Configuration

  • Pytest configuration in pyproject.toml:

    • Test discovery patterns for test_*.py and *_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
  • 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

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 files
  • mock_config: Mock configuration dictionary
  • mock_database_session: Mock database session for testing
  • flask_test_client: Placeholder for Flask test client
  • sample_tower_data: Sample tower data for testing
  • sample_gps_data: Sample GPS data for testing
  • reset_environment: Reset environment variables between tests
  • mock_requests: Mock requests library for API testing
  • isolated_filesystem: Create isolated filesystem for file operations

Poetry Scripts

  • Added command shortcuts:
    • poetry run test - Run all tests
    • poetry 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 report
  • coverage.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

  1. Existing .gitignore: The project already has comprehensive gitignore entries for Python development, including test artifacts.

  2. Poetry Lock File: The poetry.lock file has been generated and should be committed to ensure reproducible builds.

  3. Virtual Environment: Poetry creates a virtual environment in .venv/ which is properly ignored by git.

  4. 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

  1. Write unit tests for the existing codebase modules in src/
  2. Add integration tests for API endpoints and database operations
  3. Configure CI/CD to run tests automatically
  4. Consider adding pre-commit hooks for test execution
  5. 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

llbbl avatar Jun 14 '25 00:06 llbbl