pytest + yaml 框架 -69.新增depend 关键字,导入其它yaml用例

93 篇文章 62 订阅

前言

有小伙伴提到,test_a.yml 中已经写了几个流程,test_b.yml 中希望能接着test_a.yml去写用例,于是就需要先导入test_a.yml用例。
为了满足此需求,v1.6.3版本 在config 中新增 depend 关键字。

需求场景

test_a.yml 中已经写了一些用例

config:
  variables:
    x: hello
    y: world


test_x1:
  name: xx
  request:
    method: post
    url: http://httpbin.org/post
    json:
      title: aa
      message: bbb

test_x2:
  name: xx
  print: ${x} -- ${y}

test_b.yml 中可以在config 中添加depend 关键字, test_a.yml和test_a.yml 在同一个目录

config:
  depend: test_a.yml

test_b3:
  name: xx
  print: 输出结果--${y}

那么test_b.yml 就等价于把test_a.yml内容复制过来了

config:
  variables:
    x: hello
    y: world


test_x1:
  name: xx
  request:
    method: post
    url: http://httpbin.org/post
    json:
      title: aa
      message: bbb

test_x2:
  name: xx
  print: ${x} -- ${y}

test_b3:
  name: xx
  print: 输出结果--${y}

所以会重复执行一次test_a.yml用例,再执行test_b.yml 中的用例。

depend 关键字使用

depend 关键字使用规则:

  • 1.只能在 config 中使用,因为它是接着前面一个 yml 用例继续写其他用例。
  • 2.目前只支持一个层级的引用如:b 引入a, 不支持多层级引入,如:c引入b, b又引入了a
  • 3.depend 后面写用例文件的路径,如果在同一目录,可以直接写文件名称,不在同一个目录,需从项目根目录开始,写相对路径。

当 test_a.yml 用例中有在config 定义变量 variables

config:
  variables:
    x: hello
    y: world


test_x1:
  name: xx
  request:
    method: post
    url: http://httpbin.org/post
    json:
      title: aa
      message: bbb

test_x2:
  name: xx
  print: ${x} -- ${y}

test_b.yml 中没有定义config变量时,会默认读取test_a.yml 用例中定义的变量。

config:
  depend: test_a.yml

test_b3:
  name: xx
  print: 输出结果--${y}

test_b.yml 中也可以定义config 变量,替换test_a.yml中的变量

config:
  depend: test_a.yml
  variables:
      y: aabcc

test_b3:
  name: xx
  print: 输出结果--${y}

此时 test_b3 用例引用变量${y} 得到的结果就是 aabbcc

当用例中有局部变量时,用例中定义的变量优先级是最高的

config:
  depend: test_a.yml
  variables:
      y: aabcc

test_b3:
  name: xx
  variables:
    y: aa1122
  print: 输出结果--${y}

此时 test_b3 用例引用变量${y} 得到的结果就是 aa1122

  • 8
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值