Pytest如何使用skip跳過執(zhí)行測試
1、@pytest.mark.skip(reason=" ") -- 跳過執(zhí)行測試函數(shù)
可傳入一個(gè)非必須參數(shù)reason表示原因
import pytest
@pytest.mark.skip(reason="no reason")
def test_01():
print("---用例a執(zhí)行---")
class TestCase():
@pytest.mark.skip(reason="no reason")
def test_02(self):
print("---用例b執(zhí)行---")
def test_03(self):
print("---用例c執(zhí)行---")
輸出結(jié)果:
test_fixture2.py ss---用例c執(zhí)行---
2、@pytest.mark.skipif(condition...) -- 若滿足condition,則跳過測試函數(shù)
傳入condition參數(shù)為判斷條件,可以選擇傳入非必須參數(shù)reason;如果多個(gè)標(biāo)簽一起使用,滿足其中一個(gè)跳過條件則會(huì)跳過該測試函數(shù)。
import pytest
def test_01():
print("---用例a執(zhí)行---")
class TestCase():
#當(dāng)多個(gè)@pytest.mark.skipif()標(biāo)簽時(shí),若滿足一個(gè),則跳過測試函數(shù)
@pytest.mark.skipif(condition='a' >= 'b', reason="no reason")
@pytest.mark.skipif(condition='a' <= 'b', reason="no reason")
def test_02(self):
print("---用例b執(zhí)行---")
def test_03(self):
print("---用例c執(zhí)行---")
輸出結(jié)果:
test_fixture2.py ---用例a執(zhí)行---
.s---用例c執(zhí)行---
3、自定義@pytest.mark.skip()標(biāo)簽
myskip = pytest.mark.skip() 或 myskip = pytest.mark.skipif(condition=...)
裝飾時(shí)用該變量代替標(biāo)簽即可:@myskip
import pytest
# myskip = pytest.mark.skip()
myskip = pytest.mark.skipif(condition=2>1, reason="no reason")
@myskip
def test_01():
print("---用例a執(zhí)行---")
class TestCase():
@myskip
def test_02(self):
print("---用例b執(zhí)行---")
def test_03(self):
print("---用例c執(zhí)行---")
輸出結(jié)果:
test_fixture2.py ss---用例c執(zhí)行---
4、通過pytest.skip()方法跳過測試函數(shù)
import pytest
def test_01():
pytest.skip(msg="no reason")
print("---用例a執(zhí)行---")
class TestCase():
def test_02(self):
pytest.skip()
print("---用例b執(zhí)行---")
def test_03(self):
print("---用例c執(zhí)行---")
輸出結(jié)果:
test_fixture2.py ss---用例c執(zhí)行--
5、跳過測試類
跳過測試類其實(shí)和跳過測試方法一樣,使用@pytest.mark.skip()和@pytest.mark.skipif()兩個(gè)標(biāo)簽,用他們裝飾測試類就好啦。
import pytest
myskip = pytest.mark.skip(reason="no reason")
def test_01():
print("---用例a執(zhí)行---")
@myskip
class TestCase():
def test_02(self):
print("---用例b執(zhí)行---")
def test_03(self):
print("---用例c執(zhí)行---")
輸出結(jié)果:
test_fixture2.py ---用例a執(zhí)行---
6、跳過模塊
使用pytestmark(不可更改變量名)變量,讓他等于標(biāo)簽即可。
import pytest
pytestmark = pytest.mark.skip(condition=2>1, reason='no reason')
def test_01():
print("---用例a執(zhí)行---")
class TestCase():
def test_02(self):
print("---用例b執(zhí)行---")
def test_03(self):
print("---用例c執(zhí)行---")
輸出結(jié)果:
test_fixture2.py sss
7、pycharm中運(yùn)行多個(gè)測試文件
依次將要運(yùn)行的文件名寫在后面即可,用逗號(hào)隔開,無需鏈表元組等形式。
if __name__ == "__main__": pytest.main(['-s', 'test_fixture1.py', 'test_fixture2.py'])
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Python實(shí)現(xiàn)視頻中添加音頻工具詳解
本文主要為大家介紹了Python中提供在無音頻的視頻中添加音頻的工具詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考一下2021-12-12
利用python和百度地圖API實(shí)現(xiàn)數(shù)據(jù)地圖標(biāo)注的方法
這篇文章主要介紹了利用python和百度地圖API實(shí)現(xiàn)數(shù)據(jù)地圖標(biāo)注的方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-05-05
Python圖像處理庫Pillow的簡單實(shí)現(xiàn)
本文主要介紹了Python圖像處理庫Pillow的簡單實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-06-06
python NumPy ndarray二維數(shù)組 按照行列求平均實(shí)例
今天小編就為大家分享一篇python NumPy ndarray二維數(shù)組 按照行列求平均實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-11-11
淺談pytorch 模型 .pt, .pth, .pkl的區(qū)別及模型保存方式
這篇文章主要介紹了淺談pytorch 模型 .pt, .pth, .pkl的區(qū)別及模型保存方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-05-05
使用pycharm運(yùn)行flask應(yīng)用程序的詳細(xì)教程
這篇文章主要介紹了使用pycharm運(yùn)行flask應(yīng)用程序,首先大家需要使用pycharm創(chuàng)建你的第一個(gè)app,接下來就開始配置pycharm,需要的朋友可以參考下2021-06-06

