Python request Deom based on UnitTest requests

Source: Internet
Author: User


# 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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.