变量的注册和使用

如果用例内容(如: 测试账号、对象id 等)经常需要更新、变化,那么对用例文件的修改就是一个稍显麻烦的问题。

特别是内容比较繁多的用例文件,寻找内容很麻烦,修改起来更麻烦。

在实践中,一个好的思路是将【会变化的内容】放到变量中,那么在需要更新时只 集中修改变量 即可,避免了从众多文件中进行搜寻,然后小心翼翼的修改的繁琐操作。

pytest-yaml-sanmu支持3种变量的设置方式:

  1. 局部变量:仅限于当前用例

  2. 共享变量:被多个用例使用

  3. 全局变量:被所有用例使用

设置局部变量

局部变量在yaml用例的 vars 字段下进行创建

# test_vars_locals.yaml

name: 使用局部变量
vars:  # 创建局部遍历
  var_a: 1
  var_b: 2

steps:
  - request:
      method: get
      url: https://www.baidu.com/?a=${var_a}&b=${var_b}

执行结果如下:

(.venv) C:\demo\pytest-yaml-demo>pytest -vs
============================= test session starts ==============================
platform win32 -- Python 3.12.0, pytest-8.2.2, pluggy-1.5.0
cachedir: .pytest_cache
rootdir: C:\demo\pytest-yaml-demo
configfile: pytest.ini
plugins: yaml-1.1.0
collected 1 item

tests/variable/test_vars_locals.yaml::使用局部变量 url=https://www.baidu.com/?a=1&b=2
PASSED

============================== 1 passed in 0.15s ===============================

设置共享变量

共享变量也写在yaml用例的 vars 字段下,但不是直接写,而是通过 include 方式加载数据文件。

因为数据文件可以被多个用例 include ,所以其变量值也被多个用例共享,但其本质还是局部变量。

首先创建一个JSON或YAML数据文件文件:

{
  "var_a": "s1_with_json",
  "var_b": "s2_with_json"
}

接着,在用例中通过 !include 加载该文件:

# test_vars_shared.yaml

name: 使用共享变量
vars: !include shared_variable.json # 支持JSON文件和yaml文件

steps:
  - request:
      method: get
      url: https://www.baidu.com/?a=${var_a}&b=${var_b}

执行结果如下:

(.venv) C:\demo\pytest-yaml-demo>pytest -vs
============================= test session starts ==============================
platform win32 -- Python 3.12.0, pytest-8.2.2, pluggy-1.5.0
cachedir: .pytest_cache
rootdir: C:\demo\pytest-yaml-demo
configfile: pytest.ini
plugins: yaml-1.1.0
collected 1 item

tests/variable/test_vars_shared.yaml::使用共享变量 url=https://www.baidu.com/?a=s1_with_json&b=s2_with_json
PASSED

============================== 1 passed in 0.23s ===============================

设置全局变量

全局变量和共享变量不一样的是:全局变量会被所有用例使用,

所以不需要用例文件中进行设置

# test_vars_globals.yaml

name: 使用全局变量

steps:
  - request:
      method: get
      url: https://www.baidu.com/?a=${var_c1}&b=${var_d2}

而是在配置文件中进行设置全局变量文件

[pytest]
; 执行全局变量文件
yaml_global_variable_paths =
    global_variable.yaml

全局变量文件内容如下

# global_variable.yaml
var_c1: g1
var_d2: g2

警告

如果全局变量文件不存在,会引发错误。通过配置,可以忽略此类错误。

执行结果如下

(.venv) C:\demo\pytest-yaml-demo>pytest -vs
============================= test session starts ==============================
platform win32 -- Python 3.12.0, pytest-8.2.2, pluggy-1.5.0
cachedir: .pytest_cache
rootdir: C:\demo\pytest-yaml-demo
configfile: pytest.ini
plugins: yaml-1.1.0
collected 1 item

tests/variable/test_vars_globals.yaml::使用全局变量 url=https://www.baidu.com/?a=g1&b=g2&c=
PASSED

============================== 1 passed in 0.19s ===============================

优先级和作用域

优先级

目前,在yaml中通过使 ${var_name} 的方式使用变量,这一用法和JMeter相似。

在前文中,fixture返回值的使用方式也是 ${var_name} , 那么,当变量名、fixture名相同时当多种变量具有变量名,优先使用哪一个?

在pytest-yaml-sanmu中,对于同名变量,会按照以下顺序返回值:

  1. fixture

  2. 局部变量

  3. 全局变量

警告

如果某个变量不存在,则返回空字符串(这一点和JMeter是不同的)

也就是说, fixture的优先级最高,全局变量的优先级最低

作用域

作用域决定了变量可以被哪些用例使用,具体区别如下:

  1. 局部变量:仅限于当前用例

  2. 共享变量:被多个用例使用

  3. 全局变量:被所有用例使用

这使得我们可以放心的地在不同yaml用中使用相同的 局部变量 名,而不担心产生额外影响和冲突。

相反,如果需要创建(或修改)一个可以被其他yaml用例使用的数据,则应该使用 全局变量