【发布时间】:2021-12-18 12:04:00
【问题描述】:
我正在尝试模拟这个 urlopen 函数。从其他类似问题中得到了几个解决方案,但似乎都没有。
def lambda_handler(event, context):
try:
request = urllib.request.Request(url = URL, data = data, method = method, headers = headers)
with urllib.request.urlopen(request) as httpResponse:
# print(httpResponse.read().decode('utf-8'))
string = httpResponse.read().decode('utf-8')
response = json.loads(string)
print(response)
return response
尝试的方法:
class mock_request():
class Request():
....
def urlopen(*args, **krwargs):
return {}
@mock.patch(.....urllib.request, mock_request)
【问题讨论】:
标签: request mocking urllib python-unittest