htpprunner4快速入门

1.

新建project

$ httprunner startproject demo

2.编写数据存放,如数据驱动:

username,password
test1,111111
test2,222222
test3,333333

然后在 YAML/JSON 测试用例文件中,就可以通过内置的 parameterize(可简写为 P)函数引用 CSV 文件。

假设项目的根目录下有 data 文件夹,account.csv 位于其中,那么 account.csv 的引用描述如下:

config:
  name: test suite demo
  base_url: "https://postman-echo.com"

testcases:
-
  name: test case 1
  parameters:
    username-password: ${P(data/account.csv)}
  testcase: /path/to/testcase1

 

要嫌弃麻烦还可以通过函数:

例如,若需对 user_id 进行参数化数据驱动,参数取值范围为 1001~1004,那么就可以在 debugtalk.py 中定义一个函数,返回参数列表。

def get_user_id():
    return [
        {"user_id": 1001},
        {"user_id": 1002},
        {"user_id": 1003},
        {"user_id": 1004}
    ]

然后,在 YAML/JSON 的 parameters 中就可以通过调用自定义函数的形式来指定数据源。


config:
  name: test suite demo
  base_url: "https://postman-echo.com"

testcases:
-
  name: test case 1
  parameters:
    user_id: ${get_user_id()}
  testcase: /path/to/testcase1

另外,通过函数的传参

二、testcase:关联参数化:

config:
  name: testacse-demo
  variables:              # 配置变量(config variables)
   username: "test1"
   password: 111223
  base_url: "http://127.0.0.1:8099"
  verify: False

teststeps:
  - name: 这个是post demo
    request:
      method: POST
      url: /login
      headers:
        User-Agent: "PostmanRuntime/7.29.0"
        Content-Type: "application/json"
      json:
        username: $username
        password: $password
    extract:
      userid: "body.userid"
    validate:
      - eq: ["body.code",200]
      - eq: ["body.userid",99]
  - name: 这个是testget请求demo
    variables:
      sum_v: "${sum_two(1, 2)}"
    request:
      method: GET
      url: /getUser/Info
      params:
        userid: $userid
      headers:
        User-Agent: "PostmanRuntime/7.29.0"
    extract:
      code: "body.code"
    validate:
      - eq: [ "body.userid","99" ]

 

 

 

 

 接口2:

 

 

 登录后获取userid 参数化给第二个get接口使用

三、testsuite:

# testsuites目录:
# 新建一个testsuite.yml

config:
  name: test suite demo
  variables:  # testsuite config variables
    var2: "xxxx"
  base_url: "http://127.0.0.1:8099"

testcases:
  -
    name: test case 1
    variables:  # testcase variables
      uid: 10001
    testcase: /testcases/test_get_post.yml
    export: ["varA", "varB"]  # export variables
  -
    name: test case 2
    variables:  # testcase variables
      uid: 1002
    testcase: /testcases/test_export_other_case.yml

 引用几个testcase组件橙suite

四、testcase之间变量传递:

通过export导出变量userid供给其他case使用

# test_exort_othercase_var.yml
config:
  name: 引用其他用例在当前测试用例
  variables: # 配置变量(config variables)
    var1: "12333"
teststeps:
  -
    name: 引用testcase demo,并且导出引用的用例变量userid,让其他case使用
    variables:
      foo1: testcase_ref_bar1
      expect_foo1: testcase_ref_bar1
    testcase: testcases/test_get_post.yml
    export:
      - userid

五、日志和报告详细程度

 

hrun testcases --html=reports/index.html --self-contained-html --log-level debug

 

 

posted @ 2022-08-04 22:10  不带R的墨菲特  阅读(275)  评论(0)    收藏  举报