Skip to content

feat: Add comprehensive Python testing infrastructure with Poetry#9

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

feat: Add comprehensive Python testing infrastructure with Poetry#9
llbbl wants to merge 1 commit into
JialeTao:masterfrom
UnitSeeker:add-testing-infrastructure

Conversation

@llbbl

@llbbl llbbl commented Aug 23, 2025

Copy link
Copy Markdown

Add Python Testing Infrastructure

Summary

This PR establishes a comprehensive testing infrastructure for the Python project using Poetry as the package manager and pytest as the testing framework. The setup provides a solid foundation for writing and running tests with coverage reporting, mocking capabilities, and organized test structure.

Changes Made

Package Management

  • Poetry Setup: Configured Poetry as the package manager with pyproject.toml
  • Dependency Migration: Migrated existing dependencies to Poetry format
  • Lock File: Generated poetry.lock for reproducible installations

Testing Framework

  • pytest: Main testing framework with comprehensive configuration
  • pytest-cov: Coverage reporting with HTML and XML output formats
  • pytest-mock: Mocking utilities for unit testing

Testing Configuration (pyproject.toml)

  • pytest settings: Test discovery patterns, output formatting, and strict options
  • Coverage settings:
    • Source directories configuration
    • Exclusion patterns for non-testable code
    • HTML and XML report generation
    • Coverage thresholds (configurable, currently set to 80%)
  • Custom markers: unit, integration, and slow for test categorization

Project Structure

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

Shared Fixtures (conftest.py)

  • temp_dir: Temporary directory management
  • sample_config: Mock configuration dictionary
  • mock_config_file: YAML config file fixture
  • sample_image_tensor: PyTorch tensor fixtures
  • sample_batch: Batch data fixtures
  • sample_keypoints: Keypoint data fixtures
  • mock_checkpoint: Model checkpoint fixture
  • sample_video_frames: Video frame data fixture
  • mock_dataset_path: Dataset directory structure
  • mock_logger: Logging fixture
  • reset_random_seeds: Reproducible random seeds
  • Device and GPU availability fixtures

Development Tools

  • Makefile: Convenient commands for common tasks
    • make install: Install dependencies
    • make test / make tests: Run all tests
    • make test-unit: Run unit tests only
    • make test-integration: Run integration tests only
    • make coverage: Generate coverage report
    • make lint: Run code linting
    • make format: Format code with black and isort
    • make typecheck: Run type checking
    • make clean: Clean cache and build files

Additional Configuration

  • Code formatting: Black and isort configuration
  • Type checking: mypy configuration
  • Linting: flake8 configuration
  • .gitignore: Updated with testing artifacts and Poetry files

How to Use

Installation

# Install Poetry (if not already installed)
curl -sSL https://install.python-poetry.org | python3 -

# Install project dependencies
poetry install --no-root

# Or using make
make install

Running Tests

# Run all tests
poetry run pytest
# or
make test

# Run specific test categories
poetry run pytest -m unit        # Unit tests only
poetry run pytest -m integration # Integration tests only
poetry run pytest -m slow        # Slow tests only

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

# Generate coverage report
make coverage

Writing Tests

  1. Place unit tests in tests/unit/
  2. Place integration tests in tests/integration/
  3. Use fixtures from conftest.py for common test data
  4. Mark tests appropriately:
    @pytest.mark.unit
    def test_my_unit_test():
        assert True
    
    @pytest.mark.integration
    def test_my_integration_test():
        assert True

Validation

The setup includes test_setup_validation.py which verifies:

  • Python version compatibility (3.8+)
  • All testing packages are installed correctly
  • All fixtures work as expected
  • Project imports are accessible
  • Test markers are properly configured

Run validation: poetry run pytest tests/test_setup_validation.py -v

Dependencies Added

Testing Dependencies (dev)

  • pytest ^7.4.0
  • pytest-cov ^4.1.0
  • pytest-mock ^3.11.1

Code Quality Tools (dev)

  • black ^23.7.0
  • flake8 ^6.0.0
  • mypy ^1.4.0
  • isort ^5.12.0

Notes

  • The testing infrastructure is ready for immediate use
  • No actual unit tests for the codebase were written - only infrastructure setup
  • Coverage is configured but will show 0% until actual tests are added
  • All tools are configured with sensible defaults that can be customized as needed
  • The setup supports both local development and CI/CD integration

- Set up Poetry package manager with pyproject.toml configuration
- Add testing dependencies (pytest, pytest-cov, pytest-mock)
- Configure pytest with coverage reporting and custom markers
- Create testing directory structure (tests/unit, tests/integration)
- Add comprehensive pytest fixtures in conftest.py
- Update .gitignore with testing and Poetry artifacts
- Add Makefile with convenient testing commands
- Include validation tests to verify setup
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