Pytest學習7 -skip、skipif跳過用例

前言
  • pytest.mark.skip 可以標記無法在某些平臺上運行的測試功能,戒者您希望失敗的測試功能
  • 希望滿足某些條件才執(zhí)行某些測試用例,否則pytest會跳過運行該測試用例
  • 實際常見場景:跳過非Windows平臺上的僅Windows測試,或者跳過依賴于當前不可用的外部資源(例如數(shù)據(jù)庫)的測試
@pytest.mark.skip

跳過執(zhí)行測試用例,有可選參數(shù)reason:跳過的原因,會在執(zhí)行結果中打印
代碼參考:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import pytest

@pytest.fixture(autouse=True)
def login():
    print("====登錄====")

def test_case01():
    print("我是測試用例11111")

@pytest.mark.skip(reason="不執(zhí)行該用例??!因為沒寫好?。?)
def test_case02():
    print("我是測試用例22222")

class Test1:
    def test_1(self):
        print("%% 我是類測試用例1111 %%")

    @pytest.mark.skip(reason="不想執(zhí)行")
    def test_2(self):
        print("%% 我是類測試用例2222 %%")


@pytest.mark.skip(reason="類也可以跳過不執(zhí)行")
class TestSkip:
    def test_1(self):
        print("%% 不會執(zhí)行 %%")

執(zhí)行結果

知識點

@pytest.mark.skip 可以加在函數(shù)上,類上,類方法上
如果加在類上面,類里面的所有測試用例都不會執(zhí)行
以上小案例都是針對:整個測試用例方法跳過執(zhí)行,如果想在測試用例執(zhí)行期間跳過不繼續(xù)往下執(zhí)行呢?

pytest.skip()函數(shù)基礎使用
作用:在測試用例執(zhí)行期間強制跳過不再執(zhí)行剩余內(nèi)容
類似:在Python的循環(huán)里面,滿足某些條件則break 跳出循環(huán)
參考代碼:

def test_function():
    n = 1
    while True:
        print(f"這是我第{n}條用例")
        n += 1
        if n == 5:
            pytest.skip("我跑五次了不跑了")

執(zhí)行結果

設置在模塊級別跳過整個模塊,可以用 allow_module_level=True
參考代碼:

#!/usr/bin/env python
# -*- coding: utf-8 -*-


import sys
import pytest

if sys.platform.startswith("win"):
    pytest.skip("skipping windows-only tests", allow_module_level=True)

@pytest.fixture(autouse=True)
def login():
    print("====登錄====")

def test_case01():
    print("我是測試用例11111")
  • 關于@pytest.mark.skipif(condition, reason="")
    作用:希望有條件地跳過某些測試用例
    注意:condition需要返回True才會跳過
@pytest.mark.skipif(sys.platform == 'win32', reason="does not run on windows")
class TestSkipIf(object):
    def test_function(self):
        print("不能在window上運行")

跳過標記
可以將 pytest.mark.skip 和 pytest.mark.skipif 賦值給一個標記變量
在不同模塊之間共享這個標記變量
若有多個模塊的測試用例需要用到相同的 skip 或 skipif ,可以用一個單獨的文件去管理這些通用標記,然后適用于整個測試用例集
代碼參考:

# 標記
skipmark = pytest.mark.skip(reason="不能在window上運行=====")
skipifmark = pytest.mark.skipif(sys.platform == 'win32', reason="不能在window上運行啦啦啦=====")

@skipmark
class TestSkip_Mark(object):
    @skipifmark
    def test_function(self):
        print("測試標記")

    def test_def(self):
        print("測試標記")

@skipmark
def test_skip():
    print("測試標記")

pytest.importorskip( modname: str, minversion: Optional[str] = None, reason: Optional[str] = None )
作用:如果缺少某些導入,則跳過模塊中的所有測試
參數(shù)列表

  • modname:模塊名
  • minversion:版本號
  • reasone:跳過原因,默認不給也行
pexpect = pytest.importorskip("pexpect", minversion="0.3")

@pexpect
def test_import():
    print("test")
  • 1執(zhí)行結果一:如果找不到module

Skipped: could not import 'pexpect': No module named 'pexpect'
collected 0 items / 1 skipped

  • 2執(zhí)行結果一:如果版本對應不上

Skipped: module 'sys' has version None, required is: '0.3'
collected 0 items / 1 skipped


參考鏈接
https://www.cnblogs.com/poloyy/p/12666682.html

?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容