Record Python interface automation test (first)

Source: Internet
Author: User

The first 2 months learned to use Jmeter+ant to do interface testing, and realized the interface of the batch maintenance management (about 500 cases), the "Interface" and "Interface Test" has a basic understanding, recently found some use Python interface test data, On the one hand, in order to learn how to use Python for interface testing (how to make a simple basic interface test framework), on the other hand in order to exercise how to write code in Python (contact Python for a while, but each time their own programming always have the feeling of forgetting words, really owe practice!! )

First of all, using Python to do interface testing, mainly used is the requests library and JSON library.

1. Start by looking at a simple demo

1 #-*-coding:utf-8-*-2 ImportRequests3Get_url ='HTTP://192.168.0.53:7001/COMMONSERVICE/API/USER/LOGIN.V'4data = {'Usercode':'csqy123456','userpwd':'123456'}5R = Requests.get (Url=get_url, Params=data, timeout=5 #产生一个名为r的Response对象, you can get the information we want from this object; #get请求传参数时, use the params keyword #timeout参数用来设定停止等待响应的时间6 Print(r.url) #返回请求url 7 Print(R.json ()) #以JSON格式解析响应内容8 Print(r.status_code) #返回状态码9 Print(R.raise_for_status ()) #如果发送了一个错误请求, such as 404, 500, can be thrown by raise_for_status () to throw an exception Ten Print(r.encoding) #查看requests使用了什么编码, and you can change it with the R.encoding property  One Print(R.raw) #获取来自服务器的原始套接字响应 A Print(r.headers) #服务器返回给我们的响应头信息, you can also customize the request header by headers=xxx at the time of the parameter transfer  - Print(r.request) #获取原来创建的Request对象 - Print(r.request.headers)#发送到服务器的请求头
1 #-*-coding:utf-8-*-2 ImportRequests 3Post_url ='HTTP://192.168.0.53:7001/COMMONSERVICE/API/CONTROL/STARTBATCHCONTROL/ADD.V'4data = {'Approveseq':'6610','Usercode':'qyzh123456_88','ProjName':'Test Project'}5R = Requests.post (Post_url, Data=payload2, timeout=1)   #get请求传参数时, use the data keyword
6Print(R.json ())7Print(R.status_code)

2. Then use the function call to send the Get/post request

#-*-coding:utf-8-*-ImportRequestsdefsend_get (URL, data):"""define the Send_get function to receive parameters and send get requests"""R= Requests.get (Url=url, params=data) Result=R.json ()returnresultdefsend_post (URL, data):"""define the Send_post function to receive parameters and send a POST request"""R= Requests.get (Url=url, Data=data) Result=R.json ()returnresultdefMain (URL, method, data):"""define a main function that calls Send_post () or Send_get (), depending on whether the method is get or post"""    ifmethod = ='POST': R= Send_post (URL, data)#if it is a POST request, call Send_post ()    Else: R= Send_get (URL, data)#if it is a GET request, call Send_get ()    returnR#return the resultsURL='HTTP://192.168.0.53:7001/COMMONSERVICE/API/CONTROL/CONTROLPROGRESS/QUERY.V'Data= {    'Controlseq':'2018118325'}demo= Main (URL,'GET', data)Print(demo)

3. Define a class that encapsulates the post request and the GET request

#-*-coding:utf-8-*-ImportRequestsImportJSONclassRunmain:"""contains constructors"""    def __init__(Self, URL, method, Data=None): self.t=self.run_main (URL, method, data)defsend_post (self, URL, data): R= Requests.post (Url=url, Data=data) Result=R.json ()returnJson.dumps (result, indent=2, Sort_keys=false, ensure_ascii=False)defsend_get (self, URL, data): R= Requests.get (Url=url, params=data) Result=R.json ()returnJson.dumps (result, indent=2, Sort_keys=false, ensure_ascii=False)#processing return data formats with Json.dumps        #indent=2 indents the output by 2 characters        #Sort_keys=false, whether the output is sorted by keyword        #json.dumps ASCII encoding used by default in Chinese when serializing, Ensure_ascii=false will not output Chinese        #return result    defRun_main (self, URL, method, Data=None):ifmethod = ='GET': R=self.send_get (URL, data)Else: R=self.send_post (URL, data)returnRif __name__=='__main__': URL='HTTP://192.168.0.53:7001/COMMONSERVICE/API/CONTROL/CONTROLPROGRESS/QUERY.V'Data= {        'Controlseq':'2018118325'} Test= Runmain (URL,'GET', data)#because there is a constructor __init__, you need to take parameters when instantiating    Print(TEST.T)

The following is a comparison chart before and after json.dumps use

This concludes with a brief introduction to the usage of the requests, and a basic encapsulation of the post and get requests on the other hand, which is invoked by instantiating the object

Record Python interface automation test (first)

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.