pytest?用例執(zhí)行失敗后其他不再執(zhí)行
1.hook函數(shù)介紹
在執(zhí)行用例時,遇到用例之前存在有關(guān)聯(lián),用例執(zhí)行失敗后,其余用例也沒有必要再去執(zhí)行(比如:登錄失敗后,我的列表、我的訂單等用例就沒有比必要執(zhí)行了)。
完成這個功能我們只要學習pytest給我們提供的兩個Hook函數(shù):
pytest_runtest_setup
:在執(zhí)行測試之前執(zhí)行,類似unittest中的setup方法。pytest_runtest_makereport
:測試報告鉤子函數(shù),打印有關(guān)測試運行的信息。
2.pytest_runtest_setup
方法只有一個參數(shù)item
,不需要手動傳遞,pytest會自動的將所需內(nèi)容傳入給這個方法。item
:主要記錄了用例的名稱、所在模塊、所在類名、文件路徑等信息。
介紹一下item中不太明確的參數(shù):
keywords:當前用例被mark
標記的名稱。
- 例如@pytest.mark.somke:keywords == somke
cls:當前用例的類名稱。
3.pytest_runtest_makereport
方法有兩個參數(shù)item
和call
,item參數(shù)與上面一樣的。
參數(shù)call
:用于了記錄執(zhí)行過程、測試報告等信息。
call 中的參數(shù)介紹:
when:記錄測試用例運行的狀態(tài),總共三個狀態(tài):
- setup:用例“執(zhí)行前”。
- call:用例"執(zhí)行中"。
- teardown:用例"執(zhí)行完成"。
excinfo:如果用例執(zhí)行失敗,會記錄在這里。
4.解決思路
通過上面的分析,可以得到一個簡單的思路,就是:在每個測試用例執(zhí)行之前,判斷當前用例所在的類是否有失敗的用例,如果有就使用pytest.xfail
標記為失敗的用例,不再執(zhí)行。
步驟:
1. 定義一個全局變量。
2. 自定一個mark標記,將用例之間有耦合性的用例標記出來(可以通過這個來控制是否需要)。
3. 添加失敗用例:用pytest_runtest_makereport
hook函數(shù)中使用call中的excinfo信息判斷用例是否失敗,如果失敗就將用例添加到全局變量中。
4. 在用例執(zhí)行前判斷是否有失敗用例:用pytest_runtest_setup
hook函數(shù)中實現(xiàn)判斷當前用例所在的類是否有失敗的用例。
實現(xiàn)代碼:
當前代碼最好在conftest.py文件中實現(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) # 判斷當前用例的類是否在全局變量中 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) # 如果當前類中的用例存在失敗用例,就跳過 if test_name is not None: pytest.xfail("previous test failed ({})".format(test_name))
測試代碼:
@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跳過。
到此這篇關(guān)于pytest 用例執(zhí)行失敗后其他不再執(zhí)行的文章就介紹到這了,更多相關(guān)pytest 用例執(zhí)行失敗內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
基于TensorFlow的CNN實現(xiàn)Mnist手寫數(shù)字識別
這篇文章主要為大家詳細介紹了基于TensorFlow的CNN實現(xiàn)Mnist手寫數(shù)字識別,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2020-06-06Python NumPy數(shù)組利器之np.zeros函數(shù)詳解與應用實例
在Python的科學計算庫NumPy中,numpy.zeros()是一個非常重要的函數(shù),它用于創(chuàng)建一個指定形狀和數(shù)據(jù)類型的全零數(shù)組,這篇文章主要給大家介紹了關(guān)于Python NumPy數(shù)組利器之np.zeros函數(shù)詳解與應用實例的相關(guān)資料,需要的朋友可以參考下2024-06-06Python利用pandas進行數(shù)據(jù)合并詳解
當使用Python中的pandas庫時,merge函數(shù)是用于合并(或連接)兩個數(shù)據(jù)框(DataFrame)的重要工具。它類似于SQL中的JOIN操作,下面我們就來看看它的具體操作吧2023-11-11Python實現(xiàn)的插入排序算法原理與用法實例分析
這篇文章主要介紹了Python實現(xiàn)的插入排序算法原理與用法,簡單描述了插入排序的原理,并結(jié)合實例形式分析了Python實現(xiàn)插入排序的相關(guān)操作技巧,需要的朋友可以參考下2017-11-11