ElectricEye icon indicating copy to clipboard operation
ElectricEye copied to clipboard

feat: set up comprehensive 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 ElectricEye project, migrating from basic requirements.txt to Poetry for modern dependency management and setting up pytest with full coverage support.

Changes Made

Package Management

  • Migrated to Poetry: Created pyproject.toml with Poetry configuration
  • Migrated dependencies: Moved all dependencies from requirements.txt to Poetry
  • Added test dependencies:
    • pytest (^8.0.0) - Main testing framework
    • pytest-cov (^5.0.0) - Coverage reporting
    • pytest-mock (^3.14.0) - Mocking utilities
    • black (^24.0.0) - Code formatting (already in requirements-dev.txt)

Testing Configuration

  • Pytest configuration in pyproject.toml:

    • Strict markers and configuration
    • Test discovery patterns for test_*.py and *_test.py
    • Custom markers: unit, integration, slow
    • Coverage settings with 80% threshold
    • HTML and XML coverage reports
  • Coverage configuration:

    • Source set to eeauditor package
    • Branch coverage enabled
    • Proper exclusions for test files and virtual environments
    • Report precision and missing line display

Directory Structure

/workspace/
├── pyproject.toml          # Poetry and tool configurations
├── tests/                  # Test root directory
│   ├── __init__.py
│   ├── conftest.py        # Shared fixtures
│   ├── test_setup_validation.py  # Infrastructure validation
│   ├── unit/              # Unit tests directory
│   │   └── __init__.py
│   └── integration/       # Integration tests directory
│       └── __init__.py
└── eeauditor/tests/       # Existing tests (preserved)

Shared Fixtures (conftest.py)

Created comprehensive fixtures for common testing patterns:

  • temp_dir - Temporary directory management
  • mock_config - Mock configuration for cloud providers
  • mock_aws_client/session - AWS service mocking
  • sample_finding - Security Hub finding template
  • mock_env_vars - Environment variable mocking
  • json_file/temp_file - File handling helpers
  • mock_cloud_provider - Generic cloud provider mock
  • capture_logs - Log capture for testing
  • mock_datetime - Date/time mocking

Additional Updates

  • Updated .gitignore with:
    • Testing artifacts (.pytest_cache/, .coverage, htmlcov/, etc.)
    • Claude settings (.claude/*)
    • Build artifacts and virtual environments
    • IDE files and temporary files
    • Note: poetry.lock is NOT ignored (important for reproducible builds)

Running Tests

After setting up the environment with Poetry:

# Install Poetry (if not already installed)
curl -sSL https://install.python-poetry.org | python3 -

# Install dependencies
poetry install

# Run all tests
poetry run pytest

# Alternative command (both work)
poetry run test
poetry run tests

# Run with specific options
poetry run pytest -v                    # Verbose output
poetry run pytest -m unit               # Only unit tests
poetry run pytest -m integration        # Only integration tests
poetry run pytest --no-cov             # Skip coverage
poetry run pytest tests/unit/          # Run specific directory

Notes

  • Python Version: Updated to ^3.9 due to pandas requirements
  • Existing Tests: The existing tests in eeauditor/tests/ are preserved and can be moved to the new structure as needed
  • Coverage Threshold: Set to 80% to encourage good test coverage
  • No Actual Tests Written: This PR only sets up infrastructure; actual unit tests for the codebase should be written separately
  • Validation Tests: Included test_setup_validation.py to verify the testing infrastructure works correctly

Next Steps

  1. Developers can now start writing unit tests in tests/unit/
  2. Integration tests can be added to tests/integration/
  3. Existing tests from eeauditor/tests/ can be refactored to use the new fixtures
  4. Consider adding pre-commit hooks for running tests
  5. Set up CI/CD pipeline to run tests automatically

llbbl avatar Jun 14 '25 20:06 llbbl