網(wǎng)站首頁 編程語言 正文
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ù)item
和call
,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_setup
hook函數(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
相關(guān)推薦
- 2022-03-20 基于ABP框架實(shí)現(xiàn)RBAC(角色訪問控制)_實(shí)用技巧
- 2022-11-14 基于統(tǒng)計(jì)自適應(yīng)線性回歸-目標(biāo)尺寸預(yù)測(cè)
- 2022-10-22 ListView下拉列表控件使用方法詳解_Android
- 2022-09-16 利用Python第三方庫xlwt寫入數(shù)據(jù)到Excel工作表實(shí)例代碼_python
- 2022-09-19 Tomca啟動(dòng)閃退問題解決(八大類)_Tomcat
- 2022-05-28 使用pandas計(jì)算環(huán)比和同比的方法實(shí)例_python
- 2022-10-04 Redis中Redisson紅鎖(Redlock)使用原理_Redis
- 2022-08-13 采用python開發(fā)sparkstreming全流程
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支