Skip to content

Conversation

@llbbl
Copy link

@llbbl llbbl commented Jul 1, 2025

Add Python Testing Infrastructure

Summary

This PR sets up a complete testing infrastructure for the Python course assignments project using Poetry as the package manager and pytest as the testing framework.

Changes Made

Package Management

  • Poetry Setup: Initialized Poetry with package-mode = false since this is a collection of assignments rather than a distributable package
  • Dependencies: Added testing dependencies as development dependencies:
    • pytest ^8.3.0 - Core testing framework
    • pytest-cov ^6.0.0 - Coverage reporting
    • pytest-mock ^3.14.0 - Mocking utilities

Testing Configuration

  • pytest Configuration in pyproject.toml:

    • Strict markers and configuration enforcement
    • Coverage reporting with 80% threshold
    • HTML and XML coverage report generation
    • Custom markers: unit, integration, slow
    • Test discovery patterns configured
  • Coverage Configuration:

    • Source set to tests directory only (excluding course assignments)
    • Comprehensive omit patterns
    • Branch coverage enabled
    • Multiple report formats

Directory Structure

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

Shared Fixtures (in conftest.py)

  • temp_dir - Temporary directory management
  • mock_config - Sample configuration data
  • sample_data - Test data for assignments
  • reset_environment - Environment cleanup (autouse)
  • mock_file_content - Sample file content
  • capture_stdout - Print statement capture
  • project_root - Project root path helper

Additional Updates

  • .gitignore: Updated with comprehensive Python, testing, and Claude-specific entries
  • CLAUDE.md: Initialized for project context
  • Validation Tests: Created to verify the infrastructure works correctly

How to Use

Installation

# Install Poetry (if not already installed)
curl -sSL https://install.python-poetry.org | python3 -

# Install dependencies
poetry install

Running Tests

# Run all tests with coverage
poetry run pytest

# Run specific test markers
poetry run pytest -m unit
poetry run pytest -m integration
poetry run pytest -m "not slow"

# Run with specific options
poetry run pytest -v --no-cov  # Verbose without coverage
poetry run pytest tests/unit/  # Run only unit tests

Writing Tests

  1. Create test files in tests/unit/ or tests/integration/
  2. Use the test_ prefix for test files and functions
  3. Import shared fixtures from conftest.py
  4. Apply appropriate markers (@pytest.mark.unit, etc.)

Coverage Reports

  • Terminal: Shown automatically after test runs
  • HTML: Open htmlcov/index.html in a browser
  • XML: Available at coverage.xml for CI/CD integration

Notes

  • Poetry is configured with package-mode = false since this is a course assignment collection, not a package
  • Coverage is configured to only analyze test files, not the course assignments
  • The 80% coverage threshold applies only to the test infrastructure itself
  • All Python assignment files remain untouched and are excluded from coverage

- Initialize Poetry for dependency management (package-mode: false)
- Add pytest, pytest-cov, and pytest-mock as dev dependencies
- Configure pytest with coverage, markers, and strict settings
- Create testing directory structure with shared fixtures
- Update .gitignore with Python/testing/Claude 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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant