Skip to content

feat: Set up complete Python testing infrastructure with Poetry#11

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

feat: Set up complete Python testing infrastructure with Poetry#11
llbbl wants to merge 1 commit intozacharski:masterfrom
UnitSeeker:add-testing-infrastructure

Conversation

@llbbl
Copy link

@llbbl llbbl commented Jun 28, 2025

Add Python Testing Infrastructure

Summary

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

Changes Made

Package Management

  • Poetry Setup: Created pyproject.toml with Poetry configuration
  • Dependencies: Added testing dependencies as development dependencies:
    • pytest (^7.4.0) - Main testing framework
    • pytest-cov (^4.1.0) - Coverage reporting
    • pytest-mock (^3.11.0) - Mocking utilities

Testing Configuration

  • pytest Configuration:
    • Test discovery patterns for test_*.py and *_test.py files
    • Coverage reporting with HTML, XML, and terminal output
    • Custom markers: unit, integration, and slow
    • Strict mode enabled with verbose output
    • Coverage threshold set to 0% initially (with note to increase to 80%)

Directory Structure

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

Shared Fixtures (conftest.py)

  • temp_dir: Creates temporary directories for test files
  • sample_data_file: Creates sample data files for testing
  • mock_config: Provides mock configuration dictionaries
  • sample_dataset: Provides sample ML datasets
  • sample_matrix: Provides sample 2D matrices
  • reset_environment: Resets environment variables between tests
  • capture_stdout: Captures stdout for testing print statements

Development Commands

  • poetry run test - Run all tests with coverage
  • poetry run tests - Alternative command (both work identically)
  • All standard pytest options are available (e.g., -v, -k, --no-cov)

Additional Setup

  • .gitignore: Comprehensive Python gitignore including:
    • Testing artifacts (.pytest_cache/, coverage.xml, htmlcov/)
    • Claude settings (.claude/*)
    • Python build artifacts and virtual environments
    • Note: poetry.lock is NOT ignored and should be committed

Usage Instructions

  1. Install dependencies:

    poetry install
  2. Run all tests:

    poetry run test
    # or
    poetry run tests
  3. Run specific tests:

    poetry run test tests/unit/
    poetry run test -k "test_name"
    poetry run test -m unit
  4. Run tests without coverage:

    poetry run test --no-cov
  5. View coverage report:

    • Terminal: Shown automatically after test run
    • HTML: Open htmlcov/index.html in browser
    • XML: Available at coverage.xml

Notes

  • The coverage threshold is initially set to 0% to allow setup without existing tests
  • Update --cov-fail-under in pyproject.toml to 80% once you have sufficient test coverage
  • The validation test file (test_setup_validation.py) verifies that all testing infrastructure components are working correctly
  • All chapter modules (ch2-ch8) are configured for coverage reporting
  • The project is ready for immediate test development

Next Steps

  1. Start writing unit tests in tests/unit/ directory
  2. Add integration tests in tests/integration/ directory
  3. Increase coverage threshold to 80% once tests are written
  4. Consider adding additional testing tools as needed (e.g., pytest-asyncio for async code)

- Initialize Poetry package manager with pyproject.toml
- Add pytest, pytest-cov, and pytest-mock as dev dependencies
- Configure pytest with coverage reporting and custom markers
- Create tests directory structure with shared fixtures
- Add comprehensive .gitignore with Python and Claude entries
- Set up poetry scripts for 'test' and 'tests' commands
- Include 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

Comments