使用Mock进行第三方API的Django / Python测试

时间:2012-12-20 01:40:21

标签: python django unit-testing mocking tropo

我有一个代码,我试图使用Mock库/ python-django进行测试。简要总结一下我的申请是:

(第一阶段):客户端使用我的app暴露API。映射的API函数向第三方API(Tropo是CaaS提供者)发出HTTP连接请求

(第二阶段):Tropo服务器(第三方)用一些网址回复我的服务器,我将其映射到函数,该服务向Tropo Server发送另一个请求,在他们一边,拨打电话()给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()

我如何模拟这个特定的连接请求?

1 个答案:

答案 0 :(得分:0)

我能够通过在我的代码中模拟类来达到某种程度的测试。

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可以更好地解决此类问题,请告诉我