我正在尝试使用模拟来模拟HTTP请求调用,因为我不想Behave
实际调用它。
所以我在matches.py
文件中有此代码方案:
import request
def get_match():
response = request.get("https://example.com")
return response
并且在我的行为的步骤定义match_steps.py
中,我有这个:
def logoport_matches_response(context):
mock_response = context.text # this is the payload will come from feature file
with patch ('match') as mock_match:
mock_match.get_match.return_value = {"status": "success"}
但是似乎这不起作用,因为它仍在请求实际的HTTP请求。
我需要模拟get_match
方法以返回{"status": "success"}
结果
好吧,我知道了,您需要将您的初始化放在模拟中,这样:
from matches import get_match
with patch ('match') as mock_match:
mock_match.retun_value = {"status": "success"}
get_match()