日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

python考试pass or fail_python-pytest学习(十二)-标记失败xfail

發(fā)布時間:2025/3/21 44 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python考试pass or fail_python-pytest学习(十二)-标记失败xfail 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

一、前言

當用例a失敗的時候,如果用例b和用例c都是依賴于第一個用例的結果,那可以直接跳過用例b和c的測試,直接給他標記失敗xfail

用到的場景,登錄是第一個用例,登錄之后的操作b是第二個用例,登錄之后操作c是第三個用例,很明顯三個用例都會用到登錄操作。

例,很明顯三個用例都會用到登錄操作。

如果登錄都失敗了,那后面2個用例就沒測試必要了,直接跳過,并且標記為失敗用例,這樣可以節(jié)省用例時間。

二、用例設計

pytest里面用xfail標記用例未失敗的用例,可以直接跳過。實現(xiàn)基本思路:

1.把登錄寫為前置操作;

2.對登錄的用戶和密碼參數(shù)化,參數(shù)用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執(zhí)行前用if判斷登錄的結果,登錄失敗就執(zhí)行,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%]

上面?zhèn)鞯牡卿泤?shù)是登錄成功的案例,三個用例全部通過;第二組參數(shù)登錄失敗,第一個用例就失敗了,用例2和3都沒執(zhí)行,直接標記為xfail。

總結

以上是生活随笔為你收集整理的python考试pass or fail_python-pytest学习(十二)-标记失败xfail的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。