Skip to content

feat: Set up comprehensive Python testing infrastructure with Poetry#46

Open
llbbl wants to merge 1 commit intofacebookresearch:mainfrom
UnitSeeker:add-testing-infrastructure
Open

feat: Set up comprehensive Python testing infrastructure with Poetry#46
llbbl wants to merge 1 commit intofacebookresearch:mainfrom
UnitSeeker:add-testing-infrastructure

Conversation

@llbbl
Copy link
Copy Markdown

@llbbl llbbl commented Jun 15, 2025

Set Up Python Testing Infrastructure

Summary

This PR establishes a comprehensive testing infrastructure for the hiera-transformer project using Poetry as the package manager and pytest as the testing framework. The setup provides a solid foundation for writing and maintaining tests with proper coverage reporting and organization.

Changes Made

Package Management

  • Migrated to Poetry: Created pyproject.toml with all dependencies from setup.py
  • Preserved existing metadata: Maintained all project information, classifiers, and dependencies
  • Added development dependencies: pytest, pytest-cov, and pytest-mock for testing

Testing Configuration

  • Pytest configuration in pyproject.toml:

    • Test discovery patterns for test_*.py and *_test.py files
    • Coverage reporting with 80% threshold
    • HTML and XML coverage report generation
    • Custom markers: unit, integration, and slow
    • Strict marker enforcement and verbose output
  • Coverage configuration:

    • Source set to hiera package
    • Exclusions for test files, examples, and boilerplate code
    • Branch coverage enabled
    • Multiple output formats (terminal, HTML, XML)

Directory Structure

tests/
├── __init__.py
├── conftest.py          # Shared fixtures and configuration
├── test_setup_validation.py  # Infrastructure validation tests
├── unit/
│   ├── __init__.py
│   └── test_example.py  # Example unit test file
└── integration/
    └── __init__.py

Shared Fixtures (conftest.py)

  • temp_dir: Temporary directory for test files
  • mock_model_config: Mock Hiera model configuration
  • device: PyTorch device (CPU/CUDA) detection
  • sample_image_tensor: Sample image tensor for testing
  • mock_checkpoint_path: Mock checkpoint file creation
  • reset_random_state: Ensures reproducible tests
  • capture_stdout: Capture print statements
  • mock_timm_model: Mock timm model creation

Development Commands

  • poetry run test - Run all tests
  • poetry run tests - Alternative command (both work)
  • All standard pytest options are available

Additional Setup

  • Updated .gitignore with:
    • Testing artifacts (.pytest_cache/, .coverage, htmlcov/, coverage.xml)
    • Claude settings (.claude/*)
    • Virtual environments and IDE files
    • Build artifacts

How to Use

Installation

# Install all dependencies including dev dependencies
poetry install

# Or install only production dependencies
poetry install --no-dev

Running Tests

# Run all tests
poetry run test

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

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

# Run with coverage report only
poetry run pytest --cov=hiera --cov-report=term

Writing New Tests

  1. Create test files in tests/unit/ or tests/integration/
  2. Use appropriate markers (@pytest.mark.unit, @pytest.mark.integration, @pytest.mark.slow)
  3. Utilize shared fixtures from conftest.py
  4. Follow the example in tests/unit/test_example.py

Notes

  • The testing infrastructure is ready for immediate use
  • Coverage threshold is set to 80% but can be adjusted in pyproject.toml
  • The validation tests ensure all components are properly configured
  • Poetry lock file is committed to ensure reproducible builds
  • No actual unit tests for the codebase were written - only infrastructure setup

Testing the Setup

The PR includes validation tests that verify:

  • All testing dependencies are installed
  • Custom fixtures are available and working
  • Test markers are properly configured
  • Directory structure is correct
  • Coverage reporting is functional

Run poetry run test after installation to verify everything works correctly.

- Migrated from setup.py to Poetry-based dependency management
- Added pytest, pytest-cov, and pytest-mock as dev dependencies
- Configured pytest with coverage reporting and custom markers
- Created testing directory structure with unit/integration separation
- Added shared fixtures in conftest.py for common test scenarios
- Updated .gitignore with testing and Claude-specific entries
- Added validation tests to verify infrastructure setup
@facebook-github-bot
Copy link
Copy Markdown

Hi @llbbl!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at [email protected]. Thanks!

@facebook-github-bot
Copy link
Copy Markdown

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants