Skip to content

feat: Set up comprehensive Python testing infrastructure with Poetry#60

Open
llbbl wants to merge 1 commit intoJiahuiYu:masterfrom
UnitSeeker:add-testing-infrastructure
Open

feat: Set up comprehensive Python testing infrastructure with Poetry#60
llbbl wants to merge 1 commit intoJiahuiYu:masterfrom
UnitSeeker:add-testing-infrastructure

Conversation

@llbbl
Copy link

@llbbl llbbl commented Jun 26, 2025

Add Python Testing Infrastructure

Summary

This PR sets up a comprehensive testing infrastructure for the AutoSlim project using Poetry as the package manager and pytest as the testing framework. The setup provides a solid foundation for writing and maintaining tests throughout the project.

Changes Made

Package Management

  • Poetry Configuration: Added pyproject.toml with Poetry setup as the primary package manager
  • Testing Dependencies: Added pytest, pytest-cov, and pytest-mock as development dependencies

Testing Configuration

  • pytest Configuration: Configured in pyproject.toml with:

    • Test discovery patterns for test_*.py and *_test.py files
    • Coverage reporting with HTML and XML output formats
    • Custom markers for unit, integration, and slow tests
    • Strict configuration and verbose output
  • Coverage Settings:

    • Configured to cover models and utils directories
    • Excluded test files, cache, and site-packages from coverage
    • Coverage threshold currently set to 0% (should be increased as tests are added)

Directory Structure

tests/
├── __init__.py
├── conftest.py
├── test_setup_validation.py
├── unit/
│   └── __init__.py
└── integration/
    └── __init__.py

Test Fixtures (conftest.py)

Created comprehensive fixtures including:

  • temp_dir: Temporary directory management
  • mock_config: Mock configuration objects
  • mock_model: Mock PyTorch model
  • sample_data: Sample tensor data (with fallback for non-torch environments)
  • mock_dataset & mock_dataloader: Mock data loading utilities
  • mock_optimizer & mock_loss_function: Mock training utilities
  • mock_meter: Mock metrics tracking
  • capture_stdout: Stdout capture for testing print statements
  • mock_file_system: Mock file system structure
  • environment_variables: Test environment setup

Additional Setup

  • Updated .gitignore with:

    • Testing artifacts (.pytest_cache/, .coverage, htmlcov/, coverage.xml)
    • Claude settings (.claude/*)
    • Python artifacts, virtual environments, IDE files, and OS files
  • Added validation tests to ensure the infrastructure works correctly

How to Use

Running Tests

You can run tests using either of these commands:

poetry run test
poetry run tests

Both commands are configured and will execute the same pytest runner.

Running Specific Test Types

# Run only unit tests
poetry run pytest -m unit

# Run only integration tests  
poetry run pytest -m integration

# Run tests excluding slow ones
poetry run pytest -m "not slow"

Coverage Reports

After running tests, coverage reports are available in:

  • Terminal: Shown directly in the output
  • HTML: Open htmlcov/index.html in a browser
  • XML: Available at coverage.xml for CI/CD integration

Next Steps

  1. Increase Coverage Threshold: Currently set to 0% in pyproject.toml. As tests are added, increase the --cov-fail-under value to maintain code quality.

  2. Write Tests: The infrastructure is ready. Developers can now:

    • Add unit tests in tests/unit/
    • Add integration tests in tests/integration/
    • Use the fixtures from conftest.py for common testing scenarios
  3. CI/CD Integration: The setup is compatible with most CI/CD systems. The XML coverage report can be used for coverage tracking in CI pipelines.

Notes

  • The sample_data fixture gracefully handles environments without PyTorch installed
  • All validation tests pass, confirming the infrastructure is properly configured
  • Poetry lock file should be committed to ensure consistent dependencies across environments

- Add Poetry configuration with testing dependencies (pytest, pytest-cov, pytest-mock)
- Configure pytest with coverage reporting, test discovery, and custom markers
- Create test directory structure with unit/integration subdirectories
- Add comprehensive test fixtures in conftest.py for common testing needs
- Update .gitignore with testing artifacts and Claude settings
- Add validation tests to verify infrastructure setup
- Configure Poetry scripts for running tests with 'poetry run test/tests'
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