• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

pytest接口自动化测试框架 | 对requests进行二次封装

武飞扬头像
热爱编程的通信人
帮助1

视频来源:B站《冒死上传!pytest接口自动化测试框架(基础理论到项目实战及二次开发)教学视频【软件测试】》

一边学习一边整理老师的课程内容及试验笔记,并与大家分享,侵权即删,谢谢支持!


在项目中要考虑不同的接口请求方式,有的接口时get,有的接口时post,并且参数也是不一样的,有些要的是json传参,有的用data传参,有些用put,有些用delete,要的不同方式。

所以我们提前封装,封装意味着这个方式能适应所有的请求

  1. 通过同一个session发送请求 为什么?
  1. 需要传入不同的请求方式,地址,参数类型,数据,可能还有其他数据。根据不同的数据类型传不同的处理

test2.py

  1.  
    import requests
  2.  
     
  3.  
    class HttpClient:
  4.  
    # 只要用到这个类 就会进入到init这个函数里面去
  5.  
    def __init__(self):
  6.  
    self.session = requests.session()
  7.  
     
  8.  
    # 封装请求 post delete get put .. 请求方式
  9.  
    # 接口地址 不同的接口
  10.  
    # 接口参数 不同的参数
  11.  
    # 参数类型 表单 json
  12.  
    # 请求头 数据类型设置。。 **kwargs 接受几个参数 post Post poST
  13.  
    # 测试登录 请求方式post 地址login 参数admin password,数据类型 json
  14.  
    def send_request(self, method, url, param_type, data=None, **kwargs):
  15.  
    # 请求方式转成大写 POST
  16.  
    method = method.upper()
  17.  
    # 参数类型转成大写 JSON
  18.  
    param_type = param_type.upper()
  19.  
    # 判断 post get
  20.  
    if 'GET' == method:
  21.  
    response = self.session.request(method=method, url=url, params=data, **kwargs)
  22.  
    elif 'POST' == method:
  23.  
    if 'FROM' == param_type:
  24.  
    # 参数 json 提交 data提交 判断传的类型
  25.  
    response = self.session.request(method=method, url=url, data=data, **kwargs)
  26.  
    else:
  27.  
    response = self.session.request(method=method, url=url, json=data, **kwargs)
  28.  
    elif 'DELETE' == method:
  29.  
    if 'FROM' == param_type:
  30.  
    # 参数 json 提交 data提交 判断传的类型
  31.  
    response = self.session.request(method=method, url=url, data=data, **kwargs)
  32.  
    else:
  33.  
    response = self.session.request(method=method, url=url, json=data, **kwargs)
  34.  
    elif 'PUT' == method:
  35.  
    if 'FROM' == param_type:
  36.  
    # 参数 json 提交 data提交 判断传的类型
  37.  
    response = self.session.request(method=method, url=url, data=data, **kwargs)
  38.  
    else:
  39.  
    response = self.session.request(method=method, url=url, json=data, **kwargs)
  40.  
    else:
  41.  
    raise ValueError
  42.  
     
  43.  
    return response
  44.  
     
  45.  
    def close_session(self):
  46.  
    self.session.close()
学新通

test3.py

  1.  
    # 测试登录
  2.  
    from test2 import HttpClient
  3.  
     
  4.  
    httpclient = HttpClient()
  5.  
     
  6.  
    # 登录接口
  7.  
    url = 'http://39.98.138.157:5000/api/login'
  8.  
    data = {"password": "123456", "username": "admin"}
  9.  
    # res = requests.post(url, json=data)
  10.  
    # method, url, param_type, data, **kwargs
  11.  
    res = httpclient.send_request(method='post', url=url, param_type='json', data=data)
  12.  
    print(res.json())
  13.  
     
  14.  
     
  15.  
    # 工牌值取出来 放在一个变量中 下个接口使用的时候 直接拿变量
  16.  
    # 取值 转成的是字典 字典怎么取值 键值对 拿到键就拿到值
  17.  
    token = res.json()['token']
  18.  
     
  19.  
     
  20.  
    url = 'http://39.98.138.157:5000/api/getuserinfo'
  21.  
    header = {'token': token}
  22.  
    res = httpclient.send_request(method='get', url=url, param_type='json', headers=header)
  23.  
    print(res.json())
学新通

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhfjbebb
系列文章
更多 icon
同类精品
更多 icon
继续加载