BMW-TensorFlow-Training-GUI icon indicating copy to clipboard operation
BMW-TensorFlow-Training-GUI copied to clipboard

feat: Add comprehensive Python testing infrastructure with Poetry

Open llbbl opened this issue 6 months ago • 0 comments

Add Python Testing Infrastructure

Summary

This PR sets up a comprehensive testing infrastructure for the BMW LabelTool Lite project using Poetry as the package manager and pytest as the testing framework.

Changes Made

Package Management

  • Poetry Setup: Created pyproject.toml with Poetry configuration as the project's package manager
  • Dependencies: Added testing dependencies as development dependencies:
    • pytest ^7.4.4 - Main testing framework
    • pytest-cov ^4.1.0 - Coverage reporting plugin
    • pytest-mock ^3.12.0 - Mocking utilities

Testing Configuration

  • Pytest Configuration: Configured in pyproject.toml with:

    • Automatic test discovery patterns
    • Coverage settings with 80% threshold
    • HTML and XML coverage report generation
    • Custom markers: unit, integration, slow
    • Strict mode enabled for better error detection
  • Coverage Configuration: Set up comprehensive coverage tracking:

    • Covers all three main modules: docker_sdk_api, inference_api, training_api
    • Excludes test files, cache, and virtual environments from coverage
    • Configured multiple report formats (terminal, HTML, XML)

Directory Structure

tests/
├── __init__.py
├── conftest.py                    # Shared pytest fixtures
├── test_infrastructure_validation.py  # Validation tests
├── unit/
│   └── __init__.py
└── integration/
    └── __init__.py

Shared Fixtures (conftest.py)

Created comprehensive fixtures for common testing needs:

  • temp_dir - Temporary directory management
  • mock_config - Configuration mocking
  • mock_logger - Logger mocking
  • sample_image_path, sample_model_path - File fixtures
  • mock_request, mock_response - HTTP mocking
  • sample_training_data, sample_inference_data - Data fixtures
  • mock_docker_client - Docker client mocking
  • Additional fixtures for database, async operations, and file uploads

Development Commands

Configured Poetry scripts for running tests:

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

All standard pytest options are available (e.g., -v, -k, -m, --no-cov)

Additional Setup

  • Updated .gitignore: Added comprehensive Python-specific entries including:

    • Testing artifacts (.pytest_cache/, coverage.xml, htmlcov/)
    • Claude-specific entries (.claude/*)
    • Virtual environments and IDE files
    • ML/AI specific files and directories
  • Validation Tests: Created test_infrastructure_validation.py to verify:

    • All directories and configuration files exist
    • Fixtures are properly available
    • Markers work correctly
    • Coverage is configured properly

How to Use

  1. Install Poetry (if not already installed):

    curl -sSL https://install.python-poetry.org | python3 -
    
  2. Install dependencies:

    poetry install
    
  3. Run tests:

    poetry run test              # Run all tests with coverage
    poetry run test -v           # Verbose output
    poetry run test -m unit      # Run only unit tests
    poetry run test -m "not slow" # Skip slow tests
    poetry run test --no-cov     # Run without coverage
    
  4. View coverage reports:

    • Terminal: Shown automatically after test run
    • HTML: Open htmlcov/index.html in a browser
    • XML: Available at coverage.xml for CI integration

Notes

  • The project uses Python 3.9+ due to dependency requirements
  • Coverage threshold is set to 80% - tests will fail if coverage drops below this
  • The infrastructure is ready for immediate test writing - no actual unit tests for the codebase were created
  • Each microservice (docker_sdk_api, inference_api, training_api) is included in coverage tracking
  • Poetry lock file (poetry.lock) should be committed to ensure reproducible builds

llbbl avatar Jun 16 '25 23:06 llbbl