我想模拟某个模块以测试使用该模块的一段代码。
也就是说,我有一个my_module要测试的模块。my_module导入一个外部模块real_thing并调用real_thing.compute_something():
#my_module
import real_thing
def my_function():
return real_thing.compute_something()
我需要模拟real_thing,以便在测试中它的行为类似于fake_thing我创建的模块:
#fake_thing
def compute_something():
return fake_value
测试调用my_module.my_function()which 调用real_thing.compute_something():
#test_my_module
import my_module
def test_my_function():
assert_something(my_module.my_function())
我应该在测试代码中添加什么以便在测试中my_function()调用fake_thing.compute_something()而不是real_thing.compute_something()?
我试图弄清楚如何使用Mock做到这一点,但我没有。