趁熱打鐵!一起來看下Airtest1.2.7新增的那些斷言API

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

呀,這麼認真都看到這裏啦,幫忙點個推薦支持一下唄,灰常感謝~

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章