Skip to content

Conversation

@llbbl
Copy link

@llbbl llbbl commented Jun 29, 2025

Add Python Testing Infrastructure

Summary

This PR sets up a complete testing infrastructure for the CGCNN Python project using Poetry for dependency management and pytest for testing. The infrastructure provides a foundation for writing comprehensive unit and integration tests.

Changes Made

1. Package Management

  • ✅ Created pyproject.toml with Poetry configuration
  • ✅ Migrated dependencies from project imports (PyTorch, NumPy, scikit-learn, pymatgen)
  • ✅ Added development dependencies for testing (pytest, pytest-cov, pytest-mock)

2. Testing Configuration

  • ✅ Configured pytest with:
    • Test discovery patterns for test_*.py and *_test.py files
    • Custom markers: unit, integration, and slow
    • Coverage reporting with HTML and XML outputs
    • Strict mode with verbose output
  • ✅ Configured coverage with:
    • Source directory targeting cgcnn module
    • Exclusion patterns for test files and boilerplate code
    • Coverage threshold set to 0% (with note to increase to 80% when writing actual tests)

3. Directory Structure

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

4. Testing Fixtures

Created comprehensive fixtures in conftest.py:

  • temp_dir: Temporary directory for test files
  • sample_data_dir: Sample data directory with test files
  • mock_crystal_data: Mock crystal structure data
  • sample_model_config: Model configuration for testing
  • sample_training_config: Training configuration
  • mock_torch_model: Simple PyTorch model for testing
  • sample_checkpoint: Model checkpoint file
  • capture_stdout: Stdout capture utility
  • reset_random_seeds: Reproducible test runs
  • disable_cuda: CPU-only testing
  • mock_args: Command line argument mocking

5. Development Commands

Added Poetry script commands:

  • poetry run test - Run all tests
  • poetry run tests - Alternative command (both work)

6. Additional Setup

  • ✅ Updated .gitignore with:
    • Testing artifacts (.pytest_cache/, htmlcov/, coverage.xml)
    • Claude settings (.claude/*)
    • IDE files and OS artifacts
  • ✅ Created validation tests to verify the infrastructure works correctly

Instructions for Running Tests

  1. Install dependencies:

    poetry install
  2. Run all tests:

    poetry run test
    # or
    poetry run tests
  3. Run specific test markers:

    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
  4. View coverage report:

    • HTML report: Open htmlcov/index.html in a browser
    • Terminal report: Automatically shown when running tests
    • XML report: Available at coverage.xml for CI integration

Notes

  • Coverage Threshold: Currently set to 0% to allow infrastructure setup without actual unit tests. Should be increased to 80% when implementing real tests.
  • Python Version: Configured for Python 3.8+ to match PyTorch requirements
  • Dependencies: All existing dependencies from the project have been properly migrated to Poetry
  • Poetry Lock: The poetry.lock file is intentionally NOT gitignored to ensure reproducible builds

Validation

The infrastructure has been validated with 9 passing tests that verify:

  • ✅ Pytest installation and configuration
  • ✅ Project structure integrity
  • ✅ Module importability
  • ✅ Fixture availability
  • ✅ Coverage configuration
  • ✅ Custom marker functionality

All validation tests pass successfully, confirming the testing infrastructure is ready for use.

- Set up Poetry for dependency management with pyproject.toml
- Add pytest, pytest-cov, and pytest-mock as dev dependencies
- Configure pytest with custom markers (unit, integration, slow)
- Set up coverage reporting with HTML and XML outputs
- Create test directory structure with shared fixtures in conftest.py
- Add Poetry script commands for running tests
- Update .gitignore with testing and Claude-specific entries
- Create validation tests to verify infrastructure 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