pytest + yaml 框架 -47.parameters参数化支持笛卡尔积

发布时间 2023-07-06 08:56:51作者: 上海-悠悠

前言

v1.3.8 版本对 parameters 参数化格式重新做了定义,支持笛卡尔积了。当然以前旧版本的格式还是继续兼容。

parameters 参数化

新版本对 parameters 参数化重新做了定义,简化了步骤,更加清晰简洁.

1.只有一个变量需要参数化的情况

test_p1.yml

config:
  parameters:
    x: ["a", "b", "c"]


test_a:
  print: "输出-${x}"

运行结果

test_p1.yml 输出-a
.输出-b
.输出-c
.

2.有2个变量需要参数化的情况,变量中间用逗号隔开(或者-)
test_p2.yml

config:
  parameters:
    x,y: [["a", "b"], ['c', 'd']]

test_b:
  print: "输出-${x}-${y}"

也可以实现横线隔开

config:
  parameters:
    x-y: [["a", "b"], ['c', 'd']]

运行结果

test_p2.yml 输出-a-b
.输出-c-d
.

笛卡尔积

对2个变量同时参数化,生成笛卡尔积的情况
x 变量只有一个值,可以写成x: "a", 也可以写成x: ["a"]
test_p3.yml

config:
  parameters:
    x: "a"
    y: ["hello", "world"]


test_r:
  name: 11
  print: "组合-${x}-${y}"

x 变量也可以有多个值
test_p4.yml

config:
  parameters:
    x: ["a", 'b']
    y: ["hello", "world"]


test_r:
  name: 11
  print: "组合-${x}-${y}"

运行结果

test_p4.yml 组合-a-hello
.组合-a-world
.组合-b-hello
.组合-b-world
.

模块级别和用例级别参数化

支持模块级别和用例级别参数化

  • config 中 parameters 参数化,作用域是整个模块级别
  • 用例中 parameters 参数化,作用域只针对单个用例

用例级别参数化
test_p5.yml

config:
   name: demo

test_1:
  name: 用例1
  parameters:
    user: [hello, world]
  print: "case-${user}"

运行结果

test_p5.yml case-hello
.case-world
.

config 中 parameters 参数化,作用域是整个模块级别, test_p6.yml 文件示例

config:
  parameters:
    x: ["a", 'b']
    y: ["hello", "world"]


test_a:
  print: "组合-${x}-${y}"

test_b:
  print: "组合-${x}-${y}"

运行结果

test_p6.yml 组合-a-hello
.组合-a-hello
.组合-a-world
.组合-a-world
.组合-b-hello
.组合-b-hello
.组合-b-world
.组合-b-world
.