HttpRunner2.x--variables(变量)取值顺序

变量优先级

有几种不同类型的variables,优先顺序可能会造成混淆。避免混淆的最好方法是使用不同的变量名。但是,如果必须使用相同的变量名,则应该了解优先级策略。

API文件

name: login api
variables:
    p_username : test1      # api变量
    p_password: test123456  # api变量
base_url: http://localhost:8000

request:
    url: /api/v1/login/
    method: POST
    headers:
        Content-Type: "application/json"
    json:
        username: $p_username # 使用变量p_username
        password: $p_password # 使用变量p_password
validate:
    - eq: ["status_code", 200]

 

在api文件中设置variables,请求username和password通过$变量名的方式直接取variables中设置的变量名 

测试用例--testcases

config:
    name: "demo testcase"
    variables:
        p_username: test
        p_password: test123456
    base_url: http://localhost:8000
teststeps:
-
    name: login step 1
    api: api/login.yml
    variables:
        p_username: admin2
        p_password: test123456
    extract:
        - token: content.token
    validate:
        - eq: ["status_code", 200]

  

在一个测试用例中,变量优先级按以下顺序排列:

config 和 testteps中的variables名称一样时
优先取config中variables中参数
config中没有设置,取testseps中参数
testcase中参数会覆盖api文件中参数

测试套件

config:
    name: test 测试套件
    variables:
      p_username : test1      # 测试套件-config变量
      p_password: test123456  # 测试套件-config变量
    base_url: http://localhost:8000

testcases:
-
    name: 测试用例test.yml
    testcase: testcases/test.yml
    variables:
        p_username: admin2     # 测试套件-testcases变量
        p_password: test123456 # 测试套件-testcases变量

  

在测试套件中,变量优先级按以下顺序排列:

 

config 和 testcases中的variables名称一样时
优先取config中variables中参数
config中没有设置,取testcases中参数
testsuites中参数会覆盖api文件和testcases中参数

 存在extract提取值变量取值

config:
    name: "demo testcase"
    variables:
        p_username: test
        p_password: test123456
        token: "af82c1394b9d4e95d5fdf34de622785a659600b1"
    base_url: http://localhost:8000
teststeps:
-
    name: login step 1
    api: api/login.yml
    variables:
        p_username: admin2
        p_password: test123456
    extract:
        - token: content.token
    validate:
        - eq: ["status_code", 200]
-
    name: cards step 2
    api: api/cards.yml
    variables:
        token: "3ec6f550e7df762d0d7f5897fb2abe0931bc8319"
    validate:
        - eq: ["status_code", 201]

变量优先级按以下顺序排列:

存在extract提取变量值的,优先取提取中参数content.token

在无extract时,优先取config变量

在无config变量时,优先取用例中变量

以上均无时,取api变量

  

 

  

 

 

 

  

 

posted @ 2021-06-04 11:31  莫使娇躯空对月  阅读(220)  评论(0编辑  收藏  举报