Skip to content

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

Open
llbbl wants to merge 1 commit into
egyptdj:mainfrom
UnitSeeker:add-testing-infrastructure
Open

feat: Set up comprehensive Python testing infrastructure with Poetry#22
llbbl wants to merge 1 commit into
egyptdj:mainfrom
UnitSeeker:add-testing-infrastructure

Conversation

@llbbl

@llbbl llbbl commented Sep 1, 2025

Copy link
Copy Markdown

Set up Python Testing Infrastructure with Poetry

Summary

This PR establishes a comprehensive testing infrastructure for the Python project using Poetry as the package manager. The setup provides a complete, ready-to-use testing environment that allows developers to immediately start writing and running tests with proper coverage reporting and organization.

Changes Made

Package Management Migration

  • Created pyproject.toml with Poetry configuration
  • Migrated dependencies from requirements.txt to Poetry format
  • Added development dependencies: pytest, pytest-cov, pytest-mock
  • Generated poetry.lock for dependency version locking

Testing Configuration

  • Configured pytest in pyproject.toml with:

    • Test discovery patterns (test_*.py, *_test.py)
    • Coverage reporting (HTML, XML, terminal with 80% threshold)
    • Custom markers: unit, integration, slow
    • Strict configuration options for robust testing
    • Warning filters for clean test output
  • Configured coverage.py with:

    • Source directory inclusion
    • Exclusion patterns for test files and external dependencies
    • Multiple report formats (HTML in htmlcov/, XML as coverage.xml)

Directory Structure

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

Shared Testing Utilities

  • Comprehensive conftest.py with fixtures for:
    • Temporary directories and file management
    • Sample data structures (CSV files, arrays, brain imaging data)
    • Mock objects (loggers, Telegram bots, PyTorch models)
    • Environment variable management
    • Random seed control for reproducible tests
    • Log capturing utilities

Validation & Infrastructure Tests

  • Created validation tests (test_basic_infrastructure.py) to verify:
    • Pytest functionality and Python version compatibility
    • All fixtures work correctly
    • Custom markers function properly
    • Project structure is properly established
    • Configuration file structure is valid

Development Workflow

  • Updated .gitignore with:
    • Testing artifacts (.pytest_cache/, .coverage, htmlcov/)
    • Claude Code settings (.claude/*)
    • All standard Python exclusions maintained

Running Tests

Basic Commands

# Install dependencies (run once)
poetry install

# Run all tests
poetry run pytest

# Run tests with verbose output
poetry run pytest -v

# Run only unit tests
poetry run pytest -m unit

# Run tests with coverage report
poetry run pytest --cov

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

Coverage Reports

  • Terminal: Displayed after each test run with missing line numbers
  • HTML Report: Generated in htmlcov/ directory (open htmlcov/index.html)
  • XML Report: Generated as coverage.xml for CI/CD integration

Custom Markers

  • @pytest.mark.unit - Fast unit tests
  • @pytest.mark.integration - Integration tests
  • @pytest.mark.slow - Tests that take more time

Validation Results

Infrastructure verified: All 14 validation tests pass
Coverage reporting: HTML and XML reports generated successfully
Marker filtering: Custom markers work correctly
Fixture functionality: All shared fixtures operational
Configuration validation: pyproject.toml structure verified

Dependencies Added

Main Dependencies (migrated from requirements.txt)

  • einops==0.4.1, joblib==1.1.0, matplotlib==3.5.2, nilearn==0.9.2
  • nipy==0.5.0, numpy==1.22.3, pandas==1.4.3, pingouin==0.5.3
  • python-telegram-bot==20.3, scikit-learn==1.0.2, seaborn==0.11.2
  • torch==1.12.1, torchvision==0.13.1, tqdm==4.63.0

Test Dependencies (new)

  • pytest==^7.4.0 (testing framework)
  • pytest-cov==^4.1.0 (coverage reporting)
  • pytest-mock==^3.11.0 (mocking utilities)

Next Steps

  1. Write actual tests: Use the established structure to create unit and integration tests
  2. CI/CD Integration: Use coverage.xml for automated coverage reporting
  3. Pre-commit hooks: Consider adding pytest runs to pre-commit workflows
  4. Documentation: Add test-specific documentation as the test suite grows

Notes

  • Poetry Lock File: The poetry.lock file ensures consistent dependency versions across environments
  • Coverage Threshold: Set to 80% - adjust in pyproject.toml if needed
  • Flexible Fixtures: The shared fixtures handle missing dependencies gracefully
  • Extensible Structure: Easy to add new test categories and fixtures as needed

The testing infrastructure is now ready for immediate use. Developers can start writing tests following the established patterns and directory structure.

- Create pyproject.toml with Poetry configuration and dependency management
- Migrate existing requirements.txt dependencies to Poetry
- Add testing dependencies (pytest, pytest-cov, pytest-mock)
- Configure pytest with coverage reporting, custom markers, and strict options
- Set up testing directory structure (tests/, tests/unit/, tests/integration/)
- Create comprehensive shared fixtures in conftest.py for common test utilities
- Add validation tests to verify testing infrastructure functionality
- Update .gitignore with testing-related entries and Claude settings
- Configure 80% coverage threshold with HTML and XML report generation
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