Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
class C: def f(): calls g def g(): # Do something
如何在导入 C 类的测试模块中模拟 g 来测试 f?
您可以使用这样的路径对象来模拟该功能;
with patch.object(C, 'g', MagicMock(return_value='something')): c.f()
这样,当您的代码调用 g 函数时,它会返回模拟响应
注意:使用前需要先导入补丁
您可以从以下链接中找到详细信息:
https://docs.python.org/3/library/unittest.mock.html