pytest測試框架之skip&skipif跳過測試用例


前言

在我們自動化測試過程中,經常會遇到功能阻塞、功能未實現、環境等一系列外部因素問題導致的一些用例執行不了,這時我們就可以用到跳過skip用例,如果我們注釋掉或刪除掉,后面還要進行恢復操作。

① pytest.mark.skip 裝飾器可以標記無法在某些平台上運行的測試功能,或者希望失敗的測試功能,使其跳過。

②skip意味着只有在滿足某些條件時才希望測試通過,否則pytest應該跳過其運行其他測試。 常見示例是在非Windows平台上跳過僅限Windows的測試,或跳過測試依賴於當前不可用的外部資源(例如數據庫)。

③xfail意味着您希望測試由於某種原因而失敗。 一個常見的例子是對功能的測試尚未實施,或尚未修復的錯誤。 當測試通過時盡管預計會失敗(標記 pytest.mark.xfail ),它是一個xpass,將在測試摘要中報告。

④pytest計數並分別列出skip和xfail測試。 未顯示有關跳過/ xfailed測試的詳細信息默認情況下,以避免混亂輸出。 可以使用 -r選項查看與 short 字母對應的詳細信息顯示在測試進度中。

⑤有關-r選項的更多詳細信息,請運行 pytest -h 

⑥skip跳過成功的情況,標識為  s  ============================= 2 skipped in 0.04s ==============================

總的來講:skip和skipif允許你跳過不希望運行的測試。

skip

①跳過測試函數的最簡單方法是使用跳過裝飾器 pytest.mark.skip標記它,可以傳遞一個可選的跳過用例的原因,形參是 reason 

代碼示例:

@pytest.mark.skip(reason="no way of currently testing this")
def test_the_unknown():
    ...

②也可以通過在測試方法執行期間跳過pytest.skip(reason=reason)功能。【一般在測試方法中與 if 分支方法共同使用,表示在測試用例執行期間滿足某條件從而跳過。(與skip標記不同的是,標記更像是一個烙印,打上后直接跳過)

代碼示例:【注意】此時的 pytest.skip(reason=reason) 已經不再是一個裝飾器,而是一個函數;此外需要注意的是, pytest.skip(reason=reason) 放置位置對測試用例的的執行至關重要。如果只放在一個測試用例中,則僅僅會跳過這一個測試用例。

def test_function():
    if not valid_config():
        pytest.skip("unsupported configuration")

③還可以使用 pytest.skip(reason=reason,allow_module_level = True) 跳過整個模塊級別

import pytest
if not pytest.config.getoption("--custom-flag"): pytest.skip("--custom-flag is missing, skipping tests", allow_module_level=True)

當在測試用例收集時間內(還未執行測試用例時)無法實現跳過條件時,命令性方法很有用。【當在用例收集的時候因為某些原因導致測試用例無法跳過,使用命令性方法更方便。使用裝飾器反而達不到測試用例跳過的需求

skipif

①如果希望有條件地跳過某些內容,則可以使用 pytest.mark.skipif 裝飾器代替。

示例:標記測試的示例在Python3.6之前的解釋器上運行時要跳過的函數。

import sys
@pytest.mark.skipif(sys.version_info
< (3,6), reason="requires python3.6 or higher")
def test_function(): ...

如果條件在收集期間評估為True,則將跳過測試函數,具體跳過的原因使用 命令行參數-rs 運行測試用例時出現在測試結果的控制台中。

②在模塊之間共享 skipif 標記

案例:

# content of test_mymodule.py
import mymodule
minversion
= pytest.mark.skipif(mymodule.__versioninfo__ < (1,1), reason="at least mymodule-1.1 required")
@minversion
def test_function(): ...

然后導入上述案例的標記並在另一個測試模塊中重復使用它:

# test_myothermodule.py
from test_mymodule import minversion
@minversion
def test_anotherfunction(): ...

對於較大的測試套件,通常最好有一個專門的模塊來定義標記,然后在測試用例模塊導入並引用【這樣做的好處:方便管理和查看測試用例的標記】;這種方法一致適用於整個測試套件。

skip類或模塊

在類上使用 skipif 標記。

示例:如果 pytest.mark.skipif 裝飾器跳過條件為True,則此標記將為該類的每個測試方法生成跳過結果。

@pytest.mark.skipif(sys.platform == 'win32',reason="does not run on windows")
class TestPosixCalls(object):
    def test_function(self):
        "will not be setup or run under 'win32' platform"

【注意】:強烈建議不要在使用繼承的類上使用 skipif 標記。 pytest中的一個已知錯誤標記可能會導致超類中的意外行為。

如果要跳過某個測試模塊的所有測試方法,可以在全局級別使用 pytestmark 名稱。

示例:

# test_module.py
pytestmark = pytest.mark.skipif(...)

skip文件或目錄

有時可能需要跳過整個文件或目錄:例如,如果測試依賴於特定於Python的版本功能或包含您不希望pytest運行的代碼。

