crewai-experiments icon indicating copy to clipboard operation
crewai-experiments copied to clipboard

feat: Set up complete Python testing infrastructure with Poetry

Open llbbl opened this issue 6 months ago • 0 comments

Set Up Python Testing Infrastructure

Summary

This PR establishes a complete testing infrastructure for the Python newsletter automation project using Poetry as the package manager and pytest as the testing framework.

Changes Made

Package Management

  • Poetry Configuration: Created pyproject.toml with Poetry configuration
  • Dependencies: Migrated existing dependencies (crewai, langchain, praw, etc.) to Poetry
  • Python Version: Set to Python 3.9+ for compatibility with all dependencies
  • Lock File: Generated poetry.lock for reproducible builds

Testing Setup

  • Testing Framework: Added pytest with extensions:
    • pytest-cov for code coverage reporting
    • pytest-mock for mocking utilities
  • Configuration: Comprehensive pytest configuration including:
    • Test discovery patterns
    • Coverage settings with 80% threshold
    • HTML and XML coverage reports
    • Custom test markers (unit, integration, slow)
    • Strict mode for better error reporting

Directory Structure

tests/
├── __init__.py
├── conftest.py          # Shared fixtures and configuration
├── unit/                # Unit tests directory
│   └── __init__.py
├── integration/         # Integration tests directory
│   └── __init__.py
└── test_infrastructure_validation.py  # Validation tests

Shared Fixtures (conftest.py)

  • temp_dir: Temporary directory for test files
  • mock_env_vars: Mock environment variables
  • mock_config: Mock configuration dictionary
  • mock_langchain_llm: Mock LangChain LLM
  • mock_crewai_agent: Mock CrewAI Agent
  • mock_crewai_task: Mock CrewAI Task
  • mock_reddit_client: Mock Reddit (PRAW) client
  • sample_newsletter_data: Sample data for testing
  • capture_logs: Log capture fixture

Commands

  • poetry run test - Run all tests with coverage
  • poetry run tests - Alternative command (both work)
  • poetry run pytest - Direct pytest execution

Additional Configuration

  • Updated .gitignore with testing artifacts and Poetry files
  • Added validation tests to ensure infrastructure works correctly
  • Configured coverage to exclude test files from coverage calculation

Testing Instructions

  1. Install Poetry if not already installed:

    curl -sSL https://install.python-poetry.org | python3 -
    
  2. Install dependencies:

    poetry install
    
  3. Run tests:

    poetry run test
    # or
    poetry run tests
    
  4. View coverage report:

    • Terminal: Coverage is shown after test run
    • HTML: Open htmlcov/index.html in a browser
    • XML: Available at coverage.xml for CI/CD integration

Notes

  • All validation tests pass successfully
  • The infrastructure is ready for developers to write actual unit and integration tests
  • Coverage threshold is set to 80% but currently excludes source files (only infrastructure is tested)
  • Both test and tests commands are available as requested
  • The setup follows Python testing best practices and modern tooling standards

llbbl avatar Jun 16 '25 12:06 llbbl