Skip to content

Conversation

llbbl
Copy link

@llbbl llbbl commented Jun 24, 2025

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
- Configure Poetry as package manager with pyproject.toml - Add pytest, pytest-cov, and pytest-mock as dev dependencies - Set up pytest configuration with coverage thresholds and custom markers - Create tests directory structure with unit/integration subdirectories - Add comprehensive conftest.py with reusable fixtures - Update .gitignore with testing and Python-specific entries - Add 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

1 participant