gnn-model-explainer icon indicating copy to clipboard operation
gnn-model-explainer copied to clipboard

feat: Set up comprehensive Python testing infrastructure with Poetry

Open llbbl opened this issue 8 months ago • 0 comments

Set up Python Testing Infrastructure

Summary

This PR establishes a comprehensive testing infrastructure for the GNN Explainer Python 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
  • Dependency Migration: Migrated existing dependencies from requirements.txt to Poetry format
  • Development Dependencies: Added pytest, pytest-cov, and pytest-mock as dev dependencies

Testing Framework Setup

  • pytest Configuration:

    • Configured test discovery patterns
    • Added custom markers (unit, integration, slow)
    • Set up coverage reporting with multiple output formats
    • Configured strict mode for markers and configuration
  • Coverage Configuration:

    • Set 80% coverage threshold
    • Configured HTML and XML report generation
    • Added exclusion patterns for test files and boilerplate code

Directory Structure

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

Testing Fixtures (conftest.py)

  • temp_dir: Temporary directory management
  • mock_config: Mock configuration object
  • sample_graph: NetworkX graph for testing
  • sample_tensor_data: PyTorch tensor data samples
  • mock_model: Simple neural network model
  • capture_stdout: Stdout capture utility
  • reset_random_seeds: Reproducibility fixture
  • mock_data_loader: PyTorch DataLoader mock
  • cleanup_files: File cleanup helper
  • mock_logger: Logger mocking

Additional Setup

  • Updated .gitignore with testing artifacts and Poetry files
  • Created validation tests to verify the infrastructure works correctly
  • Configured Poetry scripts for running tests

How to Use

Install Dependencies

poetry install

Run Tests

Both commands work:

poetry run test
poetry run tests

Run Tests with Coverage

poetry run pytest --cov=explainer --cov=utils --cov-report=html

Run Specific Test Categories

poetry run pytest -m unit        # Run only unit tests
poetry run pytest -m integration # Run only integration tests
poetry run pytest -m "not slow"  # Skip slow tests

Notes

  • The testing infrastructure is designed to work even when main project dependencies aren't fully installed
  • Fixtures gracefully handle missing dependencies (e.g., torch, networkx)
  • Coverage reporting is configured but can be customized per project needs
  • The validation test suite ensures all components work correctly

Next Steps

Developers can now immediately start writing tests by:

  1. Creating test files in tests/unit/ or tests/integration/
  2. Using the provided fixtures from conftest.py
  3. Running tests with the configured commands

The infrastructure is ready for Test-Driven Development (TDD) practices.

llbbl avatar Jun 24 '25 15:06 llbbl