Skip to content

Conversation

@llbbl
Copy link

@llbbl llbbl commented Jun 14, 2025

Add Python Testing Infrastructure

Summary

This PR sets up a comprehensive testing infrastructure for the Python project using Poetry as the package manager and pytest as the testing framework. The setup provides a ready-to-use testing environment where developers can immediately start writing tests.

Changes Made

Package Management

  • Initialized Poetry as the package manager by creating pyproject.toml
  • Migrated all dependencies from requirements.txt to Poetry format
  • Added testing dependencies as development dependencies:
    • pytest (^7.4.0) - Main testing framework
    • pytest-cov (^4.1.0) - Coverage reporting
    • pytest-mock (^3.11.1) - Mocking utilities

Testing Configuration

  • Configured pytest in pyproject.toml with:
    • Test discovery patterns for test_*.py and *_test.py files
    • Coverage reporting with HTML and XML outputs
    • Strict markers and configuration
    • Custom markers: unit, integration, slow
    • Coverage source set to src directory

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 (conftest.py)

  • temp_dir - Creates temporary directories for tests
  • temp_file - Creates temporary files
  • mock_config - Provides mock configuration dictionary
  • mock_yaml_config - Creates temporary YAML config files
  • mock_env_vars - Sets up mock environment variables
  • clean_env - Removes specific environment variables
  • mock_request_data - Provides mock HTTP request data
  • sample_data_list - Sample test data
  • capture_logs - Helper for log assertions
  • mock_datetime - Mock datetime for consistent testing
  • Custom pytest options to handle slow tests with --run-slow flag

Build Configuration

  • Updated .gitignore with:
    • Testing artifacts (.pytest_cache/, coverage.xml, htmlcov/)
    • Claude settings (.claude/)
    • Note to commit poetry.lock file
    • IDE and OS-specific files

How to Use

Install Dependencies

poetry install

Run Tests

Both commands work and run pytest:

poetry run test
poetry run tests

Run Specific Tests

# Run only unit tests
poetry run pytest -m unit

# Run integration tests
poetry run pytest -m integration

# Run slow tests (normally skipped)
poetry run pytest --run-slow

# Run with verbose output
poetry run pytest -v

Coverage Reports

  • Coverage is automatically calculated when running tests
  • HTML report: htmlcov/index.html
  • XML report: coverage.xml
  • Terminal report shows uncovered lines

Writing New Tests

  1. Create test files in tests/unit/ or tests/integration/
  2. Name files as test_*.py or *_test.py
  3. Use fixtures from conftest.py for common needs
  4. Mark tests appropriately:
    @pytest.mark.unit
    def test_example():
        assert True

Notes

  • Coverage threshold is currently set to 0% since this PR only sets up infrastructure
  • The validation test file (test_setup_validation.py) verifies the setup works correctly
  • All Poetry commands should be run from the project root
  • The poetry.lock file should be committed to ensure reproducible builds

- Initialize Poetry as package manager with pyproject.toml
- Add pytest, pytest-cov, and pytest-mock as dev dependencies
- Configure pytest with coverage reporting (HTML/XML outputs)
- Create testing directory structure (tests/unit, tests/integration)
- Set up shared fixtures in conftest.py (temp dirs, mocks, etc.)
- Add custom markers for test organization (unit, integration, slow)
- Configure Poetry scripts for running tests
- Update .gitignore with testing and Claude-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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant