SoftTeacher icon indicating copy to clipboard operation
SoftTeacher 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 SSOD (Semi-Supervised Object Detection) Python project using Poetry as the package manager and pytest as the testing framework.

Changes Made

Package Management

  • Poetry Configuration: Created pyproject.toml with Poetry as the package manager
  • Dependency Migration: Migrated existing dependencies from requirements.txt to Poetry
  • Development Dependencies: Added testing tools as development dependencies

Testing Framework

  • pytest: Main testing framework with comprehensive configuration
  • pytest-cov: Coverage reporting with 80% threshold requirement
  • pytest-mock: Mocking utilities for unit tests

Project Structure

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

Configuration

pytest Configuration (in pyproject.toml)

  • Minimum version requirement: 7.0
  • Automatic test discovery patterns
  • Coverage reporting (terminal, HTML, XML)
  • 80% coverage threshold
  • Custom test markers: unit, integration, slow
  • Strict mode enabled for better error detection

Coverage Configuration

  • Source: ssod package
  • Branch coverage enabled
  • Exclusions for test files, __init__.py, and common patterns
  • HTML report output to htmlcov/
  • XML report for CI integration

Testing Fixtures (conftest.py)

Created comprehensive fixtures for common testing needs:

  • temp_dir / temp_file: Temporary file system resources
  • mock_config: Sample configuration dictionary
  • mock_model / mock_dataset / mock_dataloader: PyTorch mocks
  • sample_json_file: JSON test data
  • mock_wandb: Weights & Biases mocking
  • mock_torch_save / mock_torch_load: PyTorch I/O mocking
  • device / sample_tensor / sample_batch: PyTorch helpers
  • reset_torch_seed: Reproducible tests
  • capture_logs: Log output testing
  • mock_os_environ: Environment variable testing

Additional Files

  • CLAUDE.md: Documentation for common testing commands
  • .gitignore: Updated with testing artifacts and Claude settings

How to Use

Install Dependencies

poetry install

Run Tests

# Run all tests
poetry run test
# or
poetry run tests

# Run with coverage
poetry run pytest --cov

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

# Run specific file
poetry run pytest tests/test_file.py

View Coverage Report

After running tests with coverage, open htmlcov/index.html in a browser to view the detailed coverage report.

Validation

The infrastructure includes validation tests (test_infrastructure_validation.py) that verify:

  • All testing dependencies are properly installed
  • Project structure is correctly set up
  • Fixtures are working as expected
  • Custom markers are configured
  • Coverage reporting functions properly

Notes

  • The project uses Poetry for dependency management, which provides better dependency resolution and lock file support compared to pip
  • Coverage threshold is set to 80% to encourage comprehensive testing
  • The infrastructure is ready for immediate test development - developers can start writing tests in the unit/ or integration/ directories
  • All test-related artifacts (.pytest_cache, coverage reports, etc.) are properly gitignored

llbbl avatar Jun 26 '25 18:06 llbbl