# Coding:utf-8
"""
Compatible for python2.x and python3.x
REQUIREMENT:PIP Install Requests
"""
Import requests
# Request sample URL default request parameter has been URL encoded
From Httprunner Import response
Import UnitTest
Class case (UnitTest. TestCase):
def tearDown (self):
Pass
def test_01 (self):
url = ' Http://120.76.205.241:8000/tools/phone_number_ascription '
body={
' PhoneNumber ': ' 18301994686 ',
' Apikey ': ' K416xowt4zqtvm2n641lcpueizpgrhxewuhqa5hghtzsphi13uhixkbvdoofueen '
}
headers = {
' accept-encoding ': ' gzip ',
' Connection ': ' Close '
}
r = Requests.get (URL, params=body,headers=headers)
Resp_obj = Response. Responseobject (R)
Jsons=resp_obj.json
data=resp_obj.json[' data '
For h in data:
x= ' 18301994686 '
b = Len (h)
# if x in H:
# print (' ID phone number returned successfully ', ': ', h[x])
if x = = h[' id ']:
Print (' phone number returned correctly ', ': ', h[' id ')
Else
Print (' phone number returned mismatch ')
If b==5:
Print (' Test length pass ')
Else
Print (' field returned not full ')
Self.assertequal (' 18301994686 ', h[' id ') # Compare List ID Login This string
Self.assertequal (' Shanghai Mobile 183 card ', h[' Cardtype ')
Self.assertin (' location ', h) #对比字典里面是否存在这个字段
Self.assertequal (' phone_number_ascription ', jsons[' Appcode ')
def tearDown (self):
Pass
if __name__ = = ' __main__ ':
Unittest.main (verbosity=2)
# Assert related instances
# self.asserttrue (hasattr (resp_obj, ' status _code '))
# self.asserttrue (hasattr (resp_obj, ' headers '))
# self.asserttrue (hasattr (resp_obj, ' content '))
# self.assertin (' Content-type ', resp_obj.headers)
# self.assertin (' Content-length ', resp_obj.headers)
# self.assertin (' success ', Resp_obj.json)
Python request Deom based on UnitTest requests