濮阳杆衣贸易有限公司

主頁 > 知識庫 > Pytest中skip和skipif的具體使用方法

Pytest中skip和skipif的具體使用方法

熱門標簽:上海正規(guī)的外呼系統(tǒng)最新報價 煙臺電話外呼營銷系統(tǒng) 外賣地址有什么地圖標注 如何地圖標注公司 電銷機器人錄音要學(xué)習(xí)什么 企業(yè)彩鈴地圖標注 長春極信防封電銷卡批發(fā) 銀川電話機器人電話 預(yù)覽式外呼系統(tǒng)

skip的用法

使用示例:@pytest.mark.skip(reason="跳過的原因,會在執(zhí)行結(jié)果中打印")

標記在測試函數(shù)中

舉個🌰

import pytest


def test_1():
    print("測試用例1")


@pytest.mark.skip(reason="沒寫完,不執(zhí)行此用例")
def test_2():
    print("測試用例2")

執(zhí)行結(jié)果如下:

標記在測試類的測試用例中

舉個🌰

import pytest

class TestCase(object):
    def test_1(self):
        print("測試用例1")


    @pytest.mark.skip(reason="沒寫完,不執(zhí)行此用例")
    def test_2(self):
        print("測試用例2")

執(zhí)行結(jié)果如下

標記在測試類方法上

舉個🌰

import pytest


@pytest.mark.skip(reason="沒寫完,不執(zhí)行此用例")
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")

執(zhí)行結(jié)果如下

總結(jié)

  • @pytest.mark.skip 可以加在函數(shù)上,類上,類方法上
  • 如果加在類上面,則類里面的所有測試用例都不會執(zhí)行

在測試用例執(zhí)行期間強制跳過

以一個for循環(huán)為例,執(zhí)行到第3次的時候跳出

import pytest

def test_demo():
    for i in range(50):
        print(f"輸出第【{i}】個數(shù)")
        if i == 3:
            pytest.skip("跑不動了,不再執(zhí)行了")

執(zhí)行結(jié)果如下

在模塊級別跳過測試用例

語法:pytest.skip(msg="",allow_module_level=False)

當(dāng)allow_module_level=True時,可以設(shè)置在模塊級別跳過整個模塊

import pytest

pytest.skip("跳過整個模塊", allow_module_level=True)

@pytest.fixture(autouse=True)
def test_1():
    print("執(zhí)行測試用例1")

def test_2():
    print("執(zhí)行測試用例2")

執(zhí)行結(jié)果如下

有條件的跳過某些用例

語法:@pytest.mark.skipif(condition, reason="")

import sys
import pytest


@pytest.mark.skipif(sys.platform == 'darwin', reason="does not run on MacOS")
class TestSkipIf(object):
    def test_demo(self):
        print("不能在MacOS上運行")

注意:condition需要返回True才會跳過

執(zhí)行結(jié)果如下:

跳過標記的使用

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

舉個🌰

import sys
import pytest

skipmark = pytest.mark.skip(reason="不執(zhí)行此用例")
skipifmark = pytest.mark.skipif(sys.platform == 'darwin', reason="does not run on MacOS")


@skipifmark
class TestSkipIf(object):
    def test_demo(self):
        print("不能在MacOS上運行")


@skipmark
def test_1():
    print("測試用例1")


def test_2():
    print("測試用例2")

執(zhí)行結(jié)果如下

當(dāng)缺少某些導(dǎo)入時跳過用例

語法:

pytest.importorskip( modname: str, minversion: Optional[str] = None, reason: Optional[str] = None )

參數(shù):

  • modname: 需要被導(dǎo)入的模塊名稱,比如 selenium;
  • minversion: 表示需要導(dǎo)入的最小的版本號,如果該版本不達標,將會打印出報錯信息;
  • reason: 只有當(dāng)模塊沒有被導(dǎo)入時,給定該參數(shù)將會顯示出給定的消息內(nèi)容

找不到對應(yīng)module

舉個🌰

import pytest
rock = pytest.importorskip("rock")

@rock
def test_1():
    print("測試是否導(dǎo)入了rock模塊")

運行結(jié)果

如果版本不達標

舉個🌰

import pytest
sel = pytest.importorskip("selenium", minversion="3.150")

@sel
def test_1():
  	print("測試是否導(dǎo)入了selenium模塊")

運行結(jié)果

整理參考

小菠蘿的測試筆記

到此這篇關(guān)于Pytest中skip和skipif的具體使用方法的文章就介紹到這了,更多相關(guān)skip和skipif的使用內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

您可能感興趣的文章:
  • python單元測試之pytest的使用
  • python使用pytest接口自動化測試的使用
  • Python測試框架:pytest學(xué)習(xí)筆記
  • python pytest進階之xunit fixture詳解
  • python中pytest收集用例規(guī)則與運行指定用例詳解

標簽:佳木斯 西寧 盤錦 湖北 珠海 潮州 宜昌 上饒

巨人網(wǎng)絡(luò)通訊聲明:本文標題《Pytest中skip和skipif的具體使用方法》,本文關(guān)鍵詞  Pytest,中,skip,和,skipif,的,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《Pytest中skip和skipif的具體使用方法》相關(guān)的同類信息!
  • 本頁收集關(guān)于Pytest中skip和skipif的具體使用方法的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    南丰县| 南安市| 内乡县| 淅川县| 正镶白旗| 四子王旗| 柏乡县| 滁州市| 顺平县| 和平县| 田东县| 兰溪市| 怀来县| 徐州市| 五原县| 舒兰市| 宁陕县| 襄樊市| 乌海市| 故城县| 郑州市| 文安县| 壶关县| 灌南县| 定日县| 洞头县| 迭部县| 福贡县| 遂昌县| 正宁县| 双城市| 万山特区| 鄂托克旗| 尼勒克县| 南江县| 永年县| 霞浦县| 勃利县| 西乌珠穆沁旗| 罗田县| 巴彦县|