用于测试和开发的模拟python http请求

2024-10-01 04:53:13 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用的是python“请求”,它工作得很好,但是现在我希望能够“模拟”响应,而不是接触真正的服务器。在

我这样做:

r = requests.get(self.url+'/somepath', params=payload)

在guzzlephp中,我可以这样做:

^{pr2}$

在请求或python中有类似的东西吗?在

谢谢!在


Tags: self服务器urlgetparamsrequestspayloadpr2
1条回答
网友
1楼 · 发布于 2024-10-01 04:53:13

你可以用这个图书馆 https://requests-mock.readthedocs.io/en/latest/index.html做一些类似的事情

>>> import requests
>>> import requests_mock

>>> with requests_mock.Mocker() as m:
...     m.get(self.url+'/somepath', text='mocked response')
...     requests.get(self.url+'/somepath').text
...
'mocked response'

相关问题 更多 >