AirtestProject 趁热打铁!一起来看下 Airtest1.2.7 新增的那些断言 API

fishfish-yu · 2022年10月25日 · 4053 次阅读

此文章来源于项目官方公众号:“AirtestProject”
版权声明:允许转载,但转载必须保留原链接;请勿用作商业或者非法用途

1. 前言

先前我们放出了 1.2.7 版本的 Airtest,其中,一个很重要的功能是,我们 新增了非常丰富的断言 API ,今天我们就来详细看一下新版 Airtest 都有给我们提供哪些断言语句。

2. 旧版 Airtest 提供的断言语句

先回顾下,旧版 Airtest 一直以来,都只给我们提供了 2 种断言语句,一种是断言目标存在/不存在当前页面:

  • assert_exists
  • assert_not_exists

另一种是断言 2 个值相等/不相等:

  • assert_equal
  • assert_not_equal
1)断言目标存在/不存在当前画面
assert_exists(Template(r"tpl1665570124249.png", record_pos=(0.118, -0.026), resolution=(720, 1440)), "请填写测试点")

assert_not_exists(Template(r"tpl1665570165989.png", record_pos=(0.118, -0.032), resolution=(720, 1440)), "请填写测试点")

2)断言 2 个值相等/不相等
assert_equal(poco("score").get_text(), "100", "分数为100分")

assert_not_equal(poco("score").get_text(), "0", "分数不为0")

3. 新版 Airtest 新增的断言语句

而 Airtest1.2.7 版本,又给我们新增了 14 个断言的 API,包含断言表达式为 True 或者 False(bool)、断言表达式为空/不为空、断言 2 个值的大小情况等:

1)断言表达式为 True/False(bool)
from airtest.core.assertions import *

# 断言表达式为True
assert_true(1==1, msg="assert 1==1")

# 断言表达式为False
assert_false(1==2, msg="assert 1!=2")
2)断言 2 个对象相同/不相同
from airtest.core.assertions import *

# 断言2个对象相同
assert_is(1, 1, msg="assert 1 is 1")

# 断言2个对象不相同
assert_is_not(1, 2, msg="assert 1 is not 2")
3)断言表达式为 None/不为 None
from airtest.core.assertions import *

# 断言表达式为None
assert_is_none(None, msg="assert None is None")

# 断言表达式不为None
assert_is_not_none(1, msg="assert 1 is not None")
4)断言第一个参数是否在第二个参数中(包含关系)
from airtest.core.assertions import *

# 断言第一个参数在第二个参数中
assert_in(1, [1, 2], msg="assert 1 in [1, 2]")

# 断言第一个参数不在第二个参数中
assert_not_in(3, [1, 2], msg="assert 3 not in [1, 2]")
5)断言对象是不是某种类型的实例
from airtest.core.assertions import *

# 断言对象是某种类型的实例
assert_is_instance(1, int, msg="assert 1 is int")

# 断言对象不是某种类型的实例
assert_not_is_instance(1, str, msg="assert 1 is not str")

这个断言语句中,第一个参数为obj,是一个具体的对象实例,第二个参数为cls,是一种类型,我们可以用这个断言来判断某个实例是不是属于某种类型的。

不过这个断言,在 AirtestIDE 中执行会报一个错误,我们会在下个版本修复这个问题:

TypeError: can't pickle mappingproxy objects
6)断言第一个值大于/大于等于第二个值
from airtest.core.assertions import *

# 断言第一个值大于第二个值
assert_greater(2, 1, msg="assert 2 > 1")

# 断言第一个值大于等于第二个值
assert_greater_equal(1, 1, msg="assert 1 >= 1")
7)断言第一个值小于/小于等于第二个值
from airtest.core.assertions import *

# 断言第一个值小于第二个值
assert_less(1, 2, msg="assert 1 < 2")

# 断言第一个值小于等于第二个值
assert_less_equal(1, 1, msg="assert 1 <= 1")

4. 拓展:Airtest 断言的msg参数说明

可以看到,所有 Airtest 的断言语句中,都包含msg参数,这个参数是为了方便我们给当前的断言语句增加一个说明,并且该说明会显示在 Airtest 报告,断言步骤的描述上:

5. 拓展:Airtest 断言的snapshot参数说明

从 Airtest1.2.7 版本起,断言还新增了一个snapshot的参数,为了支持同学们在设置断言时,还能附带截取当前画面的图片,然后显示在 Airtest 报告中。

当然如果我们不需要断言截图的话,也可以设置关闭断言的截图:

# 默认情况下,断言截图会开启
assert_is_not_none("1", msg="assert '1' is not None")

# 如不需要断言时截取当前画面,则可以设置关闭断言的截图
assert_is_not_none("1", msg="assert '1' is not None",snapshot=False)

1)assert_exists关闭截图的特殊说明

比较特别的是,assert_exists 默认也是带截图的,但是要设置这个步骤不截图,不能使用 snapshot=False 来设置,而是要通过 Airtest 的全局设置来控制:

ST.SAVE_IMAGE = False

assert_exists(Template(r"tpl1665719197992.png", "请填写测试点"))

assert_not_exists 也是同理。如果给assert_exists强行传入snapshot=False,则会报错:

TypeError: assert_exists() got an unexpected keyword argument 'snapshot'

6. 小结

今天我们主要介绍了旧版 Airtest 的断言,以及 1.2.7 版本 Airtest 新增的断言类型,还拓展了 Airtest 断言的相关参数说明。更多关于 Airtest 新版的内容,欢迎同学们持续关注我们后续的推文。


Airtest 官网https://airtest.netease.com/
Airtest 教程官网https://airtest.doc.io.netease.com/
搭建企业私有云服务https://airlab.163.com/b2b

官方答疑 Q 群:117973773

呀~这么认真都看到这里啦,帮忙点击左下角的爱心,给我点个赞支持一下把,灰常感谢~

暫無回覆。
需要 登录 後方可回應,如果你還沒有帳號按這裡 注册