Youtube-Code-Repository icon indicating copy to clipboard operation
Youtube-Code-Repository copied to clipboard

feat: Set up comprehensive Python testing infrastructure with Poetry and pytest

Open llbbl opened this issue 5 months ago • 0 comments

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 solid foundation for writing and running tests across the codebase.

Changes Made

Package Management

  • Poetry configured as the primary package manager
  • Created pyproject.toml with project metadata and dependencies
  • Generated poetry.lock file for reproducible builds

Testing Framework

  • pytest installed as the main testing framework
  • pytest-cov added for code coverage reporting
  • pytest-mock included for mocking utilities

Configuration

  • Pytest configuration in pyproject.toml includes:
    • Test discovery patterns for test_*.py and *_test.py files
    • Custom markers: @pytest.mark.unit, @pytest.mark.integration, @pytest.mark.slow
    • Strict mode enabled for better test quality
    • Verbose output and short traceback format

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 testing
  • temp_file: Creates temporary files with content
  • mock_config: Provides mock configuration objects
  • mock_logger: Mock logger for testing logging
  • sample_data: Common test data structures
  • env_vars: Temporarily set environment variables
  • mock_file_system: Mock file system operations
  • capture_stdout: Capture print output
  • mock_requests: Mock HTTP requests
  • And more...

Development Experience

  • Added .gitignore with comprehensive Python patterns
  • Poetry commands configured:
    • poetry run test - Run all tests
    • poetry run tests - Alternative command (both work)

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
poetry run test

# Run with coverage
poetry run pytest --cov=. --cov-report=html --cov-report=term-missing

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

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

Writing Tests

  1. Create test files in tests/unit/ or tests/integration/
  2. Use fixtures from conftest.py for common functionality
  3. Mark tests appropriately with @pytest.mark.unit, etc.
  4. Follow naming convention: test_*.py for test files

Coverage Configuration

Coverage settings are pre-configured but not enforced by default. To run with coverage:

poetry run pytest --cov=. --cov-report=html --cov-fail-under=80

This will:

  • Generate HTML coverage report in htmlcov/
  • Generate XML coverage report as coverage.xml
  • Fail if coverage is below 80%

Next Steps

With this infrastructure in place, developers can now:

  1. Write unit tests for individual functions and classes
  2. Create integration tests for module interactions
  3. Add performance tests marked with @pytest.mark.slow
  4. Use the provided fixtures for common testing scenarios
  5. Monitor code coverage to ensure quality

The validation tests confirm that all components are working correctly and can be run anytime to verify the setup.

llbbl avatar Jun 27 '25 23:06 llbbl