Skip to content

Conversation

@llbbl
Copy link

@llbbl llbbl commented Jun 27, 2025

Add Python Testing Infrastructure

Summary

This PR sets up a complete testing infrastructure for the neural network pruning project using Poetry as the package manager and pytest as the testing framework.

Changes Made

Package Management

  • Poetry Setup: Created pyproject.toml with Poetry configuration
  • Dependencies: Migrated existing dependencies (torch, torchvision, numpy) to Poetry
  • Testing Dependencies: Added pytest, pytest-cov, and pytest-mock as development dependencies

Testing Configuration

  • pytest Configuration in pyproject.toml:

    • Test discovery patterns for multiple naming conventions
    • Coverage reporting with HTML and XML outputs
    • Custom markers for test categorization (unit, integration, slow)
    • Strict mode enabled with colored output
  • Coverage Configuration:

    • Source directories and exclusion patterns defined
    • Coverage threshold set to 0% for validation phase
    • Multiple reporting formats configured

Directory Structure

tests/
├── __init__.py
├── conftest.py              # Shared pytest fixtures
├── unit/                    # Unit tests directory
│   └── __init__.py
├── integration/             # Integration tests directory
│   └── __init__.py
├── test_setup_validation.py # Infrastructure validation tests
└── README.md               # Testing documentation

Shared Fixtures (conftest.py)

Created comprehensive fixtures for common testing needs:

  • General: temp_dir, random_seed, device
  • PyTorch Models: mock_model, sample_tensor, sample_batch, mock_dataloader
  • Training: mock_optimizer, mock_scheduler, checkpoint_dir, sample_checkpoint
  • Utilities: mock_args, capture_stdout, tensor_shapes

Additional Setup

  • Updated .gitignore with testing-related entries and .claude directory
  • Created validation tests to verify all fixtures work correctly
  • Added testing documentation in tests/README.md

How to Use

Running Tests

# Install dependencies
poetry install

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

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

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

Writing Tests

Developers can now immediately start writing tests using the provided fixtures:

import pytest

class TestMyFeature:
    @pytest.mark.unit
    def test_model_forward(self, mock_model, sample_tensor):
        # Test implementation
        pass

Validation Results

All 17 validation tests pass successfully, confirming:

  • ✅ All dependencies installed correctly
  • ✅ pytest configuration working
  • ✅ All fixtures functioning properly
  • ✅ Test markers available
  • ✅ Coverage reporting configured

Notes

  • Poetry lock file (poetry.lock) is included and should be committed
  • Coverage threshold is set to 0% initially - should be increased as tests are added
  • The infrastructure is ready for immediate test development

- Set up Poetry as package manager with pyproject.toml
- Add testing dependencies: pytest, pytest-cov, pytest-mock
- Configure pytest with markers (unit, integration, slow)
- Create testing directory structure with unit/integration subdirs
- Add comprehensive shared fixtures in conftest.py
- Configure coverage reporting with HTML/XML outputs
- Create validation tests to verify infrastructure
- Update .gitignore with testing and Claude entries
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