-
Notifications
You must be signed in to change notification settings - Fork 6
unit tests with mock i2c #54
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
…long warnings. (120 chars is the norm in many projects and PEP8 allows a maximum of 79.) See https://stackoverflow.com/questions/17319422/how-do-i-set-the-maximum-line-length-in-pycharm#comment58112267_17319775 for discussion.
…hould use the Makefile so they are consistent.)
test/test_notecard.py
Outdated
| import unittest | ||
|
|
||
| import pytest |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why are you importing unittest when pytest is already present?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
unittest supports classes of tests, which I use for reuse of common tests and parameterization of these across different environments.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's a way we can do this without adding a second unit testing framework?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just a fyi - unittest was already being used for mocks before this PR.
unittest is built into python so we could rework the pytest assertions , which at present are just assertions for raised exceptions. But I wouldn't stop using pytest - it's a great test runner, while unittest deals with expressing tests and creating mocks.
pytest is intended to be used with unittest and supports it out of the box - https://docs.pytest.org/en/7.1.x/how-to/unittest.html#:~:text=pytest%20supports%20running%20Python%20unittest,full%20advantage%20of%20pytest's%20features.
All that said, I've reworked the test classes so they don't have unittest.TestCase as a base class, and pytest is fine with that, so long as the class name starts with Test. The module imports are now as they were and unittest is only used for mocking as before. If we want to move away from unittest for mocks, that's probably best done in a separate PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah was it? Shows that it's been too long since I've been in this library.
|
@m-mcgowan I'm having a hard time seeing what has actually changed in this pr so I am inclined to reject it until it's either simplified or broken into multiple PRs. Rather than taking on a giant refactor of how the tests work, why not add the I2C mocks and tests and we can worry about a refactor later, if at all? |
|
I didn't think it was that big a refactor. All I did was factor out the notecard connection from the tests by moving them in to a base class which calls
When we have HIL testing for this, all we have to do is define another class that uses a real I2C/serial connection running the same tests. |
.github/workflows/manual-run.yml
Outdated
| run: | | ||
| # stop the build if there are Python syntax errors or undefined names | ||
| flake8 test/ notecard/ examples/ --count --ignore=E722,F401,F403,W503 --show-source --statistics | ||
| flake8 test/ notecard/ examples/ --count --ignore=E722,F401,F403,W503,E501 --show-source --statistics |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we add a comment explaining what we're ignoring?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
will do. Ideally the github actions should use the makefile so we DRY this up.
| assert userAgent['req_port'] is not None | ||
|
|
||
| port.readline.return_value = "{\"changes\":1}\r\n" | ||
| def test_debug_mode_on_i2c(self): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The first several lines of this look like what get_i2c_and_port does. Can we use that instead?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure. there's definitely more refactoring we could do, I just wanted to do the minimal to get it working.
My main concern is that this PR is doing two things:
My advice would be to strip out the HiL-pieces of this PR and update with just the I2C pieces. |
…ior. Don't use the virtualenv if it doesn't exist.
… and tests. Removes duplication of flake8 flags.
|
I didn't build this specifically to target HIL, I just wanted to factor out the communications channel, which is orthogonal to most of the tests (i.e. the tests are comms-neutral - they don't care about what channel they are using.) It just happens that with that done, adding HIL later will be easier because we can reuse the same mechanism. We want a simple way to run all the comms-neutral tests over both mocked serial and i2c, and IMHO this was the simplest way to do it without causing maintenance headaches. The only other way I can think of is to duplicate (copy and paste) all the existing serial tests, and rework them to use i2c, but then we have a lot of duplication of code, which will only become worse when we add more comms channels to the test suite. |
|
My apologies @m-mcgowan, the combination of an impenetrable diff and mention of HiL caused me to (wrongly) assume that more was going on here than mocking I2C for testing. |
|
No problem! I was going to suggest you view the files side-by-side because then it's much easier to see the diff than what github gives you |
Adds tests over a mock i2c connection.
The tests are factored into common tests for any transport, plus transport-specific tests for mocked i2c and mocked serial.
DRY's up the python commands in github actions by reusing the Makefile.