Python3+requests: Test scripts with class-encapsulated interfaces

Source: Internet
Author: User

Introduction: The interface test cases are many, we can not write each use case requests,get or requests,post, so the common method to be encapsulated processing

First modification: The GET request and the POST request are defined separately, and the corresponding method is called directly according to different types of requests during the use process

1 ImportRequests2 ImportJSON3 4 defSend_post (url,data,headers):5Response = Requests.post (url=url,data=data,headers=headers). JSON ()6     returnJson.dumps (response,sort_keys=true,indent=4)7 8 defSend_get (url,data,headers):9Response = Requests.get (url=url,data=data,headers=headers). JSON ()Ten     returnJson.dumps (response,sort_keys=true,indent=4) One  A defRun_main (url,headers,method,data=None): -Respose =None -     ifmethod = ='GET': theRespose =send_get (url,data,headers) -     Else: -Respose =send_post (url,data,headers) -     returnrespose +  -URL ='https:// ...' +headers = {'Content-type':'application/x-www-form-urlencoded'} A Print(Run_main (url,headers,method='POST'))

Second modification: Encapsulation with class, creating an instance run in the main function to invoke methods in the class

1 Import Requests2 Import JSON3 classRunmain:4 def send_post (self,url,data,headers):5Response = Requests.post (url=url,data=data,headers=headers). JSON ()6         returnJson.dumps (response,sort_keys=true,indent=4)7 8def send_get (Self,url,params, headers):9Response = requests.Get(Url=url,params=params, headers=headers). JSON ()Ten         returnJson.dumps (response,sort_keys=true,indent=4) One  Adef run_main (Self,url,params, Data,headers,method): -Respose =None -         ifmethod = ='GET': therespose = Self.send_get (URL,params, headers) -         Else: -Respose =self.send_post (url,data,headers) -         returnrespose +  - if__name__ = ='__main__': +Run =Runmain () AURL ='https:// ...' atheaders = {'Content-type':'application/x-www-form-urlencoded'} -Print (Run.run_main (URL,params=none,data=none,headers=headers,method='POST'))

Third modification: The second modification, each time the need to instantiate and then call the corresponding method;

Improvement: Use __init__ Method implementation: The __init__ method is called whenever the class is instantiated

1 Import Requests2 Import JSON3 classRunmain:4def __init__ (Self,url,params, Data,headers,method):5Self.response = Self.run_main (URL,params, Data,headers,method)6 7 def send_post (self,url,data,headers):8Response = Requests.post (url=url,data=data,headers=headers). JSON ()9         returnJson.dumps (response,sort_keys=true,indent=4)Ten  Onedef send_get (Self,url,params, headers): AResponse = requests.Get(Url=url,params=params, headers=headers). JSON () -         returnJson.dumps (response,sort_keys=true,indent=4) -  thedef run_main (Self,url,params, Data,headers,method): -Respose =None -         ifmethod = ='GET': -respose = Self.send_get (URL,params, headers) +         Else: -Respose =self.send_post (url,data,headers) +         returnrespose A  at if__name__ = ='__main__': -URL ='https:// ...' -headers = {'Content-type':'application/x-www-form-urlencoded'} -Run = Runmain (URL,params=none,data=none,headers=headers,method='POST') -Print (Run.response)

Note: For Python classes, examples do not know the classmate, you can refer to:

Http://yangcongchufang.com/%E9%AB%98%E7%BA%A7python%E7%BC%96%E7%A8%8B%E5%9F%BA%E7%A1%80/python-object-class.html

Python3+requests: Test script with Class encapsulation interface

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.