python+appium+pytest自動(dòng)化測(cè)試-跳過(guò)測(cè)試與預(yù)期失敗的測(cè)試

來(lái)自APP Android端自動(dòng)化測(cè)試初學(xué)者的筆記,寫(xiě)的不對(duì)的地方大家多多指教哦。

在我們自動(dòng)化測(cè)試過(guò)程中,經(jīng)常會(huì)遇到功能阻塞、功能未實(shí)現(xiàn)、環(huán)境等一系列外部因素問(wèn)題導(dǎo)致的一些用例執(zhí)行不了,這時(shí)我們就可以用到跳過(guò)skip用例,如果我們注釋掉或刪除掉,后面還要進(jìn)行恢復(fù)操作。

一、跳過(guò)測(cè)試類(lèi)

該方法用于需要跳過(guò)的測(cè)試類(lèi),在測(cè)試類(lèi)前面添加裝飾器:@pytest.mark.skip()。

1.skip():被標(biāo)記的類(lèi)中所有方法測(cè)試用例都會(huì)被跳過(guò)

import pytest

@pytest.mark.skip()
class TestPhoneLogin:
    def test_error_phone_format(self):
        assert 1 == 1

    def test_error_phone_longer(self):
        assert 1 == 1

    def test_error_phone_shorter(self):
        assert 1 == 1

    def test_error_write_verification_code(self):
        assert 1 == 1

執(zhí)行結(jié)果如下圖所示:

image.png

2.skip(reason=''):被標(biāo)記的測(cè)試類(lèi)在執(zhí)行過(guò)程中會(huì)直接跳過(guò),結(jié)果會(huì)顯示reason。

import pytest

@pytest.mark.skip(reason='該類(lèi)不需要執(zhí)行測(cè)試')
class TestPhoneLogin:
    def test_error_phone_format(self):
        assert 1 == 1

    def test_error_phone_longer(self):
        assert 1 == 1

    def test_error_phone_shorter(self):
        assert 1 == 1

    def test_error_write_verification_code(self):
        assert 1 == 1

在執(zhí)行時(shí)使用”pytest -v -rs“,結(jié)果才會(huì)顯示reason。若不添加“-rs“,則只顯示跳過(guò)的數(shù)量(同1),不顯示reason。

image.png

3.skipif(條件, reason=''):被標(biāo)記的測(cè)試類(lèi)在滿足條件的情況下會(huì)跳過(guò),不滿足條件的情況下會(huì)執(zhí)行測(cè)試類(lèi)中的測(cè)試函數(shù)。

a.滿足條件:跳過(guò)測(cè)試類(lèi)中的所有測(cè)試用例。

import pytest

@pytest.mark.skipif(1 == 1, reason='該類(lèi)不需要執(zhí)行測(cè)試')
class TestPhoneLogin:
    def test_error_phone_format(self):
        assert 1 == 1
......

b.不滿足條件:執(zhí)行測(cè)試類(lèi)中的所有測(cè)試用例。測(cè)試結(jié)果同2

import pytest

@pytest.mark.skipif(1 == 2, reason='該類(lèi)不需要執(zhí)行測(cè)試')
class TestPhoneLogin:
    def test_error_phone_format(self):
        assert 1 == 1
......

二、跳過(guò)測(cè)試函數(shù)

該方法用于需要跳過(guò)的測(cè)試用例,在測(cè)試用例前面添加裝飾器:@pytest.mark.skip()。

1.skip():被標(biāo)記的測(cè)試用例函數(shù)在執(zhí)行過(guò)程中會(huì)直接跳過(guò)。

import pytest

class TestPhoneLogin:
    @pytest.mark.skip()
    def test_error_phone_format(self):
        assert 1 == 1
......

結(jié)果顯示:

image.png

2.skip(reason=''):被標(biāo)記的測(cè)試用例函數(shù)在執(zhí)行過(guò)程中會(huì)直接跳過(guò),結(jié)果會(huì)顯示reason,

在執(zhí)行時(shí)使用”pytest -v -rs“,結(jié)果才會(huì)顯示reason。若不添加“-rs“,則只顯示跳過(guò)的數(shù)量(同1),不顯示reason。

import pytest

class TestPhoneLogin:
    @pytest.mark.skip(reason='該類(lèi)不需要執(zhí)行測(cè)試')
    def test_error_phone_format(self):
        assert 1 == 1
......

image.png

3.skipif():被標(biāo)記的測(cè)試函數(shù)在滿足條件的情況下會(huì)跳過(guò),不滿足條件的情況下會(huì)測(cè)試函數(shù)。有條件地跳過(guò)。

a.滿足條件:跳過(guò)測(cè)試類(lèi)中的所有測(cè)試用例。

import pytest

class TestPhoneLogin:
    @pytest.mark.skipif(1 == 1, reason='該類(lèi)不需要執(zhí)行測(cè)試')
    def test_error_phone_format(self):
        assert 1 == 1
