Skip to content

feat: Set up complete Python testing infrastructure with Poetry#7

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

feat: Set up complete Python testing infrastructure with Poetry#7
llbbl wants to merge 1 commit intowyndwarrior:masterfrom
UnitSeeker:add-testing-infrastructure

Conversation

@llbbl
Copy link
Copy Markdown

@llbbl llbbl commented Aug 23, 2025

Python Testing Infrastructure Setup

Summary

This PR establishes a comprehensive testing infrastructure for the 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, custom fixtures, and organized test structure.

Changes Made

Package Management

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

Testing Framework

  • pytest Configuration:

    • Configured pytest with comprehensive options in pyproject.toml
    • Added custom markers for test categorization (unit, integration, slow, skip_ci)
    • Set up verbose output, color support, and failure limits
  • Coverage Reporting:

    • Configured coverage to track all source packages (traj2vec, mpc, scripts, exps)
    • Set 80% coverage threshold
    • Enabled HTML, XML, and terminal coverage reports
    • Excluded non-essential files from coverage

Test Structure

tests/
├── __init__.py
├── conftest.py                 # Shared fixtures and configuration
├── test_infrastructure_validation.py  # Validation tests
├── unit/                       # Unit tests directory
│   └── __init__.py
└── integration/                # Integration tests directory
    └── __init__.py

Shared Fixtures (conftest.py)

  • temp_dir: Temporary directory management
  • mock_config: Mock configuration dictionary
  • sample_data: NumPy array test data
  • sample_tensors: PyTorch tensor test data
  • mock_environment: Mock Gym environment
  • mock_model: Mock neural network model
  • mock_dataset: Mock dataset object
  • test_files: Test file creation utilities
  • capture_logs: Log capture for testing
  • performance_timer: Performance measurement utilities
  • Auto-fixtures: Automatic random seed reset for reproducibility

Development Dependencies

  • Testing: pytest, pytest-cov, pytest-mock, pytest-xdist, pytest-timeout
  • Code Quality: black, isort, flake8, mypy
  • Pre-commit: pre-commit hooks support

Configuration Files

  • pyproject.toml: Central configuration for Poetry, pytest, coverage, black, isort, and mypy
  • .gitignore: Updated with comprehensive testing and development exclusions

Usage Instructions

Installation

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

# Install project dependencies
poetry install

# Install only development dependencies
poetry install --only dev

Running Tests

# Run all tests
poetry run test

# Alternative command
poetry run tests

# Run with specific options
poetry run pytest tests/ -v

# Run only unit tests
poetry run pytest -m unit

# Run only integration tests  
poetry run pytest -m integration

# Run with coverage report
poetry run pytest --cov

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

Coverage Reports

  • Terminal: Displayed automatically when running tests
  • HTML Report: Generated at htmlcov/index.html
  • XML Report: Generated at coverage.xml (for CI integration)

Writing New Tests

  1. Create test files in appropriate directories (tests/unit/ or tests/integration/)
  2. Use shared fixtures from conftest.py
  3. Apply appropriate markers (@pytest.mark.unit, @pytest.mark.integration, etc.)
  4. Follow naming convention: test_*.py for files, test_* for functions

Validation

The infrastructure has been validated with test_infrastructure_validation.py which verifies:

  • Project imports work correctly
  • All fixtures are functional
  • Test markers are properly defined
  • Coverage configuration is active
  • Directory structure is correct

Notes

  • The project uses Python 3.8.1+ for compatibility with all dependencies
  • PyTorch and some system dependencies may require manual installation depending on the environment
  • The poetry.lock file should be committed to ensure reproducible builds
  • Coverage threshold is set to 80% but can be adjusted in pyproject.toml

Next Steps

With this infrastructure in place, developers can now:

  1. Write unit tests for individual functions and classes
  2. Create integration tests for module interactions
  3. Add performance tests using the provided timer fixture
  4. Implement mocking strategies using the mock fixtures
  5. Generate coverage reports to identify untested code paths

- Added Poetry as package manager with pyproject.toml configuration
- Configured pytest with coverage reporting and custom markers
- Created comprehensive test directory structure (unit/integration)
- Added shared pytest fixtures in conftest.py for common test utilities
- Implemented validation tests to verify infrastructure setup
- Updated .gitignore with testing and development-related entries
- Configured test scripts accessible via `poetry run test/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