httprunner 环境变量、Debugtalk辅助函数、set/tear down_hooks使用、参数化

2021-02-04 08:14

阅读:409

标签:none   loading   ==   webkit   tca   on()   res   htm   home   

环境变量设置

1.项目中添加.env的文件

2.在项目中使用${.env(变量名)}进行使用

技术图片

技术图片技术图片
- config:
    name: "phpwind论坛的首页模拟"
    base_url: ${ENV(URL)}
    requests:
      headers:
        "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9"
        "Accept-Encoding": "gzip, deflate"
        "Accept-Language": "zh-CN,zh;q=0.9"
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.61 Safari/537.36"
    export:
      - code
      - info
      - contenttype
      - content

- test:
    name: "phpwind论坛的首页模拟"
    request:
      url: "/phpwind/"
      method: GET
    extract:
      - code: status_code
      - info: reason
      - contenttype: headers.Content-Type
      - content: title="(.+?)">biaoti826
    validate:
      - eq: ["status_code",200]
      - str_eq: [$content,biaoti826]
View Code

Debugtalk辅助函数

1.在debugtalk.py中编写的方法可以在httprunner中的yml文件中使用(${方法名})

技术图片技术图片
import requests

def get_token_id():
    get_param_data ={grant_type: client_credential,
                     appid: wxec83eaada223a9c8,
                     secret: 1867d7f1cabb3bafae0b7304e8251a09}
    response = requests.get(url=https://api.weixin.qq.com/cgi-bin/token,
                            params=get_param_data)
    return response.json()[access_token]


if __name__ == __main__:
    print(get_token_id())
View Code
技术图片技术图片
# debugtalk使用
- config:
    name: "获取token -- 用户管理~查看粉丝基本信息操作"
    base_url: "https://api.weixin.qq.com"
    variables:
      - tokenid: ${get_token_id()}

- test:
    name: "查看粉丝基本信息"
    request:
      url: "/cgi-bin/user/info"
      method: GET
      params:
        access_token: $tokenid
        openid: "od-53v0GMqGTEiPY-QC549RTXkCk"
        lang: zh_CN
    validate:
      - eq: [content.language,zh_CN]
View Code

 set/tear down_hooks使用

1.既可以写在config中,也可以写在test中

技术图片技术图片
- config:
    name: "获取token -- 用户管理~查看粉丝基本信息操作"
    base_url: "https://api.weixin.qq.com"
    variables:
      - tokenid: ${get_token_id()}
    setup_hooks:
      - ${setup_case()}
    teardown_hooks:
      - ${teardown_case()}

- test:
    name: "查看粉丝基本信息"
    request:
      url: "/cgi-bin/user/info"
      method: GET
      params:
        access_token: $tokenid
        openid: "od-53v0GMqGTEiPY-QC549RTXkCk"
        lang: zh_CN
    setup_hooks:
      - ${setup_step()}
    teardown_hooks:
      - ${teardown_step()}
    validate:
      - eq: [content.language,zh_CN]
View Code

参数化使用

方法一:

1.新建test_suites文件夹

2.新建yaml文件如下

技术图片技术图片
config:
  name: "test suite"

testcases:
  - name: "Create tags"
    testcase: testcases\httprunnerhomework02\create_tag.yml
    parameters:
      tag_name: [test11,test12,test13,test14,test15,test16,test17,test18,test19,test20]
View Code

3.使用 hrun test_suites\yml文件执行

httprunner 环境变量、Debugtalk辅助函数、set/tear down_hooks使用、参数化

标签:none   loading   ==   webkit   tca   on()   res   htm   home   

原文地址:https://www.cnblogs.com/ClownAlin/p/13143364.html


评论


亲,登录后才可以留言!