python-interface Test (IDEA)

Source: Internet
Author: User

Case: interface sends a POST request

Step 1: Write a method for submitting a POST request

Step 2: Write the test data object, the user submits the test data

Step 3: Invoke the method and data to test

Examples show:

Step 1: Write the method Checkapi_post ()

#Coding=utf-8ImportUrllib,urllib2ImportJSONdefCheckapi_post (url,data):#encode the parametersData=urllib.urlencode (data)#. Request to send a POST request indicating that the request target is a previously defined URL, and that the requested content is in dataUrl2=Urllib2. Request (Url,data)#. Urlopen Open the result returned in the previous step, and get the response after the requestResponse=Urllib2.urlopen (URL2)#reads the response content in read ()Apicontent=Response.read ()#convert a str object to a JSON objectcontext=json.loads (apicontent)#reading the value of Retcode in a JSON objectretcode=context['RetCode']        PrintContext#Follow the status of the return to Judge    ifretcode!='0':        Print(retcode+' '+u"Commit Failed")    Else:        Print(retcode+' '+u"Submit Success")        

Step 2: Write the test data object to data and convert the object type to a dictionary type

classdata (object):def __init__(SELF,USER,USERPHONE,TITLE,STOCKID,STOCKNAME,MODELID,USERGRADE,ISFREE,SELFSTR): self. User=User Self. Userphone=Userphone Self. Title=Title Self. Stockid=Stockid Self. StockName=StockName Self. ModelID=ModelID Self. Usergrade=Usergrade Self. Isfree=Isfree Self. Selfstr=Selfstr#object type converted to dictionary typedefconvert_data (obj): Dict={} dict.update (obj.__dict__)        returnDict

Step 3: Invoke the method and data to test

#Coding=utf-8ImportCheckapi_postImportComquestion_data#Request AddressUrl="http://xxxx.xxxx.xx/API/AnalystApp/CommitQuestion"data1=comquestion_data.data ('30010384200','139****1094','Python Automation script 001','000001','Ping An bank', 2,1,2,'python Automated test data 001') Data2=comquestion_data.data ('300034228661','180****1650','Automation Script 002','000001','Ping An bank', 2,1,2,'python Automated test data 001') Data1=Comquestion_data.convert_data (data1) data2=Comquestion_data.convert_data (data2) checkapi_post.checkapi_post (URL, data1) checkapi_post.checkapi_post (URL, DATA2)

Output Result:

{u'Message': U'-1'+ R'RetCode': U'-1', u'retmsg': None}-1commit failed {u'Message': U'python\u81ea\u52a8\u5316\u6d4b\u8bd5\u6570\u636e001', u'RetCode': U'0', u'retmsg': U'Ac7940fe-0803-491c-86f3-4d64c6ea57c9'}0 successful Submission

python-interface Test (IDEA)

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.