日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無先后,達(dá)者為師

網(wǎng)站首頁 編程語言 正文

pytest?用例執(zhí)行失敗后其他不再執(zhí)行_python

作者:qq_37668510 ? 更新時(shí)間: 2023-04-10 編程語言

1.hook函數(shù)介紹

在執(zhí)行用例時(shí),遇到用例之前存在有關(guān)聯(lián),用例執(zhí)行失敗后,其余用例也沒有必要再去執(zhí)行(比如:登錄失敗后,我的列表、我的訂單等用例就沒有比必要執(zhí)行了)。

完成這個(gè)功能我們只要學(xué)習(xí)pytest給我們提供的兩個(gè)Hook函數(shù):

  • pytest_runtest_setup :在執(zhí)行測(cè)試之前執(zhí)行,類似unittest中的setup方法。
  • pytest_runtest_makereport:測(cè)試報(bào)告鉤子函數(shù),打印有關(guān)測(cè)試運(yùn)行的信息。

2.pytest_runtest_setup

方法只有一個(gè)參數(shù)item,不需要手動(dòng)傳遞,pytest會(huì)自動(dòng)的將所需內(nèi)容傳入給這個(gè)方法。
item:主要記錄了用例的名稱、所在模塊、所在類名、文件路徑等信息。

介紹一下item中不太明確的參數(shù):

keywords:當(dāng)前用例被mark標(biāo)記的名稱。
- 例如@pytest.mark.somke:keywords == somke

cls:當(dāng)前用例的類名稱。

3.pytest_runtest_makereport

方法有兩個(gè)參數(shù)itemcall,item參數(shù)與上面一樣的。
參數(shù)call :用于了記錄執(zhí)行過程、測(cè)試報(bào)告等信息。

call 中的參數(shù)介紹:

when:記錄測(cè)試用例運(yùn)行的狀態(tài),總共三個(gè)狀態(tài):
- setup:用例“執(zhí)行前”。
- call:用例"執(zhí)行中"。
- teardown:用例"執(zhí)行完成"。

excinfo:如果用例執(zhí)行失敗,會(huì)記錄在這里。

4.解決思路

通過上面的分析,可以得到一個(gè)簡(jiǎn)單的思路,就是:在每個(gè)測(cè)試用例執(zhí)行之前,判斷當(dāng)前用例所在的類是否有失敗的用例,如果有就使用pytest.xfail標(biāo)記為失敗的用例,不再執(zhí)行。

步驟:
1. 定義一個(gè)全局變量。
2. 自定一個(gè)mark標(biāo)記,將用例之間有耦合性的用例標(biāo)記出來(可以通過這個(gè)來控制是否需要)。
3. 添加失敗用例:用pytest_runtest_makereport hook函數(shù)中使用call中的excinfo信息判斷用例是否失敗,如果失敗就將用例添加到全局變量中。
4. 在用例執(zhí)行前判斷是否有失敗用例:用pytest_runtest_setuphook函數(shù)中實(shí)現(xiàn)判斷當(dāng)前用例所在的類是否有失敗的用例。

實(shí)現(xiàn)代碼:
當(dāng)前代碼最好在conftest.py文件中實(shí)現(xiàn)。

# conftest.py

from typing import Dict, Tuple
import pytest

# 全局變量,記錄失敗的用例
_test_failed_incremental: Dict[str, Dict[Tuple[int, ...], str]] = {}


def pytest_runtest_makereport(item, call):
    # 判斷用例執(zhí)行失敗后是否需要跳過
    if "incremental" in item.keywords:
    	# 如果用例失敗,添加到全局變量中
        if call.excinfo is not None:
            cls_name = str(item.cls)
            parametrize_index = (
                tuple(item.callspec.indices.values())
                if hasattr(item, "callspec")
                else ()
            )
            test_name = item.originalname or item.name
            _test_failed_incremental.setdefault(cls_name, {}).setdefault(
                parametrize_index, test_name
            )


def pytest_runtest_setup(item):
	# 判斷用例執(zhí)行失敗后是否需要跳過
    if "incremental" in item.keywords:
        cls_name = str(item.cls)
        # 判斷當(dāng)前用例的類是否在全局變量中
        if cls_name in _test_failed_incremental:
            parametrize_index = (
                tuple(item.callspec.indices.values())
                if hasattr(item, "callspec")
                else ()
            )
            test_name = _test_failed_incremental[cls_name].get(parametrize_index, None)
            # 如果當(dāng)前類中的用例存在失敗用例,就跳過
            if test_name is not None:
                pytest.xfail("previous test failed ({})".format(test_name))

測(cè)試代碼:

@pytest.mark.incremental
class TestAdd:

    def test_01(self):
        print('test_01 用例執(zhí)行中...')

    
    def test_02(self):
        pytest.xfail('以后的用例都失敗了0')

    def test_03(self):
        print('test_03 用例執(zhí)行中...')
        
if __name__ == "__main__":
    pytest.main()

結(jié)果:
test_01用例執(zhí)行成功,
test_02失敗,
test_03跳過。

原文鏈接:https://blog.csdn.net/qq_37668510/article/details/125232006

欄目分類
最近更新