简体   繁体   中英

(Monkey) patch for micro python unit test

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, Ie network or file system objects replaced with mocks using patch - or a similar manual way of overriding the system objects for test purposes.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM