BGRL_Pytorch icon indicating copy to clipboard operation
BGRL_Pytorch copied to clipboard

feat: Set up comprehensive Python testing infrastructure

Open llbbl opened this issue 3 months ago • 0 comments

Set up Python Testing Infrastructure

Summary

This PR establishes a complete testing infrastructure for the BGRL PyTorch project, providing developers with everything needed to start writing and running tests immediately.

Changes Made

Package Management

  • Added Poetry configuration via pyproject.toml with project metadata and dependency management
  • Migrated existing dependencies (torch, numpy, tensorboardX) to Poetry format
  • Added testing dependencies as dev dependencies:
    • pytest (main testing framework)
    • pytest-cov (coverage reporting)
    • pytest-mock (mocking utilities)

Testing Configuration

  • Comprehensive pytest configuration in pyproject.toml:

    • Test discovery patterns for files, classes, and functions
    • Coverage reporting with 80% threshold requirement
    • HTML and XML coverage report generation
    • Custom test markers: unit, integration, slow
    • Strict configuration for better test reliability
  • Coverage configuration with:

    • Source directory inclusion/exclusion rules
    • Exclusion of test files, virtual environments, and build artifacts
    • Multiple report formats (terminal, HTML, XML)

Directory Structure

  • Created testing directory hierarchy:
    tests/
    ├── __init__.py
    ├── conftest.py
    ├── test_infrastructure.py
    ├── unit/
    │   └── __init__.py
    └── integration/
        └── __init__.py
    

Shared Testing Fixtures

  • Comprehensive conftest.py with fixtures for:
    • Temporary directories for test isolation
    • Sample graph data generation (nodes, edges, features)
    • Mock configurations and command-line arguments
    • TensorBoard writer mocking
    • Random seed management for reproducible tests
    • Mock PyTorch devices and tensor data

Development Scripts

  • Poetry script commands:
    • poetry run test - Run all tests
    • poetry run tests - Alternative command (both work identically)
    • All standard pytest options available

Infrastructure Files

  • Updated .gitignore with comprehensive patterns for:
    • Testing artifacts (.pytest_cache/, .coverage, htmlcov/)
    • Python build artifacts and virtual environments
    • IDE and OS files
    • Model checkpoints and data directories
    • Claude Code settings

Validation

  • Created test_infrastructure.py with validation tests that verify:
    • pytest and PyTorch functionality
    • All fixtures work correctly
    • Test markers function properly
    • Random seed reproducibility
    • Class-based testing capabilities

Instructions for Running Tests

Initial Setup

# Install dependencies
poetry install

# Run all tests
poetry run test

# Or alternatively
poetry run tests

Test Commands

# Run specific test markers
poetry run pytest -m unit          # Unit tests only
poetry run pytest -m integration   # Integration tests only  
poetry run pytest -m "not slow"    # Skip slow tests

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

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

# Run with verbose output
poetry run pytest -v

Coverage Reports

  • Terminal output: Shows coverage summary after each test run
  • HTML report: Generated in htmlcov/ directory
  • XML report: Generated as coverage.xml for CI integration

Dependencies Added

Production Dependencies

  • torch ^2.0.0
  • numpy ^1.21.0
  • tensorboardX ^2.4

Development Dependencies

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

Notes

  • Poetry lock file (poetry.lock) is included in version control for reproducible builds
  • Coverage threshold set to 80% - tests will fail if coverage drops below this
  • Test isolation ensured through comprehensive fixtures and temporary directories
  • Ready for CI/CD - XML coverage reports can be integrated with code quality tools
  • Extensible - Easy to add new fixtures and test categories as needed

Validation Results

✅ All 13 validation tests pass ✅ Both poetry run test and poetry run tests commands work ✅ Coverage reporting generates properly (HTML and XML) ✅ All pytest markers function correctly ✅ Fixture isolation and reproducibility verified

The testing infrastructure is now ready for developers to start writing comprehensive tests for the BGRL implementation.

llbbl avatar Sep 03 '25 14:09 llbbl