Skip to content

Conversation

@llbbl
Copy link

@llbbl llbbl commented Jun 16, 2025

Add Python Testing Infrastructure

Summary

This PR sets up a comprehensive testing infrastructure for the Sniffle Python CLI using Poetry as the package manager and pytest as the testing framework.

Changes Made

Package Management

  • Added Poetry configuration in python_cli/pyproject.toml
  • Migrated dependencies from README documentation to formal dependency management
  • Organized dependencies into groups:
    • Core: pyserial (required)
    • SDR (optional): numpy, scipy
    • Dev: pytest, pytest-cov, pytest-mock

Testing Configuration

  • Pytest configuration with:

    • Coverage reporting (HTML, XML, terminal)
    • Custom markers: unit, integration, slow
    • Strict mode enabled for better error detection
    • Test discovery patterns configured
  • Coverage settings:

    • Source coverage for sniffle package
    • Exclusions for test files and common patterns
    • 80% coverage threshold (currently commented out until tests are written)

Directory Structure

python_cli/
├── pyproject.toml         # Poetry and testing configuration
├── poetry.lock           # Locked dependencies
├── tests/
│   ├── __init__.py
│   ├── conftest.py       # Shared pytest fixtures
│   ├── test_setup_validation.py  # Infrastructure validation
│   ├── unit/
│   │   └── __init__.py
│   └── integration/
│       └── __init__.py

Testing Fixtures

Created comprehensive fixtures in conftest.py:

  • temp_dir - Temporary directory management
  • mock_serial_port - Serial port mocking
  • mock_pcap_writer - PCAP writer mocking
  • sample_ble_packet - BLE packet test data
  • sample_adv_packet - BLE advertising packet data
  • mock_decoder - Packet decoder mocking
  • mock_ble_config - BLE configuration
  • test_firmware_file - Firmware file testing
  • mock_sniffle_hw - Hardware interface mocking
  • capture_output - stdout/stderr capture
  • mock_time - Time function mocking

Development Commands

Added Poetry scripts for convenience:

  • poetry run test - Run all tests
  • poetry run tests - Alternative command (both work)

Additional Setup

  • Updated .gitignore with Python/testing artifacts and Claude settings
  • Created validation tests to ensure infrastructure works correctly

Usage Instructions

Initial Setup

cd python_cli
poetry install              # Install all dependencies
poetry install --with sdr   # Include SDR dependencies

Running Tests

# Run all tests
poetry run test

# Run with specific options
poetry run pytest -v                    # Verbose output
poetry run pytest -k "test_name"        # Run specific test
poetry run pytest -m unit               # Run only unit tests
poetry run pytest -m "not slow"         # Skip slow tests

# Coverage report
poetry run pytest --cov-report=html     # Generate HTML coverage

Writing Tests

  1. Place unit tests in tests/unit/
  2. Place integration tests in tests/integration/
  3. Use fixtures from conftest.py for common test needs
  4. Mark tests appropriately: @pytest.mark.unit, @pytest.mark.integration, @pytest.mark.slow

Notes

  • The coverage threshold is currently commented out in pyproject.toml since no actual tests exist yet
  • SoapySDR is commented out in dependencies as it requires system-level installation
  • All tests are passing and the infrastructure is ready for test development

Validation

The infrastructure has been validated with a comprehensive test suite that verifies:

  • Python version compatibility (3.9+)
  • All testing dependencies are installed
  • Project structure is correct
  • Configuration files are present
  • Fixtures work as expected
  • Poetry commands function properly
    EOF < /dev/null

- Add Poetry configuration with pyproject.toml
- Configure pytest with coverage reporting and custom markers
- Create test directory structure (unit/integration)
- Add comprehensive pytest fixtures in conftest.py
- Set up Poetry scripts for running tests
- Update .gitignore with testing and Python artifacts
- Add validation tests to verify infrastructure
EOF < /dev/null
@sultanqasim
Copy link
Collaborator

Thanks, this will be helpful for future development. I will review in more detail and test when I have some time.

@llbbl
Copy link
Author

llbbl commented Jun 17, 2025

Thanks for the positive feedback! I'm happy this looks helpful for your project.

Sounds good regarding the review. I'll be here if you need anything.

@phgarman
Copy link

phgarman commented Jun 17, 2025

Nice work on this Logan! Looks super useful @llbbl

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.

3 participants