2

I can’t find any examples, I will try to make the question specific:

Given that micropython has some form of unit test library, how can I do monkey patch or similar to replace system objects output or input within test cases.

The desire is to write test cases that cannot be mocked without altering the actual implementation code just for test, I.e network or file system objects replaced with mocks using patch - or a similar manual way of overriding the system objects for test purposes.

Mitchell Currie
  • 2,769
  • 3
  • 20
  • 26
  • Here's an example that apparently patches the datetime module https://pypkg.com/pypi/micropython-schedule/f/test_schedule.py – Peter Gibson Nov 23 '17 at 02:52
  • Thanks, looked interesting however it seems to execute fine under python2, but there's no 'mock' module available in the extras library for micrpython. Shame... back to the drawing board – Mitchell Currie Nov 25 '17 at 10:26
  • Yes strange, I can't see how they run the tests. Might be worth contacting the packages maintainer. – Peter Gibson Nov 26 '17 at 23:30

1 Answers1

0

You can try the technique I laid out in https://forum.micropython.org/viewtopic.php?t=4475#p25925

# load in the module for patching (make sure this gets run before other imports)
import modulename
# create a modified version of modulename, cherry-picking from the real thing
patchedmodule = ...
# replace the module with your modified one for all future imports
sys.modules['modulename']=patchedmodule
cefn
  • 2,895
  • 19
  • 28