• 【Pytest requests模块&】


    一、requests模块

    是一个第三方库,主要用于发送http请求,主要做接口自动化

    1、安装pip install requests

    2、requests模块的全局

    请求:

    • requests.get() 发送get请求
    • requests.post() 发送post请求
    • requests.delete() 发送delete请求
    • requests.put() 发送put请求
    • requests.request()   最核心的方法

    响应

    import requests
    rep = requests.request()
    # 返回字符串的数据  --适用:文本
    rep.text
    # 返回字节格式的数据  --适用:图片或文件
    rep.content
    # 返回字典格式的数据 --适用:json
    rep.json()
    # 状态码
    rep.status_code
    # 状态信息
    rep.reason
    # 返回cookie信息
    rep.cookies
    # 返回编码格式
    rep.encoding
    # 返回响应头信息
    rep.headers

    二、接口实战

    1、接口常见请求方式:get post put delete

    2、请求参数类型:键值对,JSON格式,文件格式

    #案例:get接口请求、设计字段的用例及断言

    import requests
    import time
    import json
    import pytest
    
    # 发送get请求
    url = 'http://tkio-receive.solar-engine.com/receive/turl/Bf6JBva'
    global parms
    parms = {
        # 触点传参时间。is_s2s=1时必传,0时选传。不同渠道有可能传多种格式,详见需求。限制32位字符内。短链传参。
        'ry_touchpoint_ts': int(round(time.time() * 1000)),
        'ts': int(round(time.time() * 1000)),
        # 集成方式。is_s2s=1为服务器上报触点,0为客户端上报触点。0或1时有不同的参数非空校验,详见需求。固定为0或1。都必传
        'is_s2s': '1',
        # 操作系统。限制16字符内。都选传。短链传参。暂不与app_platform互相做校验。
        'os': 'ios'
    }
    # param参数转换成json格式
    json.dumps(parms)
    # 封装get请求
    def send_get():
        global parms
        rep = requests.get(url=url,params=parms,timeout=10, verify=False)
        print(type(parms))
        print(rep.url)
        # 把返回的reponse转换成字典格式
        rep = json.loads(rep.text)
        print(type(rep))
        return rep
    
    # 针对os字段做测试
    def test_idfa1():
        # case1:修改parms里面os的值为2
        parms['os'] = '2'
        re = send_get()
        assert re["status"] == 119
    def test_idfa2():
        # case2:修改parms里面os的值为2
        parms['os'] = '3'
        re = send_get()
        assert re["status"] == 119
    
    if __name__ == '__main__':
        pytest.main()

     # 案例:post请求的接口

  • 相关阅读:
    原始套接字-自定义IP首部和TCP首部
    原始套接字-TCP/IP下三层数据显示
    ARP欺骗分析
    博弈论
    C++ map & set
    selenium+chrome配置环境
    windows下安装python+selenium
    python之configParser模块读写配置文件
    接口测试流程
    Python之读取文件配置
  • 原文地址:https://www.cnblogs.com/frankruby/p/16362069.html
Copyright © 2020-2023  润新知