通过在我的代码中模拟类,我能够达到某种程度的测试。
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 对此类问题有更好的解决方案