前言
- pytest.mark.skip可以標記無法在某些平臺上運行的測試功能,
- 或者您希望失敗的測試功能希望滿足某些條件才執(zhí)行某些測試用例,否則pytest會跳過運行該測試用例
- 實際常見場景:跳過非Windows平臺上的僅Windows測試,或者跳過依賴于當前不可用的外部資源(例如數據庫)的測試
@pytest.mark.skip
跳過執(zhí)行測試用例,有可選參數reason:跳過的原因,會在執(zhí)行結果中打印
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
__title__ =
__Time__ = 2020/4/9 13:49
__Author__ = 小菠蘿測試筆記
__Blog__ = https://www.cnblogs.com/poloyy/
"""
import pytest
@pytest.fixture(autouse=True)
def login():
print("====登錄====")
def test_case01():
print("我是測試用例11111")
@pytest.mark.skip(reason="不執(zhí)行該用例??!因為沒寫好??!")
def test_case02():
print("我是測試用例22222")
class Test1:
def test_1(self):
print("%% 我是類測試用例1111 %%")
@pytest.mark.skip(reason="不想執(zhí)行")
def test_2(self):
print("%% 我是類測試用例2222 %%")
@pytest.mark.skip(reason="類也可以跳過不執(zhí)行")
class TestSkip:
def test_1(self):
print("%% 不會執(zhí)行 %%")
執(zhí)行結果

知識點
- @pytest.mark.skip可以加在函數上,類上,類方法上
- 如果加在類上面,類里面的所有測試用例都不會執(zhí)行
- 以上小案例都是針對:整個測試用例方法跳過執(zhí)行,如果想在測試用例執(zhí)行期間跳過不繼續(xù)往下執(zhí)行呢?
pytest.skip()函數基礎使用
作用:在測試用例執(zhí)行期間強制跳過不再執(zhí)行剩余內容
類似:在Python的循環(huán)里面,滿足某些條件則break 跳出循環(huán)
def test_function():
n = 1
while True:
print(f"這是我第{n}條用例")
n += 1
if n == 5:
pytest.skip("我跑五次了不跑了")
執(zhí)行結果

pytest.skip(msg="",allow_module_level=False)
當allow_module_level=True時,可以設置在模塊級別跳過整個模塊
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
__title__ =
__Time__ = 2020/4/9 13:49
__Author__ = 小菠蘿測試筆記
__Blog__ = https://www.cnblogs.com/poloyy/
"""
import sys
import pytest
if sys.platform.startswith("win"):
pytest.skip("skipping windows-only tests", allow_module_level=True)
@pytest.fixture(autouse=True)
def login():
print("====登錄====")
def test_case01():
print("我是測試用例11111")
執(zhí)行結果
collecting ...
Skipped: skipping windows-only tests
collected 0 items / 1 skipped
============================= 1 skipped in 0.15s ==============================
@pytest.mark.skipif(condition, reason="")
作用:希望有條件地跳過某些測試用例
注意:condition需要返回True才會跳過
@pytest.mark.skipif(sys.platform == 'win32', reason="does not run on windows")
class TestSkipIf(object):
def test_function(self):
print("不能在window上運行")
執(zhí)行結果
collecting ... collected 1 item
07skip_sipif.py::TestSkipIf::test_function SKIPPED [100%]
Skipped: does not run on windows
============================= 1 skipped in 0.04s ==============================
跳過標記
- 可以將pytest.mark.skip和pytest.mark.skipif賦值給一個標記變量
- 在不同模塊之間共享這個標記變量
- 若有多個模塊的測試用例需要用到相同的skip或skipif,可以用一個單獨的文件去管理這些通用標記,然后適用于整個測試用例集
# 標記
skipmark = pytest.mark.skip(reason="不能在window上運行=====")
skipifmark = pytest.mark.skipif(sys.platform == 'win32', reason="不能在window上運行啦啦啦=====")
@skipmark
class TestSkip_Mark(object):
@skipifmark
def test_function(self):
print("測試標記")
def test_def(self):
print("測試標記")
@skipmark
def test_skip():
print("測試標記")
執(zhí)行結果
collecting ... collected 3 items
07skip_sipif.py::TestSkip_Mark::test_function SKIPPED [ 33%]
Skipped: 不能在window上運行啦啦啦=====
07skip_sipif.py::TestSkip_Mark::test_def SKIPPED [ 66%]
Skipped: 不能在window上運行=====
07skip_sipif.py::test_skip SKIPPED [100%]
Skipped: 不能在window上運行=====
============================= 3 skipped in 0.04s ==============================
pytest.importorskip( modname: str, minversion: Optional[str] = None, reason: Optional[str] = None )
作用:如果缺少某些導入,則跳過模塊中的所有測試
參數列表
- modname:模塊名
- minversion:版本號
- reasone:跳過原因,默認不給也行
pexpect = pytest.importorskip("pexpect", minversion="0.3")
@pexpect
def test_import():
print("test")
執(zhí)行結果一:如果找不到module
Skipped: could not import 'pexpect': No module named 'pexpect'
collected 0 items / 1 skipped
執(zhí)行結果一:如果版本對應不上
Skipped: module 'sys' has __version__ None, required is: '0.3'
collected 0 items / 1 skipped
到此這篇關于Pytest中skip skipif跳過用例詳解的文章就介紹到這了,更多相關skip skipif跳過用例內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
您可能感興趣的文章:- python使用pytest接口自動化測試的使用
- Python 測試框架unittest和pytest的優(yōu)劣
- python pytest進階之conftest.py詳解
- python的pytest框架之命令行參數詳解(下)