httprunner使用02参数化.env环境变量调用debugtalk.py文件的函数关联validate 断言

Posted xiaoshijie

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了httprunner使用02参数化.env环境变量调用debugtalk.py文件的函数关联validate 断言相关的知识,希望对你有一定的参考价值。

一、参数化

1)创建套件

config:
  name: 测试套件

testcases:
#  - name: test_demo_04
#    testcase: testcases/20200610/test_demo_04.yml
#    parameters:
#      search_word: [‘天天向上‘,‘12306‘,‘newdream‘]
  - name: test_demo_04
    testcase: testcases/tasks/demo_05.yml
    parameters:
      tag_name: [test01,test02,test03,test04,test05,test06,test07,test08,test09,test10]

2)参数化使用: $ 参数名(如:$tag_name)

- config:
    name: "创建标签"
    base_url: ${ENV(URL)}
    variables:
      - token: ${get_token()}
- test:
    name: "create tag"
    request:
      url: /cgi-bin/tags/create
      params:
        access_token: $token
      method: POST
      json: {   "tag" : {     "name" : $tag_name  } }
#    {"tag":{"id":198,"name":"2020061302"}}
    validate:
      - gt: [content.tag.id,0]

 3)执行套件,查看报告

使用命令执行:hrun 套件路径/文件名.yml

技术图片

 二、.env环境变量,调用debugtalk.py文件的函数

1)右击项目名,新建文件名为【.env】的文件,配置环境变量

技术图片

 2)右击项目名,新建py文件,名为【debugtalk.py】,自定义函数(如:get_token()函数)

import requests

def get_token():

    get_param_data={
        grant_type:client_credential,
        appid:appid,
        secret:secret
    }

    response = requests.get(https://api.weixin.qq.com/cgi-bin/token,get_param_data)
    return response.json()[access_token]

 3)yml文件调用环境变量($ENV(变量名))和 get_token()函数($函数名)

- config:
    name: "删除标签"
    base_url: ${ENV(URL)}
    variables:
      - token: ${get_token()}
- test:
      name: "delete tag"
      request:
          url: "/cgi-bin/tags/delete"
          headers:
            User-Agent: "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36"
            Content-Type: application/json
          method: POST
          params:
              access_token: $token
          json: {  "tag":{ "id" : 190 } }
      validate:
          - eq: [content.errcode,0]

 三、关联: $参数名

# httprunner关联
- config:
    name: "验证能否获取token值"
    base_url: "https://api.weixin.qq.com"

- test:
    name: "get access_token"
    request:
      url: "/cgi-bin/token"
      method: GET
      params:
        grant_type: "client_credential"
        appid: "xxx"
        secret: "xxx"
    extract:
      - access_token: content.access_token
    validate:
      - eq: ["status_code",200]
      - eq: [content.expires_in,7200] #content响应正文

- test:
    name: "delete tag"
    request:
      url: "/cgi-bin/tags/delete"
      method: POST
      params:
         access_token: $access_token
      json: {   "tag":{ "id" : 192   } }
    validate:
      - eq: [content.errcode,0]

 四、断言

1)validate支持两种格式:

"comparator_name": [check_item, expect_value]

{"check": check_item, "comparator": comparator_name, "expect": expect_value} hooks 

如:

validate:

  - eq: [content.errcode,0]

  - {"check":$reason,"comparator":"str_eq","expect":"OK"} #检查说明,检查方式,期望结果

 技术图片

 2)支持的校验器

源码

def get_uniform_comparator(comparator):
    """ convert comparator alias to uniform name
    """
    if comparator in ["eq", "equals", "==", "is"]:    ##等于
        return "equals"
    elif comparator in ["lt", "less_than"]:     ##小于
        return "less_than"
    elif comparator in ["le", "less_than_or_equals"]:  ##小于等于
        return "less_than_or_equals"
    elif comparator in ["gt", "greater_than"]:   ##大于
        return "greater_than"
    elif comparator in ["ge", "greater_than_or_equals"]:     ##大于等于
        return "greater_than_or_equals"
    elif comparator in ["ne", "not_equals"]:     ##不等于
        return "not_equals"
    elif comparator in ["str_eq", "string_equals"]:  ##字符串相等
        return "string_equals"
    elif comparator in ["len_eq", "length_equals", "count_eq"]:  ##长度相等
        return "length_equals"
    elif comparator in ["len_gt", "count_gt", "length_greater_than", "count_greater_than"]:  ##长度大于
        return "length_greater_than"
    elif comparator in ["len_ge", "count_ge", "length_greater_than_or_equals",    ##长度大于等于
        "count_greater_than_or_equals"]:
        return "length_greater_than_or_equals"
    elif comparator in ["len_lt", "count_lt", "length_less_than", "count_less_than"]:  ##长度小于
        return "length_less_than"
    elif comparator in ["len_le", "count_le", "length_less_than_or_equals",    ##长度小于等于
        "count_less_than_or_equals"]:
        return "length_less_than_or_equals"
    else:
        return comparator            ##自定义

 

技术图片

以访问百度首页举例(其中断言全部通过):

此举例原文链接:https://blog.csdn.net/weixin_42007999/java/article/details/105724379

- config:
name: TestCase

- test:
name: TestStep -1
request: 
url: https://www.baidu.com/
method: GET
headers:
User-Agent: ozilla/5.0 (Windows NT 6.1; Win64; x64; rv:65.0) Gecko/20100101 Firefox/65.0

validate:
# 断言: 是否相等, 返回响应状态码是否为200
- equals: [status_code, 200]

# 断言: 是否包含, 响应实体中是否包含 “title” 字符串
- contains: [content, title]

# 断言: 是否以指定字符串开头, 响应实体是否以“<!DOCTYPE”字符串开头
- startswith: [body, <!DOCTYPE]

# 断言: 是否大于, 响应头域数量是否大于 10 个
- length_greater_than: [headers, 10]

# 断言: 是否类型匹配, 响应实体类型是否为“str”字符串类型
- type_match: [content, str]

以上是关于httprunner使用02参数化.env环境变量调用debugtalk.py文件的函数关联validate 断言的主要内容,如果未能解决你的问题,请参考以下文章

httprunner系列04——环境变量env和脚本函数debugtalk.py的使用

httprunner 2.5.7 下.env 文件环境变量的使用及debugtalk的使用

httprunner学习13-环境变量.env

httprunner 2.x学习20 - env环境变量有#注释和空行处理

05-Httprunner-变量的使用

httprunner 3.x--参数化parameters(参数和值一对多,多对多等)