【发布时间】:2016-11-15 07:49:24
【问题描述】:
我有一个美味的 REST API 资源,假设称为 Resource,它在其 obj_get 方法中从 libs.utils 导入并使用一个名为 get_token 的函数。
所以为了测试这个资源,在我的测试类中我创建了一个如下所示的测试:
mock_get_token = Mock(return_value="something")
@patch("path.to.resource.get_token", mock_get_token)
def test_get_token(self):
params = {"args": "args"}
# following call should call the get_token function in the resource
response = self.client.get("path/to/resource", params)
# do things with the response and make sure I get right output
因此,当我自己运行测试时,@patch 可以正常工作并按预期工作,将函数替换为模拟函数。但是,在我们更大的应用程序测试套件中运行测试会导致补丁失败。
手动尝试将函数替换为模拟函数之类的操作也有一个不成功的补丁。我想知道还有什么可能导致该问题,并且我非常好奇补丁在单独运行测试或与我们的测试套件的较小子集运行时能够正常工作。
【问题讨论】:
标签: python django unit-testing mocking django-testing