Write a simple interface test using Python request

Source: Internet
Author: User

    • Retrieving return data using requests request
    • Encapsulation into functions
    • Encapsulation into class

First, the data returned by using requests request post

Import requestsimport json# data={#     ' username ': ' Sun ', #     ' password ': ' 123 ', #     ' data ': ' Test ' #}# res= Requests.post (url= ' http://127.0.0.1:8000/login/', Data=data) # print (res.text) # Print (Type (Res.json ()))

Two, encapsulated into a function

#封装成函数url = ' http://127.0.0.1:8000/login/' data={' username ': ' sun ', ' password ': ' 123 ', ' Data ': ' Test '}def send_post (url , data):    #post->get is the Get interface, but the test data above is the POST request, and get has no test    res=requests.post (url=url,data=data). JSON ()    #格式化json数据, indent=2 is a space, sort is sorted by    return Json.dumps (res,indent=2,sort_keys=true) def send_get (Url,data):    #post->get is the    res=requests.get (url=url,data=data) of the Get interface. JSON ()    #格式化json数据, indent=2 is a space, sort is sorted    return Json.dumps (res,indent=2,sort_keys=true) def run_main (Data,url,method):    res=none    if method== ' GET ':        res=send_get (Url,data)    else:        res=send_post (url,data)    return resdef run_main01 (URL, Method,data=none): #默认参数放在最后面的位置    res = None    If method = = ' GET ':        res = send_get (URL, data)    else:        res = send_post (URL, data)    return res# print (Send_post (url,data)) # Print (Run_main (Data,url, ' post '))

Three, packaging into categories:

#进一步, encapsulating class Runmain: #构造函数 # def __init__ (Self,url,method,data=none): # Self.res=self.run_main (Url,method, DATA) def send_post (Self,url, data): # Post->get is the Get interface, but the above test data is a POST request, get does not test res = Requests.post (Url=url, Data=data). JSON () # Format the JSON data, indent=2 is a space, sort is sorted # return Json.dumps (res, indent=2, Sort_keys=tru E) return res def send_get (Self,url, data): # Post->get is the res of the Get interface = Requests.get (Url=url, DAT A=data). JSON () # Format JSON data, indent=2 is a space, sort is sort # return Json.dumps (res, indent=2, sort_keys=true) ret Urn Res def run_main (Self,url, Method, Data=none): res = None if method = = ' GET ': res = SELF.S End_get (URL, data) else:res = Self.send_post (URL, data) return res# This is the way to instantiate each time before you call Run_main        If __name__== "__main__": run=runmain () url = ' http://127.0.0.1:8000/login/' data = {' username ': ' Sun ', ' Password ': ' 123 ',       ' Data ': ' Test ' Print (Run.run_main (URL, ' POST ', data)) #可以直接写一个构造函数, automatically executes run_main when instantiated # Add a constructor method # if __na  me__== "__main__": # url = ' http://127.0.0.1:8000/login/' # data = {# ' username ': ' Sun ', # ' password ': ' 123 ', # ' data ': ' Test ' #}# run=runmain (URL, ' POST ', data) #实例化的同时执行构造函数 # print (run.res)

  

Write a simple interface test using Python request

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.