LightCNN icon indicating copy to clipboard operation
LightCNN copied to clipboard

feat: set up Python testing infrastructure with Poetry and pytest

Open llbbl opened this issue 6 months ago • 0 comments

Set Up Python Testing Infrastructure

Summary

This PR establishes a complete testing infrastructure for the LightCNN Python project using Poetry as the package manager and pytest as the testing framework.

Changes Made

Package Management

  • Poetry configured as the package manager (default choice, no existing package manager found)
  • Added pyproject.toml with proper Poetry configuration
  • Set package-mode to false since this is a script-based project

Testing Dependencies

Added as development dependencies:

  • pytest (^7.4.0) - Core testing framework
  • pytest-cov (^4.1.0) - Coverage reporting
  • pytest-mock (^3.11.1) - Mocking utilities

Testing Configuration

Configured in pyproject.toml:

  • pytest settings:
    • Test discovery patterns for test_*.py and *_test.py
    • Custom markers: unit, integration, slow
    • Strict marker enforcement
    • Verbose output
  • Coverage settings:
    • 80% coverage threshold target
    • HTML and XML report generation
    • Excluded directories: tests, virtual environments, build artifacts
    • Coverage lines exclusion patterns

Directory Structure

Created proper testing structure:

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

Fixtures (conftest.py)

Comprehensive set of reusable fixtures:

  • temp_dir - Temporary directory management
  • mock_config - Mock configuration objects
  • sample_data - Test data samples
  • mock_model - Mock ML model
  • test_image_path - Temporary test image files
  • test_data_file - Temporary data files
  • reset_modules - Module isolation between tests
  • capture_logs - Log capture utility
  • mock_torch - PyTorch mocking for tests

Additional Setup

  • Updated .gitignore with:
    • Testing artifacts (.pytest_cache/, coverage files)
    • Poetry lock file preservation
    • Claude settings (.claude/*)
    • IDE and build artifacts

How to Use

Install Dependencies

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

# Install project dependencies
poetry install

Run Tests

# Run all tests
poetry run pytest

# Run with coverage
poetry run pytest --cov=. --cov-report=html

# Run specific test markers
poetry run pytest -m unit
poetry run pytest -m integration
poetry run pytest -m "not slow"

Writing Tests

  1. Place unit tests in tests/unit/
  2. Place integration tests in tests/integration/
  3. Use fixtures from conftest.py for common test needs
  4. Mark tests appropriately with @pytest.mark.unit, @pytest.mark.integration, or @pytest.mark.slow

Validation

The setup includes validation tests in test_setup_validation.py that verify:

  • pytest is properly installed
  • Project structure is correct
  • All fixtures work as expected
  • Test markers function properly
  • Test discovery is configured correctly

All validation tests pass successfully ✅

Notes

  • Coverage reporting is configured but the threshold check is currently disabled for infrastructure-only setup
  • The project uses Poetry in non-package mode since it appears to be a script-based project
  • Python 3.8+ is required as specified in dependencies

llbbl avatar Jun 24 '25 04:06 llbbl