Skip to content

feat: Add comprehensive Python testing infrastructure with Poetry - #2

Open
llbbl wants to merge 1 commit into
xingyunyang01:mainfrom
UnitSeeker:add-testing-infrastructure
Open

llbbl wants to merge 1 commit into
xingyunyang01:mainfrom
UnitSeeker:add-testing-infrastructure

Conversation

@llbbl

@llbbl llbbl commented Aug 23, 2025

Copy link
Copy Markdown

Add Python Testing Infrastructure

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 with all necessary tools and configurations for writing and running tests.

Changes Made

Package Management

  • Poetry Configuration: Created pyproject.toml with complete Poetry setup
  • Dependency Migration: Migrated existing dependencies from requirements.txt to Poetry
  • Lock File: Generated poetry.lock for reproducible builds

Testing Dependencies

Added the following testing packages as development dependencies:

  • pytest (^8.3.4) - Core testing framework
  • pytest-cov (^6.0.0) - Coverage reporting
  • pytest-mock (^3.14.0) - Mocking utilities
  • pytest-asyncio (^0.24.0) - Async test support
  • pytest-xdist (^3.6.1) - Parallel test execution

Additional Development Tools

  • black (^24.10.0) - Code formatting
  • ruff (^0.8.6) - Fast Python linter
  • mypy (^1.14.1) - Static type checking
  • pre-commit (^4.0.1) - Git hook management

Testing Configuration

pytest Configuration

  • Test discovery patterns for test_*.py and *_test.py files
  • Custom markers: unit, integration, slow, asyncio
  • Strict mode enabled for better error detection
  • Automatic asyncio mode for async tests

Coverage Configuration

  • Source directories: FunctionCalling, a2aTest, agent, selenium
  • Coverage reports: Terminal, HTML (htmlcov/), and XML (coverage.xml)
  • Exclusion patterns for non-testable code
  • Branch coverage enabled

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)

Comprehensive collection of reusable test fixtures:

  • File System: temp_dir, temp_file, mock_file_system
  • Configuration: mock_config, mock_env_vars
  • HTTP Clients: mock_http_client, mock_async_http_client
  • Database: mock_database_connection
  • LLM/AI: mock_llm_client, mock_async_llm_client
  • Caching: mock_cache
  • WebSocket: mock_websocket, mock_async_websocket
  • Utilities: mock_logger, benchmark_timer, capture_logs
  • Data: sample_json_data, sample_pydantic_model

Validation Tests

Created comprehensive validation tests (test_infrastructure.py) that verify:

  • All fixtures work correctly
  • Test markers function properly
  • Async tests execute successfully
  • Parametrized tests work
  • Coverage exclusions are respected

Git Configuration

Updated .gitignore with:

  • Python artifacts (__pycache__, *.pyc, .pytest_cache/)
  • Coverage files (.coverage, htmlcov/, coverage.xml)
  • Virtual environments (.venv/, venv/)
  • IDE files (.idea/, .vscode/)
  • Claude settings (.claude/*)

How to Use

Installation

# Install Poetry (if not already installed)
pipx install poetry

# Install project dependencies
poetry install

Running Tests

# Run all tests with coverage
poetry run test
# or
poetry run tests

# Run specific test markers
poetry run pytest -m unit        # Unit tests only
poetry run pytest -m integration # Integration tests only
poetry run pytest -m slow       # Slow tests only

# Run tests in parallel
poetry run pytest -n auto

# Run tests with verbose output
poetry run pytest -v

# Run specific test file
poetry run pytest tests/test_infrastructure.py

Coverage Reports

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

Development Tools

# Format code with Black
poetry run black .

# Lint code with Ruff
poetry run ruff check .

# Type check with mypy
poetry run mypy .

Notes

  • Coverage threshold is currently set to 0% to allow incremental test addition
  • The infrastructure is validation-tested with 28 passing tests
  • All standard pytest options and plugins are available
  • Both sync and async testing patterns are supported

Next Steps

  1. Developers can immediately start writing unit tests in tests/unit/
  2. Integration tests can be added to tests/integration/
  3. Coverage threshold can be gradually increased as tests are added
  4. Pre-commit hooks can be configured using the installed pre-commit package

- Set up Poetry as package manager with complete pyproject.toml configuration
- Add pytest with coverage, mock, async, and parallel testing support
- Configure test markers (unit, integration, slow) and coverage thresholds
- Create tests/ directory structure with shared fixtures in conftest.py
- Add validation tests to verify infrastructure functionality
- Update .gitignore with testing and development artifacts
- Enable both `poetry run test` and `poetry run tests` commands
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