小技巧

這是一個快速指南,介紹如何在不同情況下跳過模塊中的測試。

1、無條件地跳過模塊中的所有測試:

pytestmark = pytest.mark.skip("all tests still WIP")

2、根據某些條件跳過模塊中的所有測試

pytestmark = pytest.mark.skipif(sys.platform == "win32", "tests for linux → only"

3、如果缺少某些導入,則跳過模塊中的所有測試

pexpect = pytest.importorskip("pexpect")

 

--------------------------------------------------------------------------------------------------------------------------------------

跳過測試類

@pytest.mark.skip() 標簽:被標記的類中所有方法測試用例都會被跳過

代碼示例:

import pytest


@pytest.mark.skip(reason='跳過Test類,會跳過類中所有方法')
class Test(object):
    def test_one(self):
        assert 1 == 1

    def test_two(self):
        print('test_02')
        assert 1 == 2


if __name__ == '__main__':
    pytest.main(['-rs', 'test01.py'])

運行結果:

② @pytest.mark.skipif() 標簽:被標記的類,當條件為ture時,會被跳過,否則不跳過

代碼示例:

import pytest


@pytest.mark.skipif(1 == 1, reason='跳過Test類,會跳過類中所有方法')
class Test(object):
    def test_one(self):
        assert 1 == 1

    def test_two(self):
        print('test_02')
        assert 1 == 2


if __name__ == '__main__':
    pytest.main(['-rs', 'test01.py'])

運行結果:

跳過方法或測試用例

想要某個方法或跳過某條用例,在方法上加以下3種裝飾器都可以:

 ①@pytest.mark.skip()  :跳過方法或用例,未備注原因。

代碼示例:

import pytest


class Test(object):
    @pytest.mark.skip()
    def test_one(self):
        assert 1 == 2

    def test_two(self):
        print('test_02')
        assert 1 == 1


if __name__ == '__main__':
    pytest.main(['-rs', 'test01.py'])

運行結果:

 ②@pytest.mark.skip(reason='跳過一個方法或一個測試用例') :跳過方法或用例,備注了原因。

示例代碼:

import pytest


class Test(object):
    @pytest.mark.skip(reason='跳過一個方法或一個測試用例')
    def test_one(self):
        assert 1 == 2

    def test_two(self):
        print('test_02')
        assert 1 == 1


if __name__ == '__main__':
    pytest.main(['-rs', 'test01.py'])

運行結果:

 ③@pytest.mark.skipif(1==1,reason='跳過一個方法或一個測試用例') : 當條件滿足,跳過方法或用例,備注了原因。

代碼示例:

import pytest


class Test(object):
    @pytest.mark.skipif(1 == 1, reason='跳過一個方法或一個測試用例')
    def test_one(self):
        assert 1 == 2

    def test_two(self):
        print('test_02')
        assert 1 == 1


if __name__ == '__main__':
    pytest.main(['-rs', 'test01.py'])

運行結果:

多個skip時,滿足1個條件即跳過

在類和方法上分別加了skip,類中滿足條件,方法中未滿足條件,所以生效類中skip。

代碼示例:

import pytest


@pytest.mark.skipif(1 == 1, reason='多個條件時,有1個條件滿足就跳過(類)')
class Test(object):
    @pytest.mark.skipif(1 == 2, reason='多個條件時,有1個條件滿足就跳過(方法)')
    def test_one(self):
        assert 1 == 2

    def test_two(self):
        print('test_02')
        assert 1 == 1


if __name__ == '__main__':
    pytest.main(['-rs', 'test01.py'])

運行結果:

skip賦值給變量,可多出調用

①無論是 @pytest.mark.skip() 標簽還是 @pytest.mark.skipif() 標簽,如果你想在多個測試方法上裝飾,依次寫起來很麻煩的話,你可以選擇定義個變量讓該變量等於標簽,然后在裝飾的時候用該變量代替標簽。

②這種方法,還可以通過在其他模塊中導入的變量的方式,與其他模塊共享標簽;

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

調用: @myskip 

代碼示例:

import pytest

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


class Test(object):
    @myskip
    def test_one(self):
        assert 1 == 2

    def test_two(self):
        print('test_02')
        assert 1 == 1


if __name__ == '__main__':
    pytest.main(['-rs', 'test01.py'])

運行結果:

pytest.skip()方法內跳過

①除了通過使用標簽的方式,還可以在測試用例中調用 pytest.skip()方法來實現跳過,可以選擇傳入 msg參數來說明跳過原因。

②如果想要通過判斷是否跳過,可以寫在if判斷語句里。

import pytest


class Test(object):
    def test_one(self):
        pytest.skip(msg='跳過')
        assert 1 == 2

    def test_two(self):
        print('test_02')
        assert 1 == 1


if __name__ == '__main__':
    pytest.main(['-rs', 'test01.py'])

運行結果:


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM