色综合图-色综合图片-色综合图片二区150p-色综合图区-玖玖国产精品视频-玖玖香蕉视频

您的位置:首頁技術文章
文章詳情頁

Pytest中skip和skipif的具體使用方法

瀏覽:5日期:2022-06-15 15:40:31
skip的用法

使用示例:@pytest.mark.skip(reason='跳過的原因,會在執行結果中打印')

標記在測試函數中

舉個🌰

import pytestdef test_1(): print('測試用例1')@pytest.mark.skip(reason='沒寫完,不執行此用例')def test_2(): print('測試用例2')

執行結果如下:

Pytest中skip和skipif的具體使用方法

標記在測試類的測試用例中

舉個🌰

import pytestclass TestCase(object): def test_1(self):print('測試用例1') @pytest.mark.skip(reason='沒寫完,不執行此用例') def test_2(self):print('測試用例2')

執行結果如下

Pytest中skip和skipif的具體使用方法

標記在測試類方法上

舉個🌰

import pytest@pytest.mark.skip(reason='沒寫完,不執行此用例')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')

執行結果如下

Pytest中skip和skipif的具體使用方法

總結 @pytest.mark.skip 可以加在函數上,類上,類方法上 如果加在類上面,則類里面的所有測試用例都不會執行在測試用例執行期間強制跳過

以一個for循環為例,執行到第3次的時候跳出

import pytestdef test_demo(): for i in range(50):print(f'輸出第【{i}】個數')if i == 3: pytest.skip('跑不動了,不再執行了')

執行結果如下

Pytest中skip和skipif的具體使用方法

在模塊級別跳過測試用例

語法:pytest.skip(msg='',allow_module_level=False)

當allow_module_level=True時,可以設置在模塊級別跳過整個模塊

import pytestpytest.skip('跳過整個模塊', allow_module_level=True)@pytest.fixture(autouse=True)def test_1(): print('執行測試用例1')def test_2(): print('執行測試用例2')

執行結果如下

Pytest中skip和skipif的具體使用方法

有條件的跳過某些用例

語法:@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才會跳過

執行結果如下:

Pytest中skip和skipif的具體使用方法

跳過標記的使用 可以將 pytest.mark.skip 和 pytest.mark.skipif 賦值給一個標記變量 在不同模塊之間共享這個標記變量 若有多個模塊的測試用例需要用到相同的 skip 或 skipif ,可以用一個單獨的文件去管理這些通用標記,然后適用于整個測試用例集

舉個🌰

import sysimport pytestskipmark = pytest.mark.skip(reason='不執行此用例')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')

執行結果如下

Pytest中skip和skipif的具體使用方法

當缺少某些導入時跳過用例

語法:

pytest.importorskip( modname: str, minversion: Optional[str] = None, reason: Optional[str] = None )

參數:

modname: 需要被導入的模塊名稱,比如 selenium; minversion: 表示需要導入的最小的版本號,如果該版本不達標,將會打印出報錯信息; reason: 只有當模塊沒有被導入時,給定該參數將會顯示出給定的消息內容找不到對應module

舉個🌰

import pytestrock = pytest.importorskip('rock')@rockdef test_1(): print('測試是否導入了rock模塊')

運行結果

Pytest中skip和skipif的具體使用方法

如果版本不達標

舉個🌰

import pytestsel = pytest.importorskip('selenium', minversion='3.150')@seldef test_1(): print('測試是否導入了selenium模塊')

運行結果

Pytest中skip和skipif的具體使用方法

整理參考

小菠蘿的測試筆記

到此這篇關于Pytest中skip和skipif的具體使用方法的文章就介紹到這了,更多相關skip和skipif的使用內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: Pytest
相關文章:
主站蜘蛛池模板: 欧美在线乱妇一级毛片 | 久久频这里精品99香蕉久网址 | 日本美女福利视频 | 尹人成人 | 中文字幕欧美日韩一 | 久久精品在线免费观看 | 国产精品激情丝袜美女 | 国产乱理片在线观看夜 | 亚洲精品久久久久久久久久久网站 | 一级毛片区 | 成年女人免费看 | 日韩精品中文字幕一区三区 | 波多野结衣在线免费视频 | 久草在线资源视频 | 免费的特黄特色大片在线观看 | 欧美成a人免费观看久久 | 国产高清专区 | 亚洲素人在线 | 日本一级高清不卡视频在线 | 日韩欧美视频一区二区在线观看 | 天天爽夜夜操 | 欧美一区二区三区日韩免费播 | 国产综合第一页 | 一级成人a做片免费 | 久草在线视频在线 | 精品老司机在线视频香蕉 | 波多野结衣aⅴ在线 | 欧美日韩中文一区二区三区 | 国产精品永久免费视频观看 | 欧美精品一区二区三区在线 | 国产经典一区 | 亚洲免费国产 | 亚洲精品一区二区三区福利 | 网红主播大尺度精品福利视频 | 欧美人交性视频在线香蕉 | 欧美高清亚洲欧美一区h | 欧美一级高清免费播放 | 欧美精品成人一区二区在线观看 | 久久ri精品高清一区二区三区 | 国产在线播放成人免费 | 国产精品成人免费视频不卡 |