使用Mock进行第三方API的Django / Python测试
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用Mock进行第三方API的Django / Python测试相关的知识,希望对你有一定的参考价值。
我有一个代码,我试图使用Mock库/ python-django进行测试。简要总结一下我的申请是:
(第一阶段):客户端使用我的app暴露API。映射的API函数向第三方API(Tropo,即CaaS提供者)发出HTTP连接请求
(第二阶段):Tropo服务器(第三方)回复一些网址回到我的服务器,我映射到功能,向Tropo服务器发送另一个请求,在他们一边,拨打电话()给phonenumbers。
我使用Django测试客户端只是使用我的API运行,但问题是它也回复Tropo真正调用我给的数字。所以我想使用mock()库但我对它知之甚少。
我做的是,我看到我从Tropo第一阶段得到的响应在变量input
中硬编码,我也有expected_output
变量,这也是第二阶段后看到输出的硬编码。
但是我想正确地构建我的测试框架,我可以在我的测试环境中模拟整个tropo库,并且所有请求都转到这个假库而不是真正的tropo。但不改变代码。
任何想法或建议。因为我是开发人员,请让我裸露,这是我尝试做的测试。
由于我没有得到任何回复,我想提供更多关于我究竟陷入困境的细节:
让我的代码片段在一个函数中...
conn = httplib.HTTPConnection('Some_External_URI')
headers = {"accept": "application/json", "Content-type":"application/json"}
params = ''
conn.request('POST', data, params, headers)
response = conn.getresponse()
payload = response.read()
我如何模拟这个特定的连接请求?
通过在我的代码中模拟类,我能够达到某种程度的测试。
test.py
from mock import patch, MagicMock
from tropo.conferencing import TropoConferencing
@patch.object(TropoConferencing, 'sendTriggerCallRequest')
def test_ConferenceCreation(self, sendTriggerCallRequest):
response_conference = self._createConference(sendTriggerCallRequest)
self.assertContains(response_conference, 200)
def _createConference(self, sendTriggerCallRequest):
self._twoParticipants_PhaseI(sendTriggerCallRequest)
response_conference = self.client.post(self.const.create_conferenceApi , {'title':self.const.title,'participants':self.const.participants})
obj = json.loads(response_conference.content)
self.conf_id = obj['details']['conference_id']
try:
conference_id = Conference.objects.get(conferenceId=self.conf_id)
except Conference.DoesNotExist:
print 'Conference not found'
# PHASE II
self._twoParticipants_PhaseII()
return response_conference
def _twoParticipants_PhaseI(self, sendTriggerCallRequest):
list_of_return_values= [{'json': {'session_id': 'e85ea1229f2dd02c7d7534c2a4392b32', 'address': u'xxxxxxxxx'}, 'response': True},
{'json': {'session_id': 'e72bf728d4de2aa039f39843097de14f', 'address': u'xxxxxxxx'}, 'response': True}
]
def side_effect(*args, **kwargs):
return list_of_return_values.pop()
sendTriggerCallRequest.side_effect = side_effect
def _twoParticipants_PhaseII(self):
input = {"session":{"id":"e72bf728d4de2aa039f39843097de14f","accountId":"xxxxx","timestamp":"2013-01-07T18:32:20.905Z","userType":"NONE","initialText":'null',"callId":'null',"parameters":{"phonenumber":"xxxxxxx","action":"create","conference_id":str(self.conf_id),"format":"form"}}}
expectedOutput = '{"tropo": [{"call": {"to": "xxxxxxx", "allowSignals": "exit", "from": "xxxxxxx", "timeout": 60}}, {"ask": {"name": "join_conference", "say": {"value": "Please press one to join conference"}, "choices": {"terminator": "*", "value": "1", "mode": "dtmf"}, "attempts": 1, "timeout": 5, "voice": "Susan"}}, {"on": {"event": "mute", "next": "' + self.const.muteEvent+ str(self.conf_id) + '/xxxxxx"}}, {"on": {"event": "unmute", "next": "/conference/rest/v1/conference/events/unmute/'+ str(self.conf_id) + '/xxxxxxx"}}, {"on": {"event": "hangup", "next": "'+ str(self.conf_id) + '/xxxxxx"}}, {"on": {"event": "continue", "next": "'+ str(self.conf_id) + '/xxxxxx"}}, {"on": {"event": "exit", "next": "'+ str(self.conf_id) + '/xxxxxx"}}, {"on": {"event": "error", "next": "/conference/rest/v1/conference/events/hangup/'+ str(self.conf_id) + '/xxxxxxx"}}, {"on": {"event": "incomplete", "next": "'+ str(self.conf_id) + '/xxxxxxx"}}, {"say": {"value": ""}}]}'
callbackPayload = json.dumps(input)
request = MagicMock()
request.raw_post_data = callbackPayload
response = call(request)
self.assertEqual(response.content, expectedOutput)
正如你所看到的,我正在硬编码我从Tropo获得的响应并传递给函数。如果有任何QA可以更好地解决这类问题,请告诉我
以上是关于使用Mock进行第三方API的Django / Python测试的主要内容,如果未能解决你的问题,请参考以下文章