Skip to content

Add unit tests for LSS master (coverage 42% → 93%)#643

Merged
acolomb merged 7 commits intocanopen-python:masterfrom
bizfsc:test/lss-coverage
Apr 29, 2026
Merged

Add unit tests for LSS master (coverage 42% → 93%)#643
acolomb merged 7 commits intocanopen-python:masterfrom
bizfsc:test/lss-coverage

Conversation

@bizfsc
Copy link
Copy Markdown
Contributor

@bizfsc bizfsc commented Apr 28, 2026

Summary

Add 28 unit tests for canopen/lss.py to improve test coverage from 42% to 93%.

Tests Added

  • Switch state commands: global configuration/waiting mode, selective with match/no-match
  • Node ID configuration: set node ID, error response handling
  • Bit timing configuration: set bit timing, activate with delay
  • Store configuration: success and error responses
  • LSS address inquiry: vendor ID, product code, revision number, serial number
  • Timeout handling: verify LssError raised on missing responses
  • Fast scan: full protocol flow with binary search
  • Obsolete aliases: send_switch_state_global backward compat
  • LssError: exception instantiation and inheritance

Approach

Uses unittest.mock.MagicMock to mock the network layer. A helper method injects CAN responses during send_message() calls to simulate real LSS slave behavior, including only responding to commands that expect a reply per the protocol.

Coverage

canopen/lss.py    42% → 93%
Overall           77% → 80%

Add 28 unit tests covering:
- Switch state global/selective commands
- Node ID and bit timing configuration
- Activate bit timing and store configuration
- LSS address inquiry (vendor/product/revision/serial)
- Timeout and error handling
- Fast scan protocol
- Obsolete method aliases
- LssError exception
@codecov
Copy link
Copy Markdown

codecov Bot commented Apr 29, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

Copy link
Copy Markdown
Member

@acolomb acolomb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good stuff, thank you!

As with other modules' tests, the raw byte sequence should be compared to catch wrong constants definitions, instead of using those from the module to be tested. I reworked that accordingly and fixed some style nits. Few comments remain, please have a look.

Comment thread test/test_lss.py Outdated
Comment thread test/test_lss.py Outdated
Comment thread test/test_lss.py Outdated
- Remove duplicate test_send_switch_state_global_no_response_expected
- Remove duplicate test_lss_address_encoding and unused struct import
- Restructure imports: use 'from canopen import lss' for constants,
  keep class imports on one line, sort alphabetically

Co-authored-by: GitHub Copilot <noreply@github.com>
Tool-assisted: tests were written with AI tooling assistance
Copy link
Copy Markdown
Contributor Author

@bizfsc bizfsc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for reviewing, you are 100% correct and I also like the suggested import-style more.
Disclaimer: I used Claude Opus 4.6 to generate those tests after analysis and reviewed them manually.

@acolomb
Copy link
Copy Markdown
Member

acolomb commented Apr 29, 2026

I used Claude Opus

So I guessed. :-) But that's fine with me as long as the result is sane and reviewed by yourself.

@acolomb acolomb merged commit 4e789fe into canopen-python:master Apr 29, 2026
4 of 5 checks passed
@bizfsc bizfsc deleted the test/lss-coverage branch April 29, 2026 08:39
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