当前位置:网站首页>【pytest学习】pytest 用例执行失败后其他不再执行
【pytest学习】pytest 用例执行失败后其他不再执行
2022-06-11 16:21:00 【qq_37668510】
1.hook函数介绍
在执行用例时,遇到用例之前存在有关联,用例执行失败后,其余用例也没有必要再去执行(比如:登录失败后,我的列表、我的订单等用例就没有比必要执行了)。
完成这个功能我们只要学习pytest给我们提供的两个Hook函数:
pytest_runtest_setup:在执行测试之前执行,类似unittest中的setup方法。pytest_runtest_makereport:测试报告钩子函数,打印有关测试运行的信息。
2.pytest_runtest_setup
方法只有一个参数item,不需要手动传递,pytest会自动的将所需内容传入给这个方法。item:主要记录了用例的名称、所在模块、所在类名、文件路径等信息。
介绍一下item中不太明确的参数:
- keywords:当前用例被
mark标记的名称。
- 例如@pytest.mark.somke:keywords == somke - cls:当前用例的类名称。
3.pytest_runtest_makereport
方法有两个参数item和call,item参数与上面一样的。
参数call :用于了记录执行过程、测试报告等信息。
call 中的参数介绍:
- when:记录测试用例运行的状态,总共三个状态:
- setup:用例“执行前”。
- call:用例"执行中"。
- teardown:用例"执行完成"。 - excinfo:如果用例执行失败,会记录在这里。
4.解决思路
通过上面的分析,可以得到一个简单的思路,就是:在每个测试用例执行之前,判断当前用例所在的类是否有失败的用例,如果有就使用pytest.xfail标记为失败的用例,不再执行。
步骤:
1. 定义一个全局变量。
2. 自定一个mark标记,将用例之间有耦合性的用例标记出来(可以通过这个来控制是否需要)。
3. 添加失败用例:用pytest_runtest_makereport hook函数中使用call中的excinfo信息判断用例是否失败,如果失败就将用例添加到全局变量中。
4. 在用例执行前判断是否有失败用例:用pytest_runtest_setuphook函数中实现判断当前用例所在的类是否有失败的用例。
实现代码:
当前代码最好在conftest.py文件中实现。
# conftest.py
from typing import Dict, Tuple
import pytest
# 全局变量,记录失败的用例
_test_failed_incremental: Dict[str, Dict[Tuple[int, ...], str]] = {
}
def pytest_runtest_makereport(item, call):
# 判断用例执行失败后是否需要跳过
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):
# 判断用例执行失败后是否需要跳过
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 用例执行中...')
def test_02(self):
pytest.xfail('以后的用例都失败了0')
def test_03(self):
print('test_03 用例执行中...')
if __name__ == "__main__":
pytest.main()
结果:
test_01用例执行成功,
test_02失败,
test_03跳过。
边栏推荐
- Will you be punished for not wearing seat belts in the back row?
- Cloud data management will break the island of storage and the island of team
- 09-最小生成树 公路村村通
- Go语言之Go 快速入门篇(一):第一个 Go 程序
- What is RS stamp paper?
- 【opencvsharp】斑点检测 条码解码 图像操作 图像旋转/翻转/缩放 透视变换 图像显示控件 demo笔记
- 回归预测 | MATLAB实现RBF径向基神经网络多输入单输出
- 09 Minimum Spanning Tree highway
- Pyqt5 enables the qplaintextedit control to support line number display
- How the autorunner automated test tool creates a project -alltesting | Zezhong cloud test
猜你喜欢

Go quick start of go language (I): the first go program

Leetcode 1974. 使用特殊打字机键入单词的最少时间(可以,一次过)

Pytest test framework Basics

Laravel listening mode

Will you be punished for not wearing seat belts in the back row?

20 full knowledge maps of HD data analysis have been completed. It is strongly recommended to collect them!

大龄码农从北京到荷兰的躺平生活

leetcode684. Redundant connection (medium)

C# 启动一个外部exe文件,并传入参数

整了20张高清数据分析全知识地图,强烈建议收藏!
随机推荐
Laravel8 implementation of sign in function
为什么芯片设计也需要「匠人精神」?
After reading the book reading methods
Search and graph theory: Dijkstra finding the shortest path i-dijkstra (shortest path)
2022年R1快開門式壓力容器操作考試題庫及模擬考試
Analysis of time complexity and space complexity
2022熔化焊接与热切割上岗证题目及模拟考试
WGet command use
Project workspace creation steps - Zezhong ar automated test tool
Interview high frequency algorithm question --- longest palindrome substring
What is a generic? Why use generics? How do I use generics? What about packaging?
开关电源电路图及原理12v分析-详细版
(OJ assignment of Hunan University of science and Technology) problem g: pattern matching of strings
Go quick start of go language (I): the first go program
再聊数据中心网络
瑞吉外卖项目(三)员工管理业务开发
Laravel 8 realizes database backup through task scheduling
If you want to learn ArrayList well, it is enough to read this article
2022年危险化学品经营单位主要负责人考试模拟100题及模拟考试
09 Minimum Spanning Tree highway