Skip to content

Conversation

@llbbl
Copy link

@llbbl llbbl commented Jun 26, 2025

Add Python Testing Infrastructure

Summary

This PR sets up a complete testing infrastructure for the PyTorch deep learning project using Poetry as the package manager and pytest as the testing framework.

Changes Made

Package Management

  • ✅ Added pyproject.toml with Poetry configuration
  • ✅ Migrated from requirements-based to Poetry dependency management
  • ✅ Added PyTorch and torchvision as main dependencies

Testing Dependencies

  • ✅ Added pytest (v7.4.0) - Main testing framework
  • ✅ Added pytest-cov (v4.1.0) - Coverage reporting
  • ✅ Added pytest-mock (v3.11.1) - Mocking utilities
  • All testing dependencies are properly isolated as development dependencies

Testing Configuration

  • ✅ Configured pytest with:

    • Test discovery patterns for test_*.py and *_test.py files
    • Coverage settings with HTML and XML report generation
    • Custom markers: unit, integration, slow
    • Strict mode for better error detection
  • ✅ Configured coverage with:

    • Source directory targeting the code/ directory
    • Exclusion patterns for test files and virtual environments
    • Branch coverage enabled
    • Multiple report formats (terminal, HTML, XML)

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 management
  • sample_config - Mock configuration dictionary
  • mock_dataset_path - Creates mock dataset structure
  • sample_tensor - Sample PyTorch tensors for testing
  • sample_labels - Sample label tensors
  • device - Consistent device selection (CPU)
  • reset_random_seeds - Reproducibility fixture
  • mock_model_checkpoint - Mock model checkpoint creation
  • capture_stdout - Stdout capture for testing print statements

Development Commands

  • poetry run test - Run all tests
  • poetry run tests - Alternative command (both work)
  • poetry run pytest -m unit - Run only unit tests
  • poetry run pytest -m integration - Run only integration tests
  • poetry run pytest -m slow - Run only slow tests

Additional Updates

  • ✅ Updated .gitignore with:
    • Testing artifacts (.pytest_cache/, .coverage, htmlcov/, etc.)
    • Build artifacts and virtual environments
    • Claude settings (.claude/*)
    • Note: poetry.lock is NOT ignored (as intended)

How to Use

  1. Install dependencies:

    poetry install
  2. Run all tests:

    poetry run test
    # or
    poetry run tests
  3. Run specific test markers:

    poetry run pytest -m unit
    poetry run pytest -m integration
    poetry run pytest -m "not slow"
  4. Generate coverage reports:

    poetry run pytest --cov-report=html
    # View report at htmlcov/index.html

Validation

The infrastructure has been validated with 15 passing tests that verify:

  • ✅ Pytest installation and execution
  • ✅ All fixtures are accessible and working
  • ✅ PyTorch integration
  • ✅ Custom markers registration
  • ✅ Coverage configuration
  • ✅ Directory structure creation

Notes

  • Coverage threshold is initially set to 0% to allow gradual test implementation
  • The infrastructure is ready for immediate test development
  • All common testing patterns are supported through the provided fixtures
  • The setup follows Python testing best practices

- Set up Poetry as package manager with pyproject.toml configuration
- Add pytest, pytest-cov, and pytest-mock as dev dependencies
- Configure pytest with markers (unit, integration, slow) and coverage settings
- Create tests directory structure with conftest.py containing shared fixtures
- Add validation tests to verify infrastructure setup
- Update .gitignore with testing and build artifacts
- Configure Poetry scripts for running tests with 'poetry run test[s]'
@UchihaBaiQing
Copy link

UchihaBaiQing commented Jun 26, 2025 via email

@llbbl
Copy link
Author

llbbl commented Jun 30, 2025

不客气

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.

2 participants