pytest(十五)--用例a失敗,跳過測試用例b和c並標記失敗xfail
阿新 • • 發佈:2020-07-30
前言
當用例a失敗的時候,如果用例b和用例c都是依賴於第一個用例的結果,那可以直接跳過用例b和c的測試,直接給他標記失敗xfail
用到的場景,登入時第一個用例,登入之後的操作b是第二個用例,登入之後操作c是第三個用例,很明顯三個用例都會走到登入。
如果登入失敗了,那後面2個用例就沒有必要了,直接跳過,並且標記為失敗用例,這樣可以節省用例時間。
用例設計
1.pytest裡面用xfail標記用例為失敗的用例,可以直接跳過。實現基本思路
把登入寫為前置操作
對登入的賬號和密碼引數化,引數用data=[{"user":"admin","pwd":"123456"}]表示
多個用例放到一個Test_xx的class裡
test_1,test_2,test_3全部呼叫fixture裡面的login功能
test_1測試登入用例
test_2和test_3執行前用if判斷登入的結果,登入失敗就執行,pytest.xfail("登入不成功,標記為xfail")
#test_fix1.py # coding:utf-8 import pytest data=[{"user":"admin","pwd":"123456"}] @pytest.fixture(scope="module") def login(request): user=request.param["user"] pwd=request.param["pwd"] print("正在操作登入,賬號:{},密碼:{}".format(user,pwd)) if pwd: return True else: return False @pytest.mark.parametrize("login",data,indirect=True) class Test__xx: def test_1(self,login): r=login print("用例1,登入結果:{}".format(r)) assert r==True def test_2(self,login): r=login print("用例2,登入結果:{}".format(r)) if not r: pytest.xfail("登入不成功,標記為xfail") def test_3(self,login): r=login print("用例3,登入結果:{}".format(r)) if not r: pytest.xfail("登入不成功,標記為xfail") assert 1 == 1 if __name__=="__main__": pytest.main(["-s","test_fix1.py"])
三個用例全部通過
標記為xfail
1.下面是登入失敗情況的用例,修改data資料
#test_fix1.py # coding:utf-8 import pytest data=[{"user":"admin","pwd":""}] @pytest.fixture(scope="module") def login(request): user=request.param["user"] pwd=request.param["pwd"] print("正在操作登入,賬號:{},密碼:{}".format(user,pwd)) if pwd: return True else: return False @pytest.mark.parametrize("login",data,indirect=True) class Test__xx: def test_1(self,login): r=login print("用例1,登入結果:{}".format(r)) assert r==True def test_2(self,login): r=login print("用例2,登入結果:{}".format(r)) if not r: pytest.xfail("登入不成功,標記為xfail") def test_3(self,login): r=login print("用例3,登入結果:{}".format(r)) if not r: pytest.xfail("登入不成功,標記為xfail") assert 1 == 1 if __name__=="__main__": pytest.main(["-s","test_fix1.py"])
執行結果
test_fix1.py 正在操作登入,賬號:admin,密碼: F用例1,登入結果:False test_fix1.py:15 (Test__xx.test_1[login0]) False != True Expected :True Actual :False <Click to see difference> self = <test_fix1.Test__xx object at 0x00000214306097B8>, login = False def test_1(self,login): r=login print("用例1,登入結果:{}".format(r)) > assert r==True E assert False == True test_fix1.py:19: AssertionError x用例2,登入結果:False self = <test_fix1.Test__xx object at 0x0000021430603DD8>, login = False def test_2(self,login): r=login print("用例2,登入結果:{}".format(r)) if not r: > pytest.xfail("登入不成功,標記為xfail") E _pytest.outcomes.XFailed: 登入不成功,標記為xfail test_fix1.py:24: XFailed x用例3,登入結果:False self = <test_fix1.Test__xx object at 0x0000021430609710>, login = False def test_3(self,login): r=login print("用例3,登入結果:{}".format(r)) if not r: > pytest.xfail("登入不成功,標記為xfail") E _pytest.outcomes.XFailed: 登入不成功,標記為xfail test_fix1.py:29: XFailed [100%] ================================== FAILURES =================================== ___________________________ Test__xx.test_1[login0] ___________________________ self = <test_fix1.Test__xx object at 0x00000214306097B8>, login = False def test_1(self,login): r=login print("用例1,登入結果:{}".format(r)) > assert r==True E assert False == True test_fix1.py:19: AssertionError ---------------------------- Captured stdout setup ---------------------------- 正在操作登入,賬號:admin,密碼: ---------------------------- Captured stdout call ----------------------------- 用例1,登入結果:False =========================== short test summary info =========================== FAILED test_fix1.py::Test__xx::test_1[login0] - assert False == True ======================== 1 failed, 2 xfailed in 0.04s =========================