pytest文件12 skip跳過用例

2022-05-18 02:49:39 字數 3529 閱讀 4500

pytest.mark.skip可以標記無法在某些平台上執行的測試功能,或者您希望失敗的測試功能

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

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

pytest計數並分別列出skip和xfail測試。 未顯示有關跳過/ xfailed測試的詳細資訊預設情況下,以避免混亂輸出。 您可以使用-r選項檢視與「short」字母對應的詳細資訊顯示在測試進度中

pytest -rxxs # show extra info on xfailed, xpassed, and skipped tests

有關-r選項的更多詳細資訊,請執行pytest -h

跳過測試函式的最簡單方法是使用跳過裝飾器標記它,可以傳遞乙個可選的原因

@pytest.mark.skip(reason="

no way of currently testing this")

deftest_the_unknown():

...

或者,也可以通過呼叫來在測試執行或設定期間強制跳過pytest.skip(reason)功能:

def

test_function():

ifnot

valid_config():

pytest.skip(

"unsupported configuration

")

也可以使用pytest.skip(reason,allow_module_level = true)跳過整個模組級別:

import

pytest

ifnot pytest.config.getoption("

--custom-flag"):

pytest.skip(

"--custom-flag is missing, skipping tests

", allow_module_level=true)

當在匯入時間內無法評估跳過條件時,命令性方法很有用。

如果您希望有條件地跳過某些內容,則可以使用skipif代替。 這是標記測試的示例在python3.6之前的直譯器上執行時要跳過的函式

import

[email protected](sys.version_info

< (3,6),

reason="

requires python3.6 or higher")

deftest_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

deftest_function():

...

您可以匯入標記並在另乙個測試模組中重複使用它:

#

test_myothermodule.py

from test_mymodule import

minversion

@minversion

deftest_anotherfunction():

...

對於較大的測試套件,通常最好有乙個檔案來定義標記,然後一致適用於整個測試套件。

或者,您可以使用條件字串而不是布林值,但它們之間不能輕易共享它們支援它們主要是出於向後相容的原因

您可以在類上使用skipif標記(與任何其他標記一樣):

@pytest.mark.skipif(sys.platform == '

win32',

reason="

does not run on windows")

class

testposixcalls(object):

deftest_function(self):

"will not be setup or run under 'win32' platform

"

如果條件為true,則此標記將為該類的每個測試方法生成跳過結果

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

如果要跳過模組的所有測試功能,可以在全域性級別使用pytestmark名稱

#

test_module.py

pytestmark = pytest.mark.skipif(...)

如果將多個skipif裝飾器應用於測試函式,則如果任何跳過條件為真,則將跳過它

有時您可能需要跳過整個檔案或目錄,例如,如果測試依賴於特定於python的版本功能或包含您不希望pytest執行的**。 在這種情況下,您必須排除檔案和目錄來自收藏。 有關更多資訊,請參閱自定義測試集合。

您可以在模組級別或測試或測試設定功能中使用以下幫助程式

docutils = pytest.importorskip("

docutils

")

如果無法在此處匯入docutils,則會導致測試跳過結果。 你也可以跳過庫的版本號

docutils = pytest.importorskip("

docutils

", minversion="

0.3")

將從指定模組的__version__屬性中讀取版本。

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

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八 skip 跳過用例

這是乙個快速指南,介紹如何在不同情況下跳過模組中的測試 1.無條件地跳過模組中的所有測試 pytestmark pytest.mark.skip all tests still wip 2.根據某些條件跳過模組中的所有測試 pytestmark pytest.mark.skipif sys.plat...

pytest跳過執行skip和skipif

相關函式 pytest.mark.skip pytest.mark.skipif 可以看看原函式 staticmethod def skipif condition,reason none skip the given test function if eval condition results ...

pytest教程之skip執行時跳過用例

在實際的自動化測試過程中,我們會因為某些原因想跳過部分用例的執行,例如個別用例在某個環境中無法執行時。這時候使用pytest的skip功能可以跳過這些用例執行。下面看例子 import pytest import sys skipmark pytest.mark.skip reason 不能在sta...