[qa] Add mocks to unit tests#48
Conversation
nemesifier
left a comment
There was a problem hiding this comment.
JSON formatting looks a lot better now. Thank you! Regarding the technical limitation with SNMP and in general on this subject, I understand, do what you can but if it's too much work leave it.
Let's focus your effort on achieving the goal of mocking each call to the network during tests.
We can hard-code some fake credentials in the tests, what do you think? |
|
@pandafy Done, all tests should now run without PS: I noticed that tests for HTTP backend are not included when you run this command. |
pandafy
left a comment
There was a problem hiding this comment.
PS: I noticed that tests for HTTP backend are not included when you run this command.
We will need to fix this. Adding from .http import * to tests/__init__.py seems to fix this issue. Can you confirm?
We can hard-code some fake credentials in the tests, what do you think?
Thank you for making this change.
pandafy
left a comment
There was a problem hiding this comment.
@purhan since we are mocking tests, can we remove the need to defined test-settings.json? What will we have to achieve to be able to do that?
I developed some doubts regarding above review of mine. I forgot to bring it up on the call but after a quick discussion with Federico, we concluded that it be good to have the ability to run the tests with or without real device.
The current state of the patch works well to run tests without a real device. This is good for running tests in CI. So we have one part already done.
For the second part, I was thinking to have a runtests script (like other modules of openwisp) for running tests. In that script we can add a flag like --test-settings that can be used to provide path to test-settings.json.
User/Developer will be required to call the script like this
./runtests.py --test-settings=test-settings.json
On executing above command actual network calls should be executed in tests instead of mocks. This will ensure that along with the test suite, our mocks are also correct.
What do you think about this?
I have a couple of queries related to this:
Possible solution: we can make use of environment variables in
Possible solution: we can define a class like this: class DisableMock(object):
...
def start(self):
pass
def stop(self):
pass
def __enter__(self, *args, **kwargs):
return self
def __exit__(self, *args, **kwargs):
passWe can then use this class to disable any patch: if settings['disable_mocks']:
self.ssh_patcher = DisableMock()Edit: Done in f6f4eb5 |
nemesifier
left a comment
There was a problem hiding this comment.
Is this PR meant to be tested with python2?
|
Earlier, I was patching internal functions of netengine, eg: # This will not detect if `next` breaks, which is an internal function of netengine
'netengine.backends.snmp.openwrt.SNMP.next'Now: # `nextCmd` comes from pysnmp and is not a part of netengine
'netengine.backends.snmp.base.cmdgen.CommandGenerator.nextCmd'Now all the patched functions are from external libraries only |
pandafy
left a comment
There was a problem hiding this comment.
GitHub was not letting me comment without posting this incomplete review. i will do a followup with another review.
Yes, It may have caused issues. |
pandafy
left a comment
There was a problem hiding this comment.
Good progress @purhan!
I like the SpyMock implementation. But, I believe @nemesisdesign meant decorating the tests with the patched object instead of using context manager each time like it is possible with unittest.mock.patch. See this example:
pandafy
left a comment
There was a problem hiding this comment.
LGTM 👍🏼
Let's make improvements to specific things in another PR and merge this one to proceed to other things. I have opened #60 for discussing such improvements.
Deferring merge to @nemesisdesign
Co-authored-by: Gagan Deep <the.one.above.all.titan@gmail.com>
Co-authored-by: Gagan Deep <the.one.above.all.titan@gmail.com>
| Install test reqirements:: | ||
|
|
||
| pip install nose | ||
| pip install -r reqirements.txt |
There was a problem hiding this comment.
@purhan this should not be necessary, please double check
Replaced calls with mocks for the following unit tests:
Closes #46