cookbook-2nd icon indicating copy to clipboard operation
cookbook-2nd copied to clipboard

feat: Set up comprehensive Python testing infrastructure with Poetry

Open llbbl opened this issue 6 months ago • 0 comments

Set Up Python Testing Infrastructure

Summary

This PR establishes a comprehensive testing infrastructure for the IPython Cookbook project using Poetry as the package manager and pytest as the testing framework.

Changes Made

Package Management

  • Poetry Configuration: Created pyproject.toml with Poetry configuration
    • Set up project metadata
    • Configured package-mode as false (dependency management only)
    • Added test/dev dependencies: pytest, pytest-cov, pytest-mock

Testing Configuration

  • pytest Configuration in pyproject.toml:

    • Test discovery patterns for test_*.py and *_test.py files
    • Coverage settings with 80% threshold
    • HTML and XML coverage report generation
    • Custom markers: unit, integration, slow
    • Strict marker enforcement
  • Coverage Configuration:

    • Source includes all project files
    • Excludes test files, virtual environments, and build artifacts
    • Precision set to 2 decimal places
    • Shows missing lines in coverage reports

Directory Structure

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

Shared Fixtures (conftest.py)

  • temp_dir: Creates temporary directories for testing
  • temp_file: Creates temporary files with content
  • mock_config: Provides mock configuration dictionaries
  • sample_data: Sample data structures for testing
  • mock_notebook_data: Mock Jupyter notebook structures
  • reset_environment: Auto-resets environment variables
  • capture_logs: Log capture configuration
  • mock_http_response: Factory for creating mock HTTP responses

Development Commands

Both commands are available for running tests:

  • poetry run pytest - Direct pytest execution
  • Standard pytest options are available (e.g., -v, -k, --runslow)

.gitignore Updates

Added comprehensive entries for:

  • Testing artifacts (.pytest_cache/, .coverage, htmlcov/)
  • Python artifacts (__pycache__/, *.pyc, *.pyo)
  • Virtual environments
  • Build artifacts
  • IDE files
  • Claude settings (.claude/*)

How to Use

  1. Install dependencies:

    poetry install
    
  2. Run all tests:

    poetry run pytest
    
  3. Run tests with coverage:

    poetry run pytest --cov
    
  4. 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 --runslow      # Include slow tests
    
  5. View coverage report:

    • HTML report: Open htmlcov/index.html in a browser
    • Terminal report: Included by default when running tests

Notes

  • The infrastructure is ready for immediate use - developers can start writing tests in the tests/unit/ or tests/integration/ directories
  • Coverage threshold is set to 80% but will only apply once actual code is added to the project
  • All validation tests pass, confirming the infrastructure is properly configured
  • Poetry lock file is tracked in git for reproducible builds

llbbl avatar Jun 24 '25 05:06 llbbl