前言
執行自動化用例的過程中,遇到已知bug或者其他平台的執行內容,這個時候我們可以選擇用跳過用例,在unittest中有無條件跳過,也有滿足條件進行跳過,那么pytest中也存在跳過用例。
skip
前面介紹了mark的時候,知道mark屬於標記用例,那么當mark.skip的時候就表示無條件進行跳過用例
import pytest class Test01(): @pytest.mark.skip() def test_01(self): print('---用例01---') def test_02(self): print('---用例02---') @pytest.mark.skip() def test_03(self): print('---用例03---') if __name__ == '__main__': pytest.main(['-vs'])
通過下圖可以發現,我們執行的用例1和用例2均被跳過了。
reason參數
執行跳過用例過程中,可以進行加入reason參數進行對跳過的用例進行補充說明詳細信息,以及跳過的原因內容
import pytest data = [{'user': "anjing", 'pwd': "123",}, {'user': "admin", "pwd": "123"},] @pytest.fixture(params=data) def login(request): yield request.param class Test01(): @pytest.mark.skip(reason='該用例存在bug,請跳過!') def test_01(self): print('---用例01---') def test_02(self): print('---用例02---') @pytest.mark.skip(reason='該用例還沒有編寫完成,請跳過') def test_03(self): print('---用例03---') if __name__ == '__main__': pytest.main(['-vs'])
通過下圖可以看出來,用例1和用例3進行了跳過,並補充了詳細的說明
函數中判斷是否跳過
舉個例子,如果我們測試后台過程中,不同的賬號登錄,會有不同的權限,如果其中一個用戶沒有改權限測試此功能,也讓跳過,這個時候我們就會通過pytest.skip的方法進行判斷進行跳過
import pytest data = [{'user': "anjing", 'pwd': "123",}, {'user': "admin", "pwd": "123"},] @pytest.fixture(params=data) def login(request): yield request.param class Test01(): def test_01(self,login): if login['user']=='anjing' and login['pwd']=='123': print('用戶:%s登錄成功'%login['user']) print('執行用例01') else: pytest.skip("用戶:%s沒有權限進行操作xx功能"%login['user']) def test_02(self): print('---用例02---') def test_03(self): print('---用例03---') if __name__ == '__main__': pytest.main(['-vs'])
通過執行結果發現,參數admin沒有通過,然后進行了跳過。當然用例之間的判斷不僅僅這樣,只是簡單的舉個例子,具體的結合實際項目為准
skipif
skipif也是屬於mark中的一個方法,也是跳過用例的內容,支持條件跳過,比如該用例只能在Windows上執行。 @pytest.mark.skipif(condition, reason='跳過的原因!')
其中condition表示跳過的條件,為True則跳過,reason和上述一樣,表示跳過的自定義條件
import pytest import sys class Test01(): @pytest.mark.skipif(sys.platform=='win32', reason='在linux上運行,跳過Windows') def test_01(self): print('---用例01---') def test_02(self): print('---用例02---') def test_03(self): print('---用例03---') if __name__ == '__main__': pytest.main(['-vs'])
通過下圖可以發現,安靜在Windows上運行代碼時,自動跳過了。注意:skipif必須要帶上跳過的詳細信息,要不然會報錯
整個類自動跳過
skip和skipif不僅僅能夠函數上進行添加裝飾器,還可以直接在類上進行使用,直接跳過整個類。
import pytest import sys @pytest.mark.skipif(sys.platform=='win32', reason='在linux上運行,跳過Windows') class Test01(): def test_01(self): print('---用例01---') def test_02(self): print('---用例02---') def test_03(self): print('---用例03---') if __name__ == '__main__': pytest.main(['-vs'])
先來個圖為skipif在類上進行跳過
skip跳過類
import pytest @pytest.mark.skip(reason='改功能還未實現,請跳過用例') class Test01(): def test_01(self): print('---用例01---') def test_02(self): print('---用例02---') def test_03(self): print('---用例03---') if __name__ == '__main__': pytest.main(['-vs'])
xfail
在執行自動化的時候,會有這種情況,知道該功能存在穩定性問題,不想要執行,但是也不想要跳過。這個時候我們就可以使用xfail,xfail表示測試期望是失敗的,不會影響測試用例的執行,如果執行成功則報xpass,如果失敗就會報xfail。
import pytest class Test01: @pytest.mark.xfail() def test_01(self): print('---用例01---,該功能暫未實現!') def test_02(self): print('---用例02---') @pytest.mark.xfail() def test_03(self): print('---用例03---,預期用例失敗!') assert 1==2 if __name__ == '__main__': pytest.main(['-vs'])
通過執行結果可以看出來,我們通過xfail標記的用例,已經全部執行,執行成功的顯示xpass,失敗的顯示xfail,執行失敗也沒有繼續報錯。
另一種寫法:
有的小伙伴們覺得,在用例前標記比較麻煩,能在用例中進行標記嗎?當然是可以的。
import pytest class Test01: def test_01(self): pytest.xfail(reason='該功能暫時未實現!') print('---用例01---') def test_02(self): print('---用例02---') def test_03(self): print('---用例03---,預期用例失敗!') pytest.xfail(reason='該功能存在bug') assert 1==2 if __name__ == '__main__': pytest.main(['-vs'])
通過執行結果可以看到,test_01和test_03已經被預計失敗了,細心的朋友會發現,pytest.xfail在用例中標記的時候,代碼執行完這句話后就立即退出該用例,繼續執行下一條用例。
簡單的介紹了pytest的跳過用例的方法,看了第一遍可能會覺得和unittest差不多,但是多看幾遍,隨便寫的比較少,但是實用性很好,具體怎么用,就看大家在項目中怎么設計了。