......

b.不滿足條件:執(zhí)行測(cè)試類(lèi)中的所有測(cè)試用例。測(cè)試結(jié)果同2

import pytest

class TestPhoneLogin:
    @pytest.mark.skipif(1 == 2, reason='該類(lèi)不需要執(zhí)行測(cè)試')
    def test_error_phone_format(self):
        assert 1 == 1
......

三、skip賦值給變量,可多處

無(wú)論是@pytest.mark.skip()標(biāo)簽還是@pytest.mark.skipif()標(biāo)簽,如果你想在多個(gè)測(cè)試方法上裝飾,依次寫(xiě)起來(lái)很麻煩的話,你可以選擇定義個(gè)變量讓它等于標(biāo)簽,然后在裝飾的時(shí)候用該變量代替標(biāo)簽。這種方法,你還可以通過(guò)在其他模塊中導(dǎo)入的變量的方式,在其他模塊中共享標(biāo)簽。

賦值:myskip=pytest.mark.skipif(1==1,reason='skip賦值給變量,可多處調(diào)用')

調(diào)用:@myskip

import pytest

my_skip = pytest.mark.skipif(1 == 1, reason=**reason='**skip賦值給變量,可多處調(diào)用**'**)
class TestPhoneLogin:
    @my_skip 
    def test_error_phone_format(self):
        assert 1 == 1
......

四、Xfail-期望測(cè)試失敗

1.xfail

您可以使用xfail標(biāo)記來(lái)指示您期望測(cè)試失敗。一個(gè)常見(jiàn)的用例是當(dāng)你發(fā)現(xiàn)在你的軟件中的錯(cuò)誤,你編寫(xiě)一個(gè)測(cè)試文檔軟件如何應(yīng)該的行為。在修復(fù)該錯(cuò)誤之前,該測(cè)試當(dāng)然會(huì)失敗。為避免測(cè)試失敗,請(qǐng)將測(cè)試標(biāo)記為xfail。修復(fù)錯(cuò)誤后,請(qǐng)刪除xfail標(biāo)記并進(jìn)行回歸測(cè)試,以確保該錯(cuò)誤不會(huì)再次發(fā)生。

import pytest

class TestPhoneLogin:
    @pytest.mark.xfail()
    def test_error_phone_format(self):
        assert 1 == 2
......

image.png

擁有xfail標(biāo)記仍然可以運(yùn)行測(cè)試,但是一旦失敗就不會(huì)報(bào)告回溯。相反,終端報(bào)告將在“預(yù)期失敗”(XFAIL)部分列出該報(bào)告。如果測(cè)試沒(méi)有失敗,它將報(bào)告為“意外通過(guò)”(XPASS)。

import pytest

class TestPhoneLogin:
    @pytest.mark.xfail()
    def test_error_phone_format(self):
        assert 1 == 1
......

2.strict參數(shù)

將strict參數(shù)設(shè)置為T(mén)rue,即strict=True,若在執(zhí)行測(cè)試用例時(shí)結(jié)果為xpass,那么設(shè)置該參數(shù)后會(huì)使測(cè)試記錄為失敗,原因是如果您按照xfail預(yù)期的實(shí)際失敗標(biāo)記了測(cè)試。如果它突然通過(guò),也許有人修復(fù)了該錯(cuò)誤,或者說(shuō)明測(cè)試沒(méi)有按照您的想法進(jìn)行。

import pytest

class TestPhoneLogin:
    @pytest.mark.xfail(strict=True)
    def test_error_phone_format(self):
        assert 1 == 1
......

雖然測(cè)試用例是正確的,但由于設(shè)置了strict參數(shù)為T(mén)rue,所以在最后執(zhí)行結(jié)果中仍然為Failed。若將strict參數(shù)設(shè)置為False,那么最后的執(zhí)行結(jié)果為xpass。

3.reason 參數(shù)

如果需要說(shuō)明期望測(cè)試失敗的理由,可以添加reason參數(shù)

import pytest

class TestPhoneLogin:
    @pytest.mark.xfail(reason='期望失敗的理由')
    def test_error_phone_format(self):
        assert 1 == 2
......

4.raises 參數(shù)

如果要更具體地說(shuō)明測(cè)試失敗的原因,可以在設(shè)置raises 參數(shù)。設(shè)置該參數(shù)后,如果執(zhí)行失敗,則標(biāo)記為常規(guī)Fail,除非是出現(xiàn)raises設(shè)置的錯(cuò)誤,才會(huì)顯示xfail。

import pytest

class TestPhoneLogin:
    @pytest.mark.xfail(raises=AttributeError)
    def test_error_phone_format(self):
        assert 1 == 2
......

若出現(xiàn)AttributeError錯(cuò)誤時(shí),才會(huì)顯示為xfail,出現(xiàn)其它錯(cuò)誤均顯示為Failed

image.png
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容