python考试pass or fail_python-pytest学习(十二)-标记失败xfail
一、前言
當用例a失敗的時候,如果用例b和用例c都是依賴于第一個用例的結果,那可以直接跳過用例b和c的測試,直接給他標記失敗xfail
用到的場景,登錄是第一個用例,登錄之后的操作b是第二個用例,登錄之后操作c是第三個用例,很明顯三個用例都會用到登錄操作。
例,很明顯三個用例都會用到登錄操作。
如果登錄都失敗了,那后面2個用例就沒測試必要了,直接跳過,并且標記為失敗用例,這樣可以節省用例時間。
二、用例設計
pytest里面用xfail標記用例未失敗的用例,可以直接跳過。實現基本思路:
1.把登錄寫為前置操作;
2.對登錄的用戶和密碼參數化,參數用canshu = [{"user":"admin","pws":"111"}]表示;
3.多個用例放到一個Test_xx的class里;
4.test_01,test_02,test_03全部調用fixture里面的login功能;
5.test_01測試登錄用例
6.test_02和test_03執行前用if判斷登錄的結果,登錄失敗就執行,pytest.xfail("登錄不成功,標記為xfail")
importpytest
data= [{"user":"admin","psw":"111"},{"user":"","psw":""}]
@pytest.fixture(scope="module")deflogin(request):
user= request.param["user"]
psw= request.param["psw"]print("正在操作登錄,賬號:%s,密碼:%s"%(user,psw))ifpsw:returnTrueelse:returnFalse
@pytest.mark.parametrize("login",data,indirect=True)classTest_xx():deftest_01(self,login):"""用例1登錄"""result=loginprint("用例1:%s"%result)assert result ==Truedeftest_02(self,login):
result=loginprint("用例2:%s"%result)if notresult:
pytest.xfail("登錄不成功,標記為xfail")assert 1==1
deftest_03(self,login):
result=loginprint("用例3:%s"%result)if notresult:
pytest.xfail("登錄不成功,標記為xfail")if __name__=="__main__":
pytest.main(["-s","test_05.py"])
運行結果:
F用例1:False
test_05.py:16(Test_xx.test_01[login1])
False!=True
Expected :True
Actual :Falseself= login=Falsedeftest_01(self,login):"""用例1登錄"""result=loginprint("用例1:%s"%result)> assert result ==True
Eassert False ==True
test_05.py:21: AssertionError
x用例2:False
self= login=Falsedeftest_02(self,login):
result=loginprint("用例2:%s"%result)if notresult:> pytest.xfail("登錄不成功,標記為xfail")
E _pytest.outcomes.XFailed: 登錄不成功,標記為xfail
test_05.py:27: XFailed
x用例3:False
self= login=Falsedeftest_03(self,login):
result=loginprint("用例3:%s"%result)if notresult:> pytest.xfail("登錄不成功,標記為xfail")
E _pytest.outcomes.XFailed: 登錄不成功,標記為xfail
test_05.py:34: XFailed
[100%]
上面傳的登錄參數是登錄成功的案例,三個用例全部通過;第二組參數登錄失敗,第一個用例就失敗了,用例2和3都沒執行,直接標記為xfail。
總結
以上是生活随笔為你收集整理的python考试pass or fail_python-pytest学习(十二)-标记失败xfail的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: pygame做的著名游戏_pygame教
- 下一篇: 通话过程中显示呼叫失败_电销行业如何破局