Verbi icon indicating copy to clipboard operation
Verbi copied to clipboard

feat: Set up comprehensive Python testing infrastructure with Poetry

Open llbbl opened this issue 6 months ago • 0 comments

Add Comprehensive Python Testing Infrastructure

Summary

This PR sets up a complete testing infrastructure for the JARVIS Voice Assistant project using Poetry as the package manager and pytest as the testing framework. The infrastructure provides everything needed for developers to immediately start writing and running tests.

Changes Made

Package Management

  • Poetry Configuration: Created pyproject.toml with full Poetry setup
  • Dependency Migration: Migrated all dependencies from requirements.txt and setup.py to Poetry
  • Development Dependencies: Added pytest (8.0+), pytest-cov (5.0+), and pytest-mock (3.14+)

Testing Configuration

  • pytest Configuration:

    • Test discovery patterns for test_*.py and *_test.py files
    • Strict markers and configuration enforcement
    • Custom markers: unit, integration, slow
    • Verbose output with detailed failure information
  • Coverage Configuration:

    • 80% coverage threshold (when coverage is enabled)
    • Multiple report formats: terminal, HTML (htmlcov/), XML (coverage.xml)
    • Intelligent exclusions for non-testable code patterns
    • Skip covered lines in terminal output for clarity

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)

  • temp_dir: Creates temporary directories for test isolation
  • temp_file: Creates temporary files with content
  • mock_config: Provides mock configuration dictionaries
  • mock_env_vars: Sets up mock environment variables
  • mock_api_response: Mock API response structures
  • mock_audio_data: Mock audio data for testing
  • mock_llm_client, mock_tts_client, mock_stt_client: Mock service clients
  • capture_logs: Captures log messages during tests
  • mock_file_system: Creates mock file system structures
  • performance_timer: Measures test execution time

Running Tests

Basic Commands

# Run all tests
poetry run test

# Alternative command (both work)
poetry run tests

# Run tests with coverage
poetry run pytest --cov

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

# Run tests by marker
poetry run pytest -m unit
poetry run pytest -m integration
poetry run pytest -m "not slow"

Using the Test Script

# Run tests without coverage
./scripts/test.sh

# Run tests with coverage enforcement
./scripts/test.sh coverage

# Pass additional pytest arguments
./scripts/test.sh -v -k test_name

Validation

The infrastructure has been validated with 17 passing tests that verify:

  • All testing dependencies are properly installed
  • Fixtures work correctly and provide expected functionality
  • Directory structure is properly created
  • Configuration files are correctly set up
  • Test markers can be applied and used

Notes

  • PyAudio Dependency: Temporarily removed from dependencies due to missing system libraries (portaudio). This can be re-added once system dependencies are installed.
  • Coverage Source: Currently configured to only measure test coverage. Update [tool.coverage.run] source to include your application code when ready.
  • poetry.lock: This file is not gitignored and should be committed to ensure reproducible builds.

Next Steps

  1. Install system dependencies for PyAudio if audio functionality is needed:

    # Ubuntu/Debian
    sudo apt-get install portaudio19-dev
    
    # macOS
    brew install portaudio
    
  2. Update coverage configuration to include source code:

    [tool.coverage.run]
    source = ["your_package_name"]
    
  3. Start writing tests in the tests/unit/ and tests/integration/ directories

  4. Consider adding pre-commit hooks to run tests automatically

Testing the Setup

To verify everything is working correctly:

# Install dependencies
poetry install

# Run validation tests
poetry run pytest tests/test_infrastructure.py -v

# Check that both test commands work
poetry run test --version
poetry run tests --version

llbbl avatar Jun 14 '25 13:06 llbbl