3

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

(第一阶段):客户端使用我的应用程序公开的 API。映射的 API 函数向 3rd 方 API(CaaS 提供者 Tropo)发出 HTTP 连接请求

(第二阶段):Tropo 服务器(第 3 方)用一些 url 回复我的服务器,我映射到函数,该函数向 Tropo 服务器发送另一个请求,在他们这边,调用()到电话号码。


我只使用我的 API 来使用 Django 测试客户端,但问题是它也回复 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()

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

4

1 回答 1

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 对此类问题有更好的解决方案

于 2013-04-09T22:25:45.620 回答