micro-jpeg-visualizer icon indicating copy to clipboard operation
micro-jpeg-visualizer copied to clipboard

feat: Add comprehensive Python testing infrastructure with Poetry

Open llbbl opened this issue 4 months ago • 0 comments

Add Python Testing Infrastructure

Summary

This PR establishes a comprehensive testing infrastructure for the micro-jpeg-visualizer Python project using Poetry as the package manager and pytest as the testing framework.

Changes Made

Package Management

  • Poetry Setup: Configured Poetry as the package manager with pyproject.toml
  • Package Mode: Set to false since this is a standalone script project
  • Dependencies: Added Pillow as a production dependency (was already used in code)

Testing Framework

  • pytest: Main testing framework with extensive configuration
  • pytest-cov: Coverage reporting with HTML and XML output
  • pytest-mock: Mocking utilities for unit tests
  • Custom Markers: Added unit, integration, and slow test markers

Project Structure

/workspace/
├── pyproject.toml          # Poetry and pytest configuration
├── poetry.lock            # Locked dependencies
├── .gitignore             # Updated with testing entries
├── run_tests.sh           # Helper script for running tests
└── tests/
    ├── __init__.py
    ├── conftest.py        # Shared fixtures
    ├── test_setup_validation.py  # Infrastructure validation
    ├── unit/
    │   └── __init__.py
    └── integration/
        └── __init__.py

Test Configuration

Coverage Settings

  • Source directory: Project root
  • Excluded: test files, virtual environments, cache directories
  • Report formats: Terminal, HTML (htmlcov/), XML (coverage.xml)
  • Coverage threshold: Removed from CI to allow initial setup

Test Discovery

  • Test paths: tests/ directory
  • File patterns: test_*.py, *_test.py
  • Class patterns: Test*
  • Function patterns: test_*

Fixtures Available

The conftest.py file provides comprehensive fixtures for testing:

  • File System:

    • temp_dir: Temporary directory with automatic cleanup
    • temp_file: Temporary file for testing
    • sample_jpeg_path: Path to sample JPEG image
  • Mocking:

    • mock_config: Configuration dictionary
    • mock_logger: Mock logger with all standard methods
    • mock_file_system: Mock file system operations
    • mock_argv: Mock command line arguments
  • Data:

    • sample_binary_data: Sample binary data for JPEG testing
    • mock_image_data: Mock image data structure
    • zigzag_data: JPEG zigzag pattern data
  • Utilities:

    • captured_output: Capture stdout/stderr
    • performance_timer: Simple timer for performance tests
    • reset_modules: Auto-reset modules between tests

How to Run Tests

Using Poetry directly:

# Run all tests
poetry run pytest

# Run with coverage
poetry run pytest --cov

# Run specific marker
poetry run pytest -m unit
poetry run pytest -m integration

# Run without slow tests
poetry run pytest -m "not slow"

# Run specific test file
poetry run pytest tests/test_setup_validation.py

Using the helper script:

# Make executable (one time)
chmod +x run_tests.sh

# Run tests
./run_tests.sh
./run_tests.sh -v  # verbose
./run_tests.sh --no-cov  # without coverage

Installation

  1. Install Poetry (if not already installed):

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

    poetry install
    
  3. Run validation tests:

    poetry run pytest tests/test_setup_validation.py
    

Next Steps

With this infrastructure in place, developers can now:

  1. Write unit tests for individual functions in tests/unit/
  2. Write integration tests in tests/integration/
  3. Use the provided fixtures for common testing needs
  4. Run tests with coverage to track code quality
  5. Use markers to organize and selectively run tests

Notes

  • The project uses package-mode = false in Poetry since it's a standalone script
  • All test dependencies are in the dev group to keep production dependencies minimal
  • The validation test suite confirms all infrastructure components work correctly
  • Coverage reporting is configured but the threshold is not enforced in CI initially

llbbl avatar Aug 22 '25 23:08 llbbl