Skip to content

Add comprehensive test suite with mocked hardware#24

Merged
roesel merged 3 commits intomainfrom
claude/improve-code-coverage-8AxCz
Mar 8, 2026
Merged

Add comprehensive test suite with mocked hardware#24
roesel merged 3 commits intomainfrom
claude/improve-code-coverage-8AxCz

Conversation

@roesel
Copy link
Copy Markdown
Owner

@roesel roesel commented Mar 8, 2026

Summary

This PR adds a comprehensive test suite for the elliptec library using mocked serial ports and controllers, eliminating the need for physical hardware during testing. The test suite covers all device classes (Rotator, Linear, Iris, Slider, Shutter) and core components (Controller, scan module).

Key Changes

New Test Files

  • tests/test_devices_mock.py: Comprehensive tests for all device classes using mocked controllers

    • Tests for Rotator (ELL14/ELL18): angle conversions, get/set/shift operations
    • Tests for Linear (ELL20): distance conversions and movement operations
    • Tests for Iris (ELL15): aperture range checking and movement with bounds validation
    • Tests for Slider (ELL6/ELL9): slot positioning and multi-slot configurations
    • Tests for Shutter: open/close operations with normal and inverted modes
    • Tests for Motor and ContinuousMotor base classes: home, jog, offset, and status extraction
  • tests/test_controller.py: Controller class tests with mocked serial ports

    • Connection initialization and error handling
    • Context manager behavior
    • Response parsing (position, status, info dict)
    • Instruction sending with various message formats (string, int, negative int)
    • Connection closing
  • tests/test_scan.py: Scan module tests

    • Port discovery with and without serial numbers
    • Device scanning across address ranges
    • Error handling for serial exceptions
  • tests/conftest.py: Shared test fixtures and utilities

    • make_info_response(): Factory function for creating canned info responses
    • mock_controller fixture: Pre-configured mocked Controller instance

Test Infrastructure

  • Uses unittest.mock for mocking serial ports and controller responses
  • Helper function _make_device() instantiates device classes with mocked controllers
  • Canned protocol responses eliminate need for real hardware communication
  • Tests cover both unit-level conversions and integration with mocked controller

Existing Code Updates

  • tests/test_unit.py: Added tests for motor info parsing (I1, I2 response codes) and move_check error handling

Implementation Details

  • Mock strategy: Controller.send_instruction is mocked to return canned protocol responses
  • Device initialization properly loads motor info on first call, then returns position/status tuples
  • Tests verify both low-level conversions (_pos_to_unit, _unit_to_pos) and high-level operations (get/set/shift)
  • Comprehensive coverage of edge cases: out-of-range values, invalid commands, inverted modes
  • All tests are isolated and can run without any physical hardware or serial ports

https://claude.ai/code/session_01P9qVe3TkpdEfGtupr8TxC4

claude added 2 commits March 8, 2026 12:49
Add tests for Controller, Motor, ContinuousMotor, Rotator, Linear,
Iris, Slider, Shutter, and scan modules using mocked serial ports.
No hardware required — all tests run in CI.

https://claude.ai/code/session_01P9qVe3TkpdEfGtupr8TxC4
The `if instruction == b"ho"` branch was unreachable since the command
dict already maps home_clockwise to b"ho0" and home_anticlockwise to
b"ho1". This was a leftover from an earlier design. Removing it brings
coverage to 100%.

https://claude.ai/code/session_01P9qVe3TkpdEfGtupr8TxC4
@codecov
Copy link
Copy Markdown

codecov Bot commented Mar 8, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

Remove unused imports (logging, pytest, patch, PropertyMock) and
unused local variables (status) flagged by ruff.

https://claude.ai/code/session_01P9qVe3TkpdEfGtupr8TxC4
@roesel roesel merged commit 3b20feb into main Mar 8, 2026
4 checks passed
@roesel roesel deleted the claude/improve-code-coverage-8AxCz branch March 8, 2026 13:01
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.

2 participants