简体   繁体   English

(Monkey) 用于微型 python 单元测试的补丁

[英](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.鉴于 micropython 具有某种形式的单元测试库,我该如何做猴子补丁或类似的操作来替换测试用例中的系统对象输出或输入。

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你可以试试我在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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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