Skip to content

Conversation

llbbl
Copy link

@llbbl llbbl commented Jun 14, 2025

Set Up Python Testing Infrastructure

Summary

This PR establishes a comprehensive testing infrastructure for the kubestriker project using Poetry as the package manager and pytest as the testing framework. The setup provides a solid foundation for writing and maintaining tests with proper coverage reporting and test organization.

Changes Made

Package Management

  • Migrated to Poetry: Converted from requirements.txt to Poetry for better dependency management
  • Created pyproject.toml: Centralized project configuration including dependencies, test settings, and coverage configuration
  • Updated dependencies: Modernized package versions for better compatibility

Testing Framework

  • Added pytest: Main testing framework with comprehensive configuration
  • Added pytest-cov: Coverage reporting with HTML and XML output
  • Added pytest-mock: Mocking utilities for isolated testing

Test Configuration

  • Pytest settings:
    • Automatic test discovery with standard naming patterns
    • Coverage reporting with configurable thresholds (currently set to 0% for initial setup)
    • Custom markers for unit, integration, and slow tests
    • Strict mode enabled for better error detection
  • Coverage settings:
    • Source directory coverage for kubestriker package
    • HTML reports in htmlcov/ directory
    • XML reports for CI integration
    • Exclusions for test files and common patterns

Directory Structure

tests/
├── __init__.py
├── conftest.py          # Shared fixtures and configuration
├── test_setup_validation.py  # Infrastructure validation tests
├── unit/
│   └── __init__.py
└── integration/
    └── __init__.py

Test Fixtures

Created comprehensive fixtures in conftest.py:

  • temp_dir: Temporary directory management
  • mock_config: Mock configuration data
  • mock_kubeconfig: Mock Kubernetes configuration
  • mock_k8s_response: Mock API responses
  • mock_vulnerability_data: Mock security scan data
  • clean_env: Environment variable isolation
  • mock_requests_response: HTTP response mocking
  • capture_logs: Log capture utility
  • mock_websocket_client: WebSocket mocking

Development Tools

  • Updated .gitignore: Added patterns for test artifacts, coverage reports, virtual environments, and IDE files
  • Poetry scripts: Both poetry run test and poetry run tests commands available for running tests

How to Use

  1. Install dependencies:

    poetry install
  2. Run all tests:

    poetry run test
    # or
    poetry run tests
  3. Run specific test categories:

    # Unit tests only
    poetry run pytest -m unit
    
    # Integration tests only
    poetry run pytest -m integration
    
    # Exclude slow tests
    poetry run pytest -m "not slow"
  4. View coverage reports:

    • HTML report: Open htmlcov/index.html in a browser
    • Terminal report: Automatically shown when running tests
    • XML report: Available at coverage.xml for CI tools

Notes

  • The coverage threshold is currently set to 0% to allow the infrastructure to be set up without requiring immediate test coverage
  • Once tests are written, update the --cov-fail-under value in pyproject.toml to enforce coverage requirements (recommended: 80%)
  • The SelectMenu package was removed from dependencies due to compatibility issues with modern Python versions
  • All test dependencies are properly isolated in the dev dependency group to avoid affecting production deployments

Next Steps

With this infrastructure in place, developers can now:

  1. Write unit tests in the tests/unit/ directory
  2. Write integration tests in the tests/integration/ directory
  3. Use the provided fixtures for common testing scenarios
  4. Gradually increase the coverage threshold as more tests are added

- Migrate from requirements.txt to Poetry for dependency management
- Add pytest, pytest-cov, and pytest-mock as dev dependencies
- Configure pytest with coverage reporting and custom markers
- Create test directory structure with unit/integration subdirectories
- Add comprehensive test fixtures in conftest.py
- Update .gitignore with testing and development patterns
- Add validation tests to verify infrastructure setup
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant