Skip to content

Conversation

llbbl
Copy link

@llbbl llbbl commented Aug 27, 2025

Python Testing Infrastructure Setup

Summary

This PR establishes 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

  • ✅ Detected and utilized existing Poetry configuration
  • ✅ Added testing dependencies as development dependencies (not production)

Testing Dependencies Added

  • pytest (^8.3.3) - Main testing framework
  • pytest-cov (^5.0.0) - Coverage reporting with HTML/XML output
  • pytest-mock (^3.14.0) - Mocking utilities for unit tests

Testing Configuration (pyproject.toml)

  • pytest settings:

    • Test discovery patterns configured for test_*.py and *_test.py
    • Coverage reporting with 80% threshold requirement
    • HTML and XML coverage reports generation
    • Strict markers enabled for better test organization
    • Custom markers defined: unit, integration, slow
  • Coverage settings:

    • Source directory set to src/
    • Exclusions configured for test files, migrations, and boilerplate code
    • Branch coverage enabled
    • Multiple report formats (terminal, HTML, XML)

Directory Structure Created

tests/
├── __init__.py
├── conftest.py           # Shared fixtures
├── test_validation.py    # Infrastructure validation tests
├── unit/
│   └── __init__.py
└── integration/
    └── __init__.py

Shared Fixtures (conftest.py)

  • temp_dir - Temporary directory with automatic cleanup
  • temp_file - Create temporary files for testing
  • mock_config - Mock configuration object
  • mock_logger - Mock logger for testing logging
  • sample_json_data - Sample JSON data fixture
  • json_file - Create temporary JSON files
  • mock_env_vars - Mock environment variables
  • mock_http_response - Mock HTTP responses
  • mock_database_connection - Mock database connections
  • capture_stdout - Capture print output for testing
  • reset_modules - Clean module state between tests

Development Commands

Both commands are now available via Poetry:

  • poetry run test - Run all tests
  • poetry run tests - Alternative command (both work identically)

All standard pytest options are available (e.g., poetry run test -v, poetry run test -k unit)

.gitignore Updates

Added comprehensive Python and testing-related entries:

  • Testing artifacts (.pytest_cache/, .coverage, htmlcov/, coverage.xml)
  • Python artifacts (__pycache__/, *.py[cod], .eggs/)
  • Virtual environments (venv/, .venv/)
  • IDE files (.vscode/, .idea/)
  • Claude settings (.claude/)
  • Build artifacts and temporary files

How to Use

Install Dependencies

poetry install

Run Tests

# Run all tests
poetry run test

# Run with verbose output
poetry run test -v

# Run only unit tests
poetry run test -m unit

# Run tests with coverage report
poetry run test --cov

# Run specific test file
poetry run test tests/test_validation.py

Writing Tests

  1. Place unit tests in tests/unit/
  2. Place integration tests in tests/integration/
  3. Use the provided fixtures from conftest.py
  4. Mark tests appropriately with @pytest.mark.unit, @pytest.mark.integration, or @pytest.mark.slow

Coverage Reports

After running tests, coverage reports are available in:

  • Terminal output (shows missing lines)
  • htmlcov/index.html - Interactive HTML report
  • coverage.xml - XML report for CI/CD integration

Validation

The infrastructure has been validated with 18 tests that verify:

  • All dependencies are properly installed
  • Directory structure is correctly created
  • All fixtures work as expected
  • Test markers are properly configured
  • Poetry scripts function correctly

Notes

  • The project uses Poetry for dependency management, which provides better dependency resolution and lock file management compared to pip
  • Coverage threshold is set to 80% - tests will fail if coverage drops below this
  • The poetry.lock file should be committed to ensure reproducible builds
  • No actual unit tests for the codebase were written - only infrastructure setup and validation tests

- Added pytest, pytest-cov, and pytest-mock as dev dependencies
- Configured pytest and coverage settings in pyproject.toml
- Created test directory structure with unit/integration folders
- Added extensive shared fixtures in conftest.py
- Set up test markers for unit, integration, and slow tests
- Configured Poetry scripts for running tests
- Updated .gitignore with testing and Python-specific entries
- Added 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