shiftpi icon indicating copy to clipboard operation
shiftpi copied to clipboard

feat: Set up comprehensive Python testing infrastructure with Poetry

Open llbbl opened this issue 5 months ago • 0 comments

Set up comprehensive Python testing infrastructure with Poetry

Summary

This PR establishes a complete testing infrastructure for the shiftpi project, migrating from Python 2.7 to Python 3.11 and setting up Poetry as the package manager with comprehensive testing capabilities.

Changes Made

Package Management

  • Added Poetry configuration in pyproject.toml
  • Migrated from setup.py to modern Poetry-based dependency management
  • Updated Python compatibility from 2.7 to 3.11
  • Fixed Python 3 compatibility issues (replaced iteritems() with items())

Testing Infrastructure

  • Added testing dependencies:
    • pytest ^7.4.0 - Main testing framework
    • pytest-cov ^4.1.0 - Coverage reporting
    • pytest-mock ^3.11.0 - Mocking utilities

Testing Configuration

  • Comprehensive pytest configuration with:
    • Test discovery patterns
    • Coverage settings (80% threshold)
    • HTML and XML coverage reports
    • Custom markers: unit, integration, slow
    • Strict configuration and verbose output

Directory Structure

  • Created proper testing directories:
    tests/
    ├── __init__.py
    ├── conftest.py          # Shared fixtures
    ├── unit/
    │   └── __init__.py
    ├── integration/
    │   └── __init__.py
    └── test_setup_validation.py  # Infrastructure validation tests
    

Fixtures and Mocking

  • Comprehensive shared fixtures in conftest.py:
    • temp_dir - Temporary directory for tests
    • mock_gpio - RPi.GPIO mocking for testing without hardware
    • mock_config - Sample configuration data
    • sample_pin_states - Test pin state data
    • reset_shiftpi_globals - Clean global state between tests
    • temp_file - Temporary file fixture
    • mock_sleep - Mock time.sleep for faster tests

Development Workflow

  • Added Poetry script commands:
    • poetry run test - Run all tests
    • poetry run tests - Alternative command (both work)
  • Updated .gitignore with comprehensive exclusions for:
    • Testing artifacts (.pytest_cache/, .coverage, htmlcov/)
    • Build artifacts (build/, dist/, *.egg-info/)
    • Virtual environments and IDE files
    • Claude settings (.claude/*)

Testing Instructions

Installation

poetry install

Running Tests

# Run all tests with coverage
poetry run pytest

# Or use the script commands
poetry run test
poetry run tests

# Run specific test categories
poetry run pytest -m unit
poetry run pytest -m integration
poetry run pytest -m slow

# Run with verbose output
poetry run pytest -v

Coverage Reports

Coverage reports are generated in multiple formats:

  • Terminal: Summary displayed after test run
  • HTML: htmlcov/index.html for detailed browser viewing
  • XML: coverage.xml for CI/CD integration

Validation

The setup has been validated with comprehensive tests that verify:

  • ✅ pytest is working correctly
  • ✅ Project structure is properly configured
  • ✅ Main module can be imported (with GPIO mocking)
  • ✅ All shared fixtures are available and functional
  • ✅ Custom test markers work
  • ✅ Coverage measurement is active

Notes

  • Python Version: Migrated from Python 2.7 to Python 3.11
  • Hardware Dependencies: RPi.GPIO is mocked in tests for development without Raspberry Pi hardware
  • Coverage Threshold: Set to 80% (currently at 42% with validation tests only)
  • Lock Files: poetry.lock is tracked in version control as intended

The testing infrastructure is now ready for developers to start writing comprehensive unit and integration tests for the shiftpi library.

🤖 Generated with Claude Code

llbbl avatar Sep 03 '25 13:09 llbbl