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

