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í)行---")
輸出結果:
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í)行---")
輸出結果:
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í)行---")
輸出結果:
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í)行---")
輸出結果:
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í)行---")
輸出結果:
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í)行---")
輸出結果:
test_fixture2.py sss
7、pycharm中運行多個測試文件
依次將要運行的文件名寫在后面即可,用逗號隔開,無需鏈表元組等形式。
if __name__ == "__main__": pytest.main(['-s', 'test_fixture1.py', 'test_fixture2.py'])
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Python Pytorch深度學習之數(shù)據(jù)加載和處理
今天小編就為大家分享一篇Pytorch 數(shù)據(jù)加載與數(shù)據(jù)預處理方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-10-10如何向scrapy中的spider傳遞參數(shù)的幾種方法
這篇文章主要介紹了如何向scrapy中的spider傳遞參數(shù)的幾種方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-11-11