Pytest中skip和skipif的具體使用方法
skip的用法
使用示例:@pytest.mark.skip(reason="跳過的原因,會在執(zhí)行結(jié)果中打印")
標記在測試函數(shù)中
舉個🌰
import pytest
def test_1():
print("測試用例1")
@pytest.mark.skip(reason="沒寫完,不執(zhí)行此用例")
def test_2():
print("測試用例2")
執(zhí)行結(jié)果如下:

標記在測試類的測試用例中
舉個🌰
import pytest
class TestCase(object):
def test_1(self):
print("測試用例1")
@pytest.mark.skip(reason="沒寫完,不執(zhí)行此用例")
def test_2(self):
print("測試用例2")
執(zhí)行結(jié)果如下

標記在測試類方法上
舉個🌰
import pytest
@pytest.mark.skip(reason="沒寫完,不執(zhí)行此用例")
class TestCase1(object):
def test_1(self):
print("測試用例1")
def test_2(self):
print("測試用例2")
class TestCase2(object):
def test_3(self):
print("測試用例3")
def test_4(self):
print("測試用例4")
執(zhí)行結(jié)果如下

總結(jié)
- @pytest.mark.skip 可以加在函數(shù)上,類上,類方法上
- 如果加在類上面,則類里面的所有測試用例都不會執(zhí)行
在測試用例執(zhí)行期間強制跳過
以一個for循環(huán)為例,執(zhí)行到第3次的時候跳出
import pytest
def test_demo():
for i in range(50):
print(f"輸出第【{i}】個數(shù)")
if i == 3:
pytest.skip("跑不動了,不再執(zhí)行了")
執(zhí)行結(jié)果如下

在模塊級別跳過測試用例
語法:pytest.skip(msg="",allow_module_level=False)
當allow_module_level=True時,可以設(shè)置在模塊級別跳過整個模塊
import pytest
pytest.skip("跳過整個模塊", allow_module_level=True)
@pytest.fixture(autouse=True)
def test_1():
print("執(zhí)行測試用例1")
def test_2():
print("執(zhí)行測試用例2")
執(zhí)行結(jié)果如下

有條件的跳過某些用例
語法:@pytest.mark.skipif(condition, reason="")
import sys
import pytest
@pytest.mark.skipif(sys.platform == 'darwin', reason="does not run on MacOS")
class TestSkipIf(object):
def test_demo(self):
print("不能在MacOS上運行")
注意:condition需要返回True才會跳過
執(zhí)行結(jié)果如下:

跳過標記的使用
- 可以將 pytest.mark.skip 和 pytest.mark.skipif 賦值給一個標記變量
- 在不同模塊之間共享這個標記變量
- 若有多個模塊的測試用例需要用到相同的 skip 或 skipif ,可以用一個單獨的文件去管理這些通用標記,然后適用于整個測試用例集
舉個🌰
import sys
import pytest
skipmark = pytest.mark.skip(reason="不執(zhí)行此用例")
skipifmark = pytest.mark.skipif(sys.platform == 'darwin', reason="does not run on MacOS")
@skipifmark
class TestSkipIf(object):
def test_demo(self):
print("不能在MacOS上運行")
@skipmark
def test_1():
print("測試用例1")
def test_2():
print("測試用例2")
執(zhí)行結(jié)果如下

當缺少某些導(dǎo)入時跳過用例
語法:
pytest.importorskip( modname: str, minversion: Optional[str] = None, reason: Optional[str] = None )
參數(shù):
- modname: 需要被導(dǎo)入的模塊名稱,比如 selenium;
- minversion: 表示需要導(dǎo)入的最小的版本號,如果該版本不達標,將會打印出報錯信息;
- reason: 只有當模塊沒有被導(dǎo)入時,給定該參數(shù)將會顯示出給定的消息內(nèi)容
找不到對應(yīng)module
舉個🌰
import pytest
rock = pytest.importorskip("rock")
@rock
def test_1():
print("測試是否導(dǎo)入了rock模塊")
運行結(jié)果

如果版本不達標
舉個🌰
import pytest
sel = pytest.importorskip("selenium", minversion="3.150")
@sel
def test_1():
print("測試是否導(dǎo)入了selenium模塊")
運行結(jié)果

整理參考
到此這篇關(guān)于Pytest中skip和skipif的具體使用方法的文章就介紹到這了,更多相關(guān)skip和skipif的使用內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python實現(xiàn)哲學(xué)家就餐問題實例代碼
這篇文章主要給大家介紹了關(guān)于Python實現(xiàn)哲學(xué)家就餐問題的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習或者工作具有一定的參考學(xué)習價值,需要的朋友們下面隨著小編來一起學(xué)習學(xué)習吧2020-11-11
Python使用pandas對數(shù)據(jù)進行差分運算的方法
今天小編就為大家分享一篇Python使用pandas對數(shù)據(jù)進行差分運算的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-12-12
Windows平臺Python編程必會模塊之pywin32介紹
在Windows平臺上,從原來使用C/C++編寫原生EXE程序,到使用Python編寫一些常用腳本程序,成熟的模塊的使用使得編程效率大大提高了2019-10-10
用python3 返回鼠標位置的實現(xiàn)方法(帶界面)
今天小編就為大家分享一篇用python3 返回鼠標位置的實現(xiàn)方法(帶界面),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-07-07
Python實現(xiàn)Opencv cv2.Canny()邊緣檢測
這篇博客將介紹Canny邊緣檢測的概念,并利用cv2.Canny()實現(xiàn)邊緣檢測,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-07-07

