zoukankan      html  css  js  c++  java
  • HttpRunner关键词、extract使用、validate断言、关联

    config相应参数

    关键词 是否必须 数据类型 描述
    name YES string 测试用例的名称,在测试报告中将作为标题
    variables NO list of dict 定义全局变量,作用域为整个用例
    parameters NO list of dict 全局参数,用于实现数据化驱动,作为域为整个用例
    request NO dict request的公共参数,作用域为整个用例;常用参数包括base_url和headers
    request关键词中的关键词
    关键词 是否必须 数据类型 描述
    base_url NO string 测试用例请求URL的公共host,指定该参数后,test的url可以只描述path部分
    headers NO dict request 中 headers 的公共参数,作用域为整个用例
    export NO list

    整个用例输出的参数列表,可输出的参数包括公共的variable 和 extract 的参数; 

    在 log-level 为 debug 模式下,会在 terminal 中打印出参数内容 

    test相应参数

    关键词 是否必备 数据类型 描述
    name YES string 测试步骤的名称,在测试报告中将作为测试步骤的名称
    request NO dict HTTP请求的详细内容
    variables NO list of dict 测试步骤中定义的变量,作用域为当前测试步骤
    extract NO list
    从当前 HTTP 请求的响应结果中提取参数,并保存到参数变量中(例如
    token),后续测试用例可通过$token的形式进行引用
    validate NO list
    测试用例中定义的结果校验项,作用域为当前测试用例,用于实现对当前测
    试用例运行结果的校验
    setup_hooks NO list 在 HTTP 请求发送前执行 hook 函数,主要用于准备工作
    teardown_hooks NO list 在 HTTP 请求发送后执行 hook 函数,主要用于测试后的清理工作

    extract:

    # test中的extract参数应用
    - config:
        name: "验证能否打开主网页"
        base_url: "http://www.hnxmxit.com"
    
    # 测试步骤
    - test:
        name: "open hnxmxit mainpage api"
        request:
          url: "/"
          method: GET
        extract:
          - code: "status_code"
          - reason: "reason"
        validate:
          - eq: [$code,200]
          - eq: [$reason,"OK"]
    View Code

    常用extract:status_code, cookies, elapsed, headers, content, text, json, encoding, ok, reason, url.

    json数据如下图extract获取json数据:

    # 模拟带参数的GET请求
    - config:
        name: 验证能否获取所有标签
        base_url: https://api.weixin.qq.com
    
    - test:
        name: get all user tag api
        request:
          url: /cgi-bin/tags/get
          method: GET
          params:
            access_token: "34__rUWIshsx9ftkwdwuRLjrztkqbDRS8WY4UlzWGF8Z7BOUn8Fyq1kejpUNBvZrOmgFYgLX4xsjDIOxMmLuSIW-f4gsgLJAId-K44kNu7rAzVjLR7ecnCIEGw374Hiy9KzgIGvWUWbyg5_nkXHSKOfAHAZRV"
        extract:
          - id: content.tags.3.id
        validate:
          - eq: ["status_code",200]
          - eq: [$id,102]
    View Code

    validate

    断言的两种格式

    # validate中两种断言方式
    - config:
        name: "验证能否打开主网页"
        base_url: "http://www.hnxmxit.com"
    
    # 测试步骤
    - test:
        name: "open hnxmxit mainpage api"
        request:
          url: "/"
          method: GET
        extract:
          - code: "status_code"
          - reason: "reason"
          - time: elapsed.microseconds
          - len: headers.Content-Length
          - title: <title>(.+?)</title>
        validate:
          - eq: [$reason,"OK"]
          - {"check":$reason,"comparator":"str_eq","expect":"OK"}
    View Code
    校验器 意义 可以使用的名称
    eq/equals 判断实际结果和期望结果是否相等 "eq", "equals", "==", "is"
    lt/less_than 判断实际结果小于期望结果 "lt", "less_than"
    le/less_than_or_equals 判断实际结果小于等于期望结果 "le", "less_than_or_equals"
    gt/greater_than 判断实际结果大于期望结果 gt", "greater_than"
    ge/greater_than_or_equals 判断实际结果大于等于期望结果 "ge", "greater_than_or_equals"
    ne/not_equals 判断实际结果和期望结果不相等 "ne", "not_equals"
    str_eq/string_equals 判断转字符串后对比,实际结果和期望结果是否相等 "str_eq", "string_equals"
    len_eq/length_equals 判断字符串或list长度 "len_eq", "length_equals", "count_eq"
    len_gt/length_greater_than 判断实际结果的长度大于和期望结果 "len_gt", "count_gt", "length_greater_than", "count_greater_than"
    len_ge/length_greater_than_or_equals 实际结果的长度大于等于期望结果 "len_ge", "count_ge", "length_greater_than_or_equals", "count_greater_than_or_equals"
    len_lt/length_less_than 实际结果的长度小于期望结果 "len_lt", "count_lt", "length_less_than", "count_less_than"
    len_le/length_less_than_or_equals 实际结果的长度小于等于期望结果 "len_le", "count_le", "length_less_than_or_equals", "count_less_than_or_equals"
    contains 实际结果包含期望结果 contains
    contains_by 实际结果被期望结果包含 contains_by

    request关键词

    • headers:请求头部信息
    • method:请求方式
    • url:请求地址
    • host:请求主机地址
    • params:GET请求参数
    • data:表单形式的参数
    • json:json格式的参数

    httprunner关联

    1.使用extract截取下一个接口需要使用的值

    2.在下一个接口需要关联的传输数据的地方,写$+变量名即可

    - config:
        name: "获取token -- 用户管理~查看粉丝基本信息操作"
        base_url: "https://api.weixin.qq.com"
    
    - test:
        name: "获取token_value"
        request:
          url: "/cgi-bin/token"
          method: GET
          params:
            grant_type: "client_credential"
            appid: "wxec83eaada223a9c8"
            secret: "1867d7f1cabb3bafae0b7304e8251a09"
        extract:
          - tokenid: content.access_token
        validate:
          - eq: ["status_code",200]
          - eq: [content.expires_in, 7200]
    
    - test:
        name: "设置粉丝备注"
        request:
          url: "/cgi-bin/user/info/updateremark"
          method: POST
          params:
           access_token: $tokenid
          json: {"openid":"od-53v0GMqGTEiPY-QC549RTXkCk","remark":"pangzi"}
        extract:
          - errmsg_value: json.errmsg
        validate:
          - eq: [$errmsg_value,'ok']
    View Code
  • 相关阅读:
    Python入门-函数进阶
    Python入门-初始函数
    Leetcode300. Longest Increasing Subsequence最长上升子序列
    Leetcode139. Word Break单词拆分
    Leetcode279. Perfect Squares完全平方数
    Leetcode319. Bulb Switcher灯泡开关
    Leetcode322. Coin Change零钱兑换
    二叉树三种遍历两种方法(递归和迭代)
    Leetcode145. Binary Tree Postorder Traversal二叉树的后序遍历
    Leetcode515. Find Largest Value in Each Tree Row在每个树行中找最大值
  • 原文地址:https://www.cnblogs.com/ClownAlin/p/13123620.html
Copyright © 2011-2022 走看看