Skip to content

Add unit tests for CiA 402 profile (coverage 36% → 45%)#644

Open
bizfsc wants to merge 1 commit intocanopen-python:masterfrom
bizfsc:test/p402-coverage
Open

Add unit tests for CiA 402 profile (coverage 36% → 45%)#644
bizfsc wants to merge 1 commit intocanopen-python:masterfrom
bizfsc:test/p402-coverage

Conversation

@bizfsc
Copy link
Copy Markdown

@bizfsc bizfsc commented Apr 28, 2026

Summary

Add 24 unit tests for canopen/profiles/p402.py to improve test coverage from 36% to 45%.

Tests Added

  • State402 enum: all states present, statusword decoding via bitmask
  • State decoding: State402.decode_statusword() for all defined states
  • Command words: verify controlword values for every state transition
  • Next-state calculation: walk through the CiA 402 state machine graph
  • Homing status: IS_HOMING / HOMING_COMPLETED / HOMING_ERROR evaluation
  • Operation mode: switching and reading via OD entries 0x6060/0x6061
  • TPDO callback: statusword update propagation through PDO mapping

Approach

Uses a minimal ObjectDictionary with the required DS402 objects (0x6040 controlword, 0x6041 statusword, 0x6060/0x6061 operation mode, 0x6502 supported drives). PDO maps are mocked with unittest.mock.MagicMock to test the TPDO callback path without requiring a real CAN bus.

Coverage

canopen/profiles/p402.py    36% → 45%

The remaining uncovered code is primarily in BaseNode402 methods that require a live CAN bus interaction (SDO reads/writes, NMT state changes, PDO communication).

Add 24 unit tests covering:
- State402 enum and state decoding from statusword
- Command word generation for state transitions
- Next-state calculation through the state machine
- Homing status evaluation
- Operation mode switching and reading
- TPDO callback handling for statusword updates
- Lookup table consistency checks
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