Pytest中skip和skipif的具體使用方法
使用示例:@pytest.mark.skip(reason='跳過的原因,會在執行結果中打印')
標記在測試函數中舉個🌰
import pytestdef test_1(): print('測試用例1')@pytest.mark.skip(reason='沒寫完,不執行此用例')def test_2(): print('測試用例2')
執行結果如下:
舉個🌰
import pytestclass TestCase(object): def test_1(self):print('測試用例1') @pytest.mark.skip(reason='沒寫完,不執行此用例') def test_2(self):print('測試用例2')
執行結果如下
舉個🌰
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')
執行結果如下
以一個for循環為例,執行到第3次的時候跳出
import pytestdef test_demo(): for i in range(50):print(f'輸出第【{i}】個數')if i == 3: pytest.skip('跑不動了,不再執行了')
執行結果如下
語法: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.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才會跳過
執行結果如下:
舉個🌰
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.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模塊')
運行結果
舉個🌰
import pytestsel = pytest.importorskip('selenium', minversion='3.150')@seldef test_1(): print('測試是否導入了selenium模塊')
運行結果
整理參考
小菠蘿的測試筆記
到此這篇關于Pytest中skip和skipif的具體使用方法的文章就介紹到這了,更多相關skip和skipif的使用內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!
相關文章:
1. Docker究竟是什么 為什么這么流行 它的優點和缺陷有哪些?2. idea打開多個窗口的操作方法3. IntelliJ IDEA設置編碼格式的方法4. IDEA 重新導入依賴maven 命令 reimport的方法5. 如何通過vscode運行調試javascript代碼6. Java14發布了,再也不怕NullPointerException了7. IntelliJ IDEA 2020.2正式發布,兩點多多總能助你提效8. Intellij IDEA 閱讀源碼的 4 個絕技(必看)9. IntelliJ IDEA 統一設置編碼為utf-8編碼的實現10. IntelliJ IDEA設置背景圖片的方法步驟
