Skip to content

Conversation

@llbbl
Copy link

@llbbl llbbl commented Jun 17, 2025

Set Up Python Testing Infrastructure

Summary

This PR establishes a comprehensive testing infrastructure for the Jetson.GPIO project using modern Python tooling. The setup migrates from traditional setup.py to Poetry for dependency management and configures pytest as the testing framework with coverage reporting.

Changes Made

Package Management

  • Migrated to Poetry: Created pyproject.toml with all package metadata from setup.py
  • Updated Python requirement: Set to Python >=3.7 to support modern testing tools
  • Preserved package structure: Maintained existing lib/python directory structure

Testing Dependencies

Added as development dependencies:

  • pytest ^7.4.0 - Core testing framework
  • pytest-cov ^4.1.0 - Coverage reporting integration
  • pytest-mock ^3.11.0 - Mocking utilities for tests

Testing Configuration

Configured in pyproject.toml:

  • Test discovery: Configured to find tests in the tests/ directory
  • Coverage settings:
    • Currently set to 0% threshold (with note to change to 80% when tests are added)
    • HTML and XML report generation
    • Excludes test files and __init__.py from coverage
  • Test markers: Added unit, integration, and slow markers
  • Poetry scripts: Both poetry run test and poetry run tests work

Directory Structure

tests/
├── __init__.py
├── conftest.py          # Shared pytest fixtures
├── unit/
│   ├── __init__.py
│   └── test_setup_validation.py  # Validation tests
└── integration/
    └── __init__.py

Test Fixtures (in conftest.py)

  • temp_dir: Creates temporary directories for test files
  • mock_gpio_hardware: Mocks GPIO hardware interface
  • mock_gpio_permissions: Mocks GPIO permissions and sysfs paths
  • gpio_cleanup: Ensures GPIO cleanup after tests
  • mock_config: Provides mock configuration
  • sample_pin_data: Sample GPIO pin data for testing
  • reset_modules: Resets imported modules between tests

Additional Updates

  • Updated .gitignore: Added comprehensive entries for:
    • Testing artifacts (.pytest_cache/, .coverage, htmlcov/, coverage.xml)
    • Claude settings (.claude/*)
    • Python artifacts (pycache/, *.pyc)
    • IDE files (.vscode/, .idea/)
    • Virtual environments

How to Use

Install Dependencies

poetry install

Run Tests

# Both commands work
poetry run test
poetry run tests

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

View Coverage Reports

After running tests:

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

Notes

  • The coverage threshold is currently set to 0% to allow the infrastructure to be set up without actual tests. This should be changed to 80% (as noted in the config) once tests are added.
  • Python version requirement was updated from >=2.7 to >=3.7 because modern testing tools don't support Python 2.7.
  • The poetry.lock file will be generated on first install and should be committed to ensure reproducible builds.

Next Steps

Developers can now immediately start writing tests in the established structure. The validation tests confirm that all fixtures and configuration work correctly.

- Migrate from setup.py to Poetry-based pyproject.toml
- Add pytest, pytest-cov, and pytest-mock as dev dependencies
- Configure comprehensive pytest and coverage settings
- Create tests directory structure with unit/integration subdirs
- Add shared fixtures in conftest.py for GPIO mocking
- Update .gitignore with testing and Claude-related entries
- Add validation tests to verify infrastructure works
- Configure test markers (unit, integration, slow)
- Set up Poetry scripts for running tests (test/tests commands)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant