zoukankan      html  css  js  c++  java
  • 转-python3+requests:使用类封装接口测试脚本

    转载请注明出处:https://www.cnblogs.com/shapeL/p/9045439.html

    前言:接口测试用例较多,我们不可能每个用例都写一次requests,get或者requests,post等,所以对共用方法要进行封装处理

    第一次修改:将get请求和post请求单独定义出来,使用过程中根据不同类型的请求直接调用对应的方法

    复制代码
     1 import requests
     2 import json
     3 
     4 def send_post(url,data,headers):
     5     response = requests.post(url=url,data=data,headers=headers).json()
     6     return json.dumps(response,sort_keys=True,indent=4)
     7 
     8 def send_get(url,data,headers):
     9     response = requests.get(url=url,data=data,headers=headers).json()
    10     return json.dumps(response,sort_keys=True,indent=4)
    11 
    12 def run_main(url,headers,method,data=None):
    13     respose = None
    14     if method == 'GET':
    15         respose = send_get(url,data,headers)
    16     else:
    17         respose = send_post(url,data,headers)
    18     return respose
    19 
    20 url = 'https://...'
    21 headers = {'Content-Type': 'application/x-www-form-urlencoded'}
    22 print(run_main(url,headers,method='POST'))
    复制代码

    第二次修改:用class进行封装,主函数中创建一个实例run来调用类中的方法

    复制代码
     1 import requests
     2 import json
     3 class Runmain:
     4     def send_post(self,url,data,headers):
     5         response = requests.post(url=url,data=data,headers=headers).json()
     6         return json.dumps(response,sort_keys=True,indent=4)
     7 
     8     def send_get(self,url,params,headers):
     9         response = requests.get(url=url,params=params,headers=headers).json()
    10         return json.dumps(response,sort_keys=True,indent=4)
    11 
    12     def run_main(self,url,params,data,headers,method):
    13         respose = None
    14         if method == 'GET':
    15             respose = self.send_get(url,params,headers)
    16         else:
    17             respose = self.send_post(url,data,headers)
    18         return respose
    19 
    20 if __name__ == '__main__':
    21     run = Runmain()
    22     url = 'https://...'
    23     headers = {'Content-Type': 'application/x-www-form-urlencoded'}
    24     print(run.run_main(url,params=None,data=None,headers=headers,method='POST'))
    复制代码

    第三次修改:第二次修改,每次都需要实例化后再调用对应的方法;

    改进办法:使用__init__方法实现:只要实例化类时候就会调用__init__方法

    复制代码
     1 import requests
     2 import json
     3 class Runmain:
     4     def __init__(self,url,params,data,headers,method):
     5         self.response = self.run_main(url,params,data,headers,method)
     6 
     7     def send_post(self,url,data,headers):
     8         response = requests.post(url=url,data=data,headers=headers).json()
     9         return json.dumps(response,sort_keys=True,indent=4)
    10 
    11     def send_get(self,url,params,headers):
    12         response = requests.get(url=url,params=params,headers=headers).json()
    13         return json.dumps(response,sort_keys=True,indent=4)
    14 
    15     def run_main(self,url,params,data,headers,method):
    16         respose = None
    17         if method == 'GET':
    18             respose = self.send_get(url,params,headers)
    19         else:
    20             respose = self.send_post(url,data,headers)
    21         return respose
    22 
    23 if __name__ == '__main__':
    24     url = 'https://...'
    25     headers = {'Content-Type': 'application/x-www-form-urlencoded'}
    26     run = Runmain(url,params=None,data=None,headers=headers,method='POST')
    27     print(run.response)
    复制代码

    备注:对python中类,实例不了解的同学,可以参考:

    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

    https://blog.csdn.net/liguilicsdn/article/details/101062594?utm_medium=distribute.pc_relevant.none-task-blog-title-6&spm=1001.2101.3001.4242

  • 相关阅读:
    ffplay(2.0.1)中的音视频同步
    (转)ffplay的音视频同步分析之视频同步到音频
    (转) 从ffmpeg中提取出YUV数据
    关于解决用tutorial7教程中的代码打造一款自己的播放器中的声音噪音问题
    基于<最简单的基于FFMPEG+SDL的视频播放器 ver2 (采用SDL2.0)>的一些个人总结
    (转)SDL2.0在mfc窗口中显示yuv的一种方法
    (转)SDL1.2到2.0的迁移指南
    APM (应用性能管理)
    在telnet下操作memcache详解(操作命令详解)
    MYSQL主从数据库搭建
  • 原文地址:https://www.cnblogs.com/liangqihui/p/13693756.html
Copyright © 2011-2022 走看看