Skip to content

test: Add comprehensive testing infrastructure with Poetry#30

Open
llbbl wants to merge 1 commit intoKaiyangZhou:masterfrom
UnitSeeker:add-testing-infrastructure
Open

test: Add comprehensive testing infrastructure with Poetry#30
llbbl wants to merge 1 commit intoKaiyangZhou:masterfrom
UnitSeeker:add-testing-infrastructure

Conversation

@llbbl
Copy link

@llbbl llbbl commented Jun 15, 2025

Add Comprehensive Testing Infrastructure

Summary

This PR establishes a complete testing infrastructure for the Center Loss PyTorch implementation project. It sets up Poetry as the package manager and configures pytest with coverage reporting and custom test organization.

Changes Made

Package Management

  • Poetry Setup: Created pyproject.toml with Poetry configuration as the default package manager
  • Dependencies: Migrated project dependencies (torch, torchvision, numpy, matplotlib) to Poetry
  • Dev Dependencies: Added pytest, pytest-cov, and pytest-mock as development dependencies

Testing Configuration

  • pytest Configuration:

    • Configured test discovery patterns and paths
    • Set up coverage reporting with 80% threshold
    • Added HTML and XML coverage report generation
    • Configured custom markers: unit, integration, slow
    • Added strict mode options for better test quality
  • Coverage Configuration:

    • Configured source directories and exclusions
    • Set up branch coverage tracking
    • Added common exclusion patterns for better coverage accuracy

Directory Structure

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

Shared Fixtures (conftest.py)

  • temp_dir: Temporary directory for test files
  • device: PyTorch device (CPU/CUDA) detection
  • random_seed: Reproducible random seeds
  • sample_features, sample_labels, sample_centers: Test data generators
  • mock_model_config, mock_training_config: Configuration fixtures
  • sample_mnist_batch: MNIST-like data generator
  • capture_stdout: Stdout capture utility
  • mock_data_loader: Mock data loader factory
  • assert_tensor_close: Tensor comparison helper
  • cleanup_matplotlib: Automatic matplotlib cleanup

Additional Configuration

  • Updated .gitignore with:
    • Testing artifacts (.pytest_cache/, coverage.xml, htmlcov/)
    • Claude settings (.claude/*)
    • IDE files and virtual environments
    • Note: poetry.lock is NOT ignored (should be committed)

How to Use

Running Tests

# Install dependencies
poetry install

# Run all tests
poetry run pytest

# Run with specific markers
poetry run pytest -m unit        # Run only unit tests
poetry run pytest -m integration # Run only integration tests
poetry run pytest -m "not slow"  # Skip slow tests

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

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

Writing Tests

  1. Unit Tests: Place in tests/unit/ directory
  2. Integration Tests: Place in tests/integration/ directory
  3. Use Fixtures: Import from conftest.py for common test scenarios
  4. Mark Tests: Use @pytest.mark.unit, @pytest.mark.integration, or @pytest.mark.slow

Coverage Reports

  • Terminal: Shown automatically after test runs
  • HTML Report: Generated in htmlcov/ directory
  • XML Report: Generated as coverage.xml for CI integration
  • Threshold: Tests will fail if coverage drops below 80%

Validation

The infrastructure has been validated with a comprehensive test suite (test_setup_validation.py) that verifies:

  • All pytest plugins are properly installed
  • Project modules can be imported
  • All fixtures work correctly
  • Test markers are configured
  • Coverage tracking is functional

All validation tests pass successfully ✅

Notes

  • This setup provides a foundation for test-driven development
  • The 80% coverage threshold encourages comprehensive testing
  • Poetry ensures reproducible environments with lock file
  • The infrastructure is ready for immediate test development

- Set up Poetry as package manager with pyproject.toml
- Add pytest, pytest-cov, and pytest-mock as dev dependencies
- Configure pytest with coverage thresholds and custom markers
- Create test directory structure with unit/integration separation
- Add shared fixtures in conftest.py for common test scenarios
- Include validation tests to verify infrastructure setup
- Update .gitignore with testing and Claude-specific entries
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