pytest-order

pytest-order 是一个 pytest 插件, 可以通过order标记,对测试用例的执行顺序进行非常灵活的编排。

使用数字排序

# test_order_index.yaml

name: 第3个用例
mark:
  - order: -2
steps:
  - request:
      method: get
      url: https://www.baidu.com/

---
name: 第4个用例
mark:
  - order:
      index: -1
steps:
  - request:
      method: get
      url: https://www.baidu.com/

---
name: 第2个用例
mark:
  - order:
      index: 2
steps:
  - request:
      method: get
      url: https://www.baidu.com/

---
name: 第1个用例
mark:
  - order: 1
steps:
  - request:
      method: get
      url: https://www.baidu.com/

执行结果:

(.venv) C:\demo\pytest-yaml-demo>pytest -v
============================= 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: order-1.3.0, yaml-1.1.1
collected 4 items

tests/plugins/test_order_index.yaml::第1个用例 PASSED                       [ 25%]
tests/plugins/test_order_index.yaml::第2个用例 PASSED                       [ 50%]
tests/plugins/test_order_index.yaml::第3个用例 PASSED                       [ 75%]
tests/plugins/test_order_index.yaml::第4个用例 PASSED                       [100%]

============================== 4 passed in 0.03s ===============================

使用相对排序

# test_order_relative.yaml

name: 第3个用例
mark:
  - order:
      after: 第2个用例

steps:
  - request:
      method: get
      url: https://www.baidu.com/

---
name: 第2个用例

steps:
  - request:
      method: get
      url: https://www.baidu.com/
---
name: 第1个用例
mark:
  - order:
      before: 第2个用例

steps:
  - request:
      method: get
      url: https://www.baidu.com/

执行结果:

(.venv) C:\demo\pytest-yaml-demo>pytest -v
============================= 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: order-1.3.0, yaml-1.1.1
collected 3 items

tests/plugins/test_order_relative.yaml::第1个用例 PASSED                    [ 33%]
tests/plugins/test_order_relative.yaml::第2个用例 PASSED                    [ 66%]
tests/plugins/test_order_relative.yaml::第3个用例 PASSED                    [100%]

============================== 3 passed in 0.04s ===============================

多个排序标记

pytest-order 插件允许为测试用例设置多个order标记,这时用例将按照定义的顺序执行多次。

pytest-yaml-sanmu 对此特性提供了兼容。

# test_order_multi.yaml

name: 第1和第3
mark:
  - order: 1
  - order: -1

steps:
  - request:
      method: get
      url: https://www.baidu.com/

---
name: 第2个用例
mark:
  - order: 2

steps:
  - request:
      method: get
      url: https://www.baidu.com/

执行结果:

(.venv) C:\demo\pytest-yaml-demo>pytest -v
============================= 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: order-1.3.0, yaml-1.1.1
collected 3 items

tests/plugins/test_order_multi.yaml::第1和第3[index=1] PASSED               [ 33%]
tests/plugins/test_order_multi.yaml::第2个用例 PASSED                       [ 66%]
tests/plugins/test_order_multi.yaml::第1和第3[index=-1] PASSED              [100%]

============================== 3 passed in 0.03s ===============================