plugin.googledrive icon indicating copy to clipboard operation
plugin.googledrive copied to clipboard

feat: Add Python testing infrastructure with Poetry and pytest

Open llbbl opened this issue 7 months ago • 0 comments

Add Python Testing Infrastructure

Summary

This PR sets up a complete Python testing infrastructure for the Google Drive Kodi plugin using Poetry as the package manager and pytest as the testing framework.

Changes Made

Package Management

  • Poetry Setup: Created pyproject.toml with Poetry configuration as the primary package manager
  • Dependencies: Added testing dependencies as development dependencies:
    • pytest (^8.0.0) - Main testing framework
    • pytest-cov (^5.0.0) - Coverage reporting
    • pytest-mock (^3.14.0) - Mocking utilities

Testing Configuration

  • pytest Configuration: Set up comprehensive pytest settings in pyproject.toml:

    • Test discovery patterns for test_*.py and *_test.py files
    • Coverage reporting with HTML and XML output
    • Custom markers for test categorization (unit, integration, slow)
    • Strict mode with verbose output
  • Coverage Configuration:

    • Source directory set to resources/
    • Exclusions for test files, virtual environments, and cache directories
    • Coverage threshold currently set to 0% (to be increased as tests are added)

Directory Structure

tests/
├── __init__.py
├── conftest.py          # Shared fixtures and configuration
├── test_infrastructure.py  # Validation tests
├── unit/
│   └── __init__.py
└── integration/
    └── __init__.py

Test Fixtures

Created comprehensive fixtures in conftest.py for Kodi plugin development:

  • temp_dir - Temporary directory management
  • mock_addon - Mock Kodi addon object
  • mock_xbmc, mock_xbmcgui, mock_xbmcplugin - Mock Kodi modules
  • mock_kodi_modules - Combined fixture that injects all mocks into sys.modules
  • sample_drive_item, sample_folder_item - Sample Google Drive data
  • mock_settings - Common plugin settings
  • mock_http_response - Mock HTTP responses for API testing
  • capture_logs - Log capture for testing

Additional Setup

  • Updated .gitignore: Added entries for:

    • Testing artifacts (.pytest_cache/, .coverage, htmlcov/, etc.)
    • Poetry files (keeping poetry.lock tracked)
    • Virtual environments and IDE files
    • Claude-specific files (.claude/*)
  • Validation Tests: Created test_infrastructure.py to verify:

    • All testing dependencies are installed correctly
    • Project structure exists as expected
    • Fixtures are properly loaded
    • Test markers work correctly
    • Kodi module mocking functions properly

How to Use

Running Tests

You can run tests using either of these commands:

poetry run test
poetry run tests

Both commands will:

  • Execute all tests in the tests/ directory
  • Generate coverage reports in terminal, HTML, and XML formats
  • Apply all pytest configurations from pyproject.toml

Running Specific Test Categories

# Run only unit tests
poetry run pytest -m unit

# Run only integration tests  
poetry run pytest -m integration

# Skip slow tests
poetry run pytest -m "not slow"

Installing Dependencies

# Install all dependencies including dev dependencies
poetry install

# Install only production dependencies
poetry install --no-dev

Notes

  • The coverage threshold is currently set to 0% to allow the infrastructure to be set up without existing tests. This should be increased to 80% (or your preferred threshold) as tests are added.
  • The validation tests all pass, confirming the infrastructure is working correctly.
  • The Poetry scripts show a warning about entry points, but this doesn't affect functionality.
  • All Kodi-specific modules are mocked to allow testing without a Kodi environment.

Next Steps

With this infrastructure in place, developers can now:

  1. Write unit tests in the tests/unit/ directory
  2. Write integration tests in the tests/integration/ directory
  3. Use the provided fixtures to mock Kodi functionality
  4. Gradually increase the coverage threshold as more tests are added

llbbl avatar Jul 01 '25 04:07 llbbl