Pytest中skip和skipif的具體使用方法
使用示例:@pytest.mark.skip(reason='跳過的原因,會在執(zhí)行結(jié)果中打印')
標記在測試函數(shù)中舉個🌰
import pytestdef test_1(): print('測試用例1')@pytest.mark.skip(reason='沒寫完,不執(zhí)行此用例')def test_2(): print('測試用例2')
執(zhí)行結(jié)果如下:
舉個🌰
import pytestclass 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é)果如下
以一個for循環(huán)為例,執(zhí)行到第3次的時候跳出
import pytestdef 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 pytestpytest.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 sysimport 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é)果如下:
舉個🌰
import sysimport pytestskipmark = pytest.mark.skip(reason='不執(zhí)行此用例')skipifmark = pytest.mark.skipif(sys.platform == ’darwin’, reason='does not run on MacOS')@skipifmarkclass TestSkipIf(object): def test_demo(self):print('不能在MacOS上運行')@skipmarkdef test_1(): print('測試用例1')def test_2(): print('測試用例2')
執(zhí)行結(jié)果如下
語法:
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 pytestrock = pytest.importorskip('rock')@rockdef test_1(): print('測試是否導(dǎo)入了rock模塊')
運行結(jié)果
舉個🌰
import pytestsel = pytest.importorskip('selenium', minversion='3.150')@seldef test_1(): print('測試是否導(dǎo)入了selenium模塊')
運行結(jié)果
整理參考
小菠蘿的測試筆記
到此這篇關(guān)于Pytest中skip和skipif的具體使用方法的文章就介紹到這了,更多相關(guān)skip和skipif的使用內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. 基于PHP做個圖片防盜鏈2. ASP.NET MVC使用Boostrap實現(xiàn)產(chǎn)品展示、查詢、排序、分頁3. .NET中實現(xiàn)對象數(shù)據(jù)映射示例詳解4. jscript與vbscript 操作XML元素屬性的代碼5. asp.net core 認證和授權(quán)實例詳解6. php使用正則驗證密碼字段的復(fù)雜強度原理詳細講解 原創(chuàng)7. XML在語音合成中的應(yīng)用8. 如何使用ASP.NET Core 配置文件9. 基于javaweb+jsp實現(xiàn)企業(yè)車輛管理系統(tǒng)10. ASP.NET MVC把數(shù)據(jù)庫中枚舉項的數(shù)字轉(zhuǎn)換成文字
