用例标记和筛选

pytest-yaml-sanmu插件为yaml用例文件提供了 @pytest.mark 的支持。

注册标记

用来筛选用例的自定义的标记需要先进行注册

# pytest.ini

[pytest]
markers =
    api:接口测试
    web:Web测试
    app

在上面的例子中,一共创建了3个标记:

  • api

  • web

  • app

其中api和web标记提供了标记说明,app没有提供,这是因为标记描述是 选填 ,可以省略。

标记注册之后会成为pytest的一部分

(.venv) >pytest --markers

@pytest.mark.api:接口测试

@pytest.mark.web:Web测试

@pytest.mark.app:

...

如果执行pytest命令能够看到你的标记,说明就注册好了,接下来可以在用例中使用标记。

使用标记

使用标记时,在 mark 字段下添加标记名称即可

# test_mark_user.yaml

name: 拥有标记api和web
mark: # 在mark下添加标记
  - api
  - web
steps:
  - request: # 步骤 1:发送请求
      method: get
      url: https://www.baidu.com

备注

  • 每个用例可以拥有多个不同的标记

  • 每个标记也可以添加给不同的用例

下面的例子中,文件包含多个用例,用例里包含多个标记,标记用于多个用例

# test_mark_user.yaml

name: 拥有标记api和web
mark: # 在mark下添加标记
  - api
  - web
steps:
  - request: # 步骤 1:发送请求
      method: get
      url: https://www.baidu.com

---
name: 拥有标记api和app
mark:
  - api
  - app
steps:
  - request:
      method: get
      url: https://www.baidu.com

筛选用例

给用例添加好标记后,启动pytest时传递 -m <标记名> 即可筛选用例,只有执行拥有指定标记的用例。

例如,传递 web 标记之后,只会有一个用例执行

(.venv) C:\demo\pytest-yaml-demo>pytest -vs -m web
============================= test session starts ==============================
platform win32 -- Python 3.12.0, pytest-8.3.4, pluggy-1.5.0
cachedir: .pytest_cache
rootdir: C:\demo\pytest-yaml-demo
configfile: pytest.ini
plugins: yaml-1.1.0
collected 2 items / 1 deselected / 1 selected

tests/mark/test_mark_user.yaml::拥有标记api和web PASSED

======================= 1 passed, 1 deselected in 0.01s ========================

传递 api 标记之后,会有两个用例执行

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

tests/mark/test_mark_user.yaml::拥有标记api和web PASSED
tests/mark/test_mark_user.yaml::拥有标记api和app PASSED

============================== 2 passed in 0.01s ===============================

使用内置标记

pytest提供了大量的内置标记,pytest-yaml-sanmu也是支持的。

下面这个示例中,为用例使用了 skip 标记

# test_mark_skip.yaml

name: 跳过用例
mark:
  - skip # 跳过该用例,不执行
steps:
  - request:
      method: get
      url: https://www.baidu.com

执行结果如下:

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

tests/mark/test_mark_skip.yaml::跳过用例 SKIPPED (unconditional skip)

============================= 1 skipped in 0.02s ===============================

为标记传递参数

如果标记需要参数,也可以为标记传递参数,这里分为几种不同情况:

  • 只有一个参数,直接传递参数:

    mark: arg
    
  • 有多个位置参数,传递数组(列表):

    mark:
      - 1
      - 2
      - 3
    
  • 有多个关键字参数,传递对象(字典):

    mark:
      a: 1
      b: 2
      c: 3
    
  • 位置参数+关键字参数: 受限于yaml格式,暂不支持

下面这个示例中,为用例使用了 skipif 标记,并只传递了一个参数

# test_mark_skipif.yaml

name: 跳过条件为真
mark:
  - skipif: 1==1 # 跳过该用例,不执行
steps:
  - request:
      method: get
      url: https://www.baidu.com

---
name: 跳过条件为假
mark:
  - skipif: 1==2 # 条件为假,不跳过
steps:
  - request:
      method: get
      url: https://www.baidu.com

执行结果如下:

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

tests/mark/test_mark_skipif.yaml::跳过条件为真 SKIPPED (condition: 1==1)
tests/mark/test_mark_skipif.yaml::跳过条件为假 PASSED
========================== 1 passed, 1 skipped in 0.03s ========================