graphtransformer icon indicating copy to clipboard operation
graphtransformer copied to clipboard

feat: Add comprehensive Python testing infrastructure with Poetry

Open llbbl opened this issue 5 months ago • 0 comments

Add Python Testing Infrastructure

Summary

This PR sets up a comprehensive testing infrastructure for the Graph Transformer project. While the project uses Conda for its main dependencies, I've added Poetry alongside it specifically for managing testing and development tools, providing a modern Python testing environment.

Changes Made

Package Management

  • Added pyproject.toml with Poetry configuration
  • Configured Poetry to work alongside the existing Conda environment setup
  • Note: DGL and other conda-specific packages remain managed by Conda

Testing Dependencies

  • pytest: Main testing framework
  • pytest-cov: Coverage reporting with 80% threshold
  • pytest-mock: Mocking utilities for unit tests

Testing Configuration

  • Configured pytest settings in pyproject.toml:
    • Test discovery patterns for test_*.py and *_test.py
    • Coverage reporting (terminal, HTML, XML)
    • Custom markers: unit, integration, slow
    • Strict mode with detailed output
    • Coverage fail threshold set to 80%

Directory Structure

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

Shared Fixtures (conftest.py)

  • temp_dir: Temporary directory management
  • mock_config: Configuration dictionary for tests
  • sample_graph_data: Graph data structures for testing
  • sample_molecular_data: Molecular data for testing
  • mock_model: PyTorch model mock
  • mock_dataloader: DataLoader mock
  • sample_checkpoint: Model checkpoint fixture
  • device: CPU device for consistent testing
  • random_seed: Reproducible random seeds
  • cleanup_cuda: Automatic CUDA cache cleanup
  • mock_tensorboard_writer: TensorBoard writer mock

Development Tools

  • Added linting (flake8), formatting (black), type checking (mypy)
  • Updated .gitignore with testing artifacts and build files
  • Added Poetry lock file to gitignore (as requested)

How to Use

Initial Setup

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

# Install testing dependencies
poetry install

# For the main project dependencies, continue using Conda:
conda env create -f environment_cpu.yml  # or environment_gpu.yml

Running Tests

# Run all tests
poetry run test
# or
poetry run tests

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

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

Writing Tests

  1. Place unit tests in tests/unit/
  2. Place integration tests in tests/integration/
  3. Use the provided fixtures from conftest.py
  4. Mark tests appropriately with @pytest.mark.unit, @pytest.mark.integration, or @pytest.mark.slow

Notes

  • The project maintains its original Conda-based dependency management for scientific packages
  • Poetry is used specifically for testing and development tools
  • DGL (Deep Graph Library) must still be installed via Conda as it's not available on PyPI
  • The validation test file (test_setup_validation.py) verifies the testing setup works correctly

Next Steps

Developers can now immediately start writing tests for the codebase using this infrastructure. The 80% coverage threshold will help maintain code quality as the project evolves.

llbbl avatar Jun 20 '25 19:06 llbbl