zoukankan      html  css  js  c++  java
  • 接口测试2-接口测试 get post请求

    一。找接口文档(后端开发人员提供,前端没有开发出来之前测)

    接口文档如下:

    一、测试服务器信息

     测试服务器地址:https://api.weixin.qq.com

     二、接口列表

     2.1 获取access_token

    接口名称

    获取access_token

    接口描述

    获取access_token

    请求地址

    /cgi-bin/token

    请求方式

    get

    输入参数

     

    参数名称

    参数描述

    参数类型

    是否必须

    备注

    grant_type

    client_credential

    String

    必须

     

    appid

    wxaefa08168b458452

    String

    必须

     

    secret

    3d98210ddcdcaed24a3418585f1569dc

    String

    必须

     

     

     

     

     

     

    输出参数

     

     

     

     

    参数名称

    参数描述

    参数类型

     

    备注

    access_token

    用户的access_token

     

     

     

    expires_in

    过期时间

     

     

     

     

     

     

     

     

    开发申请接口数据地址:https://www.juhe.cn/ 可以申请接口进行测试

     比如天气预报(可免费调用500次)

     

    二。设计接口测试用例

    除了正常的测试用例,需要设计异常的用例

    三。开展测试

    第一步,使用python测试接口,需要安装requests库

    打开电脑cmd  ---PIP

    第二步,打开pycharm,导包编码

    编码:

    get类型请求接口
    post类型请求接口
    参数关联接口
    unittest实现接口测试
    数据和代码分离:xlrd--->读取excel表数据

    举实例1:测试天气预报接口-------根据id查询天气(get类型)

     一。分析需求,准备接口文档:https://www.juhe.cn/docs/api/id/39,接口文档如下:

    二。编写用例: 

    三。接口测试python代码编写

    1.用例1

     -----------------------------------------------------------------------------

    # 导包
    import requests

    #给接口地址定义变量名称
    url = "http://v.juhe.cn/weather/index"
    #通过字典的形式保存数据
    para = {"cityname":"南京","key":"0f1c6ff6efc0408eddf0f07e792c6e5c"}

    #发送请求(下面都是request自带的方法)
    r = requests.get(url,params=para)
    #获取返回结果,获取json数据
    print(r.status_code)
    res = r.json()
    print(res["reason"])
    print(res["result"]["sk"]["temp"]) #获取json中result中sk中的temp字段值
    -----------------------------------------------------------

    2.用例2 

     举实例2:测试天气预报接口-------根据GPS坐标自动定位城市,并返回该城市的天气信息(post类型)

     一 需求

    二。编写python测试用例

    -----------------------------------------

    #导包
    import requests
    #根据gps坐标查询天气,使用post请求
    url = "http://v.juhe.cn/weather/geo"
    para = {"lon":"116.39277","lat":"39.933748","dtype":"json","key":"0f1c6ff6efc0408eddf0f07e792c6e5c"}
    #发送请求,post请求中data就是para的数据,para数据包含所有的请求参数
    r = requests.post(url,data=para)
    #获取json数据
    res =r.json()
    print(res)
    print(res["reason"])

    #输入错误的请求查看返回码
    print(res["error_code"])
    ------------------------------

  • 相关阅读:
    118/119. Pascal's Triangle/II
    160. Intersection of Two Linked Lists
    168. Excel Sheet Column Title
    167. Two Sum II
    172. Factorial Trailing Zeroes
    169. Majority Element
    189. Rotate Array
    202. Happy Number
    204. Count Primes
    MVC之Model元数据
  • 原文地址:https://www.cnblogs.com/yinlili/p/13131696.html
Copyright © 2011-2022 走看看