目錄
前言
skip
skipif
skip類或模塊
skip檔案或目錄
小技巧
跳過測驗類
跳過方法或測驗用例
多個skip時,滿足1個條件即跳過
skip賦值給變數,可多處使用裝飾器函式的語法格式呼叫
pytest.skip()方法內跳過
前言

在我們自動化測驗程序中,經常會遇到功能阻塞、功能未實作、環境等一系列外部因素問題導致的一些用例執行不了,這時我們就可以用到跳過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
代碼示例1:
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'])
運行結果1:

代碼示例2:
# test_skipif.py
import platform
import pytest
def is_windows():
if platform.system() != "Linux":
return True
elif platform.system() == "Linux":
return False
only_windows = pytest.mark.skipif(is_windows(), reason="非Linux系統時,用例均跳過")
class TestMyCode:
@pytest.mark.skipif(platform.system() != "Linux", reason="非Linux系統時,用例均跳過")
def test_skipif_001(self):
"""運算式"""
assert platform.system() == "Windows", platform.system()
@only_windows
def test_skipif_002(self):
"""裝飾器的方式封裝"""
assert platform.system() == "Linux"
if __name__ == '__main__':
pytest.main([__file__, '-vs'])
運行結果2:

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'])
運行結果:


感謝每一個認真閱讀我文章的人,看著粉絲一路的上漲和關注,禮尚往來總是要有的,雖然不是什么很值錢的東西,如果你用得到的話可以直接拿走:
① 2000多本Python電子書(主流和經典的書籍應該都有了)
② Python標準庫資料(最全中文版)
③ 專案原始碼(四五十個有趣且經典的練手專案及原始碼)
④ Python基礎入門、爬蟲、web開發、大資料分析方面的視頻(適合小白學習)
⑤ Python學習路線圖(告別不入流的學習)
在我的QQ技術交流群里(技術交流和資源共享,廣告進來腿給你打斷)
學習資料可以找到我們呢的蓓蓓小姐姐【mashang-qq】備注【csdn000】免費領取哦
【必須備注】不然不會通過哦
可以自助拿走,群號913569736(備注“csdn000”)群里的免費資料都是筆者十多年測驗生涯的精華,還有同行大神一起交流技術哦,
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/413445.html
標籤:python
