【发布时间】:2013-08-27 10:39:56
【问题描述】:
我有兴趣测试一些使用“随机”模块的代码,并且我希望能够在我的测试运行时修补/插入我自己的随机随机版本,它返回一个已知值,并且之后将其放回正常的随机模块。从文档中我只能看到我可以修补类。有没有办法修补功能?像这样的:
def my_code_that_uses_random():
return random.choice([0, 1, 2, 3])
with patch.function(random.choice, return_value=3) as mock_random:
choice = my_code_that_uses_random()
assert choice == 3
该代码不起作用,我需要什么?
【问题讨论】:
标签: python unit-testing python-3.x mocking