Pytest中skip和skipif的具體使用方法
使用示例:@pytest.mark.skip(reason='跳過的原因,會(huì)在執(zhí)行結(jié)果中打印')
標(biāo)記在測(cè)試函數(shù)中舉個(gè)🌰
import pytestdef test_1(): print('測(cè)試用例1')@pytest.mark.skip(reason='沒寫完,不執(zhí)行此用例')def test_2(): print('測(cè)試用例2')
執(zhí)行結(jié)果如下:
舉個(gè)🌰
import pytestclass TestCase(object): def test_1(self):print('測(cè)試用例1') @pytest.mark.skip(reason='沒寫完,不執(zhí)行此用例') def test_2(self):print('測(cè)試用例2')
執(zhí)行結(jié)果如下
舉個(gè)🌰
import [email protected](reason='沒寫完,不執(zhí)行此用例')class TestCase1(object): def test_1(self):print('測(cè)試用例1') def test_2(self):print('測(cè)試用例2')class TestCase2(object): def test_3(self):print('測(cè)試用例3') def test_4(self):print('測(cè)試用例4')
執(zhí)行結(jié)果如下
以一個(gè)for循環(huán)為例,執(zhí)行到第3次的時(shí)候跳出
import pytestdef test_demo(): for i in range(50):print(f'輸出第【{i}】個(gè)數(shù)')if i == 3: pytest.skip('跑不動(dòng)了,不再執(zhí)行了')
執(zhí)行結(jié)果如下
語(yǔ)法:pytest.skip(msg='',allow_module_level=False)
當(dāng)allow_module_level=True時(shí),可以設(shè)置在模塊級(jí)別跳過整個(gè)模塊
import pytestpytest.skip('跳過整個(gè)模塊', allow_module_level=True)@pytest.fixture(autouse=True)def test_1(): print('執(zhí)行測(cè)試用例1')def test_2(): print('執(zhí)行測(cè)試用例2')
執(zhí)行結(jié)果如下
語(yǔ)法:@pytest.mark.skipif(condition, reason='')
import sysimport [email protected](sys.platform == ’darwin’, reason='does not run on MacOS')class TestSkipIf(object): def test_demo(self):print('不能在MacOS上運(yùn)行')
注意:condition需要返回True才會(huì)跳過
執(zhí)行結(jié)果如下:
舉個(gè)🌰
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上運(yùn)行')@skipmarkdef test_1(): print('測(cè)試用例1')def test_2(): print('測(cè)試用例2')
執(zhí)行結(jié)果如下
語(yǔ)法:
pytest.importorskip( modname: str, minversion: Optional[str] = None, reason: Optional[str] = None )
參數(shù):
modname: 需要被導(dǎo)入的模塊名稱,比如 selenium; minversion: 表示需要導(dǎo)入的最小的版本號(hào),如果該版本不達(dá)標(biāo),將會(huì)打印出報(bào)錯(cuò)信息; reason: 只有當(dāng)模塊沒有被導(dǎo)入時(shí),給定該參數(shù)將會(huì)顯示出給定的消息內(nèi)容找不到對(duì)應(yīng)module舉個(gè)🌰
import pytestrock = pytest.importorskip('rock')@rockdef test_1(): print('測(cè)試是否導(dǎo)入了rock模塊')
運(yùn)行結(jié)果
舉個(gè)🌰
import pytestsel = pytest.importorskip('selenium', minversion='3.150')@seldef test_1(): print('測(cè)試是否導(dǎo)入了selenium模塊')
運(yùn)行結(jié)果
整理參考
小菠蘿的測(cè)試筆記
到此這篇關(guān)于Pytest中skip和skipif的具體使用方法的文章就介紹到這了,更多相關(guān)skip和skipif的使用內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. WML語(yǔ)言的基本情況2. Python過濾掉numpy.array中非nan數(shù)據(jù)實(shí)例3. python利用platform模塊獲取系統(tǒng)信息4. Python 多線程之threading 模塊的使用5. CSS代碼檢查工具stylelint的使用方法詳解6. 淺談python多線程和多線程變量共享問題介紹7. Python如何批量獲取文件夾的大小并保存8. react axios 跨域訪問一個(gè)或多個(gè)域名問題9. Python的Tqdm模塊實(shí)現(xiàn)進(jìn)度條配置10. python 實(shí)現(xiàn)rolling和apply函數(shù)的向下取值操作
