我正在尝试使用py.test
def get_service_data():
client = requests.session()
# some task on the client object
resp = client.get('https://apiendpoint.xxx.com/yyy/', params={...})
temp_json = resp.json()
result = []
# lots of processing on temp_json
return result
但是,如果我monkeypatch requests.session()
,则模拟对象将不具有get()
方法。如果我修补requests.session.get()
,那么测试代码将输出另一条错误消息:
> monkeypatch.setattr('requests.session.get', lambda: {})
E Failed: object <function session at 0x1046e3500> has no attribute 'get'
我应该如何测试上面的代码?