Skip to content

Conversation

@llbbl
Copy link

@llbbl llbbl commented Jun 24, 2025

Add Python Testing Infrastructure

Summary

This PR establishes a comprehensive testing infrastructure for the Vision Transformer (ViT) explainability project using Poetry as the package manager and pytest as the testing framework.

Changes Made

Package Management

  • Poetry Configuration: Created pyproject.toml with Poetry setup as the primary package manager
  • Dependencies: Migrated project dependencies (torch, torchvision, Pillow, numpy, opencv-python) to Poetry
  • Development Dependencies: Added pytest, pytest-cov, and pytest-mock as dev dependencies

Testing Framework Setup

  • pytest Configuration:

    • Configured test discovery patterns
    • Set up coverage reporting with 80% threshold
    • Added custom markers: unit, integration, and slow
    • Configured both HTML and XML coverage report generation
  • Directory Structure:

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

Test Fixtures

Created comprehensive fixtures in conftest.py:

  • temp_dir: Temporary directory management
  • sample_image: Test image generation
  • sample_tensor: PyTorch tensor creation
  • mock_model: Mock Vision Transformer model
  • attention_rollout_config & grad_rollout_config: Test configurations
  • mock_attention_weights & mock_gradients: Mock data for testing
  • output_dir: Test output directory management
  • reset_torch_hub_dir: Prevents model downloads during tests

Additional Configuration

  • Updated .gitignore: Added Python testing artifacts, virtual environments, and IDE files
  • Poetry Scripts: Configured both poetry run test and poetry run tests commands

How to Use

  1. Install dependencies:

    poetry install
  2. Run all tests:

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

    # Only unit tests
    poetry run pytest -m unit
    
    # Skip slow tests
    poetry run pytest -m "not slow"
    
    # Only integration tests
    poetry run pytest -m integration
  4. View coverage report:

    # After running tests, open the HTML report
    open htmlcov/index.html

Notes

  • The testing infrastructure is ready for immediate use
  • Coverage is configured with an 80% threshold
  • The validation test suite verifies the setup is working correctly
  • OpenCV (cv2) may require additional system dependencies on some platforms
  • Poetry.lock file is created and should be committed to ensure reproducible builds

Next Steps

Developers can now start writing unit and integration tests for the existing modules:

  • vit_explain.py
  • vit_grad_rollout.py
  • vit_rollout.py

- Configure Poetry as package manager with pyproject.toml
- Add testing dependencies (pytest, pytest-cov, pytest-mock)
- Configure pytest with coverage reporting and custom markers
- Create tests directory structure with unit/integration subdirectories
- Add comprehensive test fixtures in conftest.py
- Create validation tests to verify infrastructure setup
- Update .gitignore with testing and Python-related entries
- Configure test commands: `poetry run test` and `poetry run tests`
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