测试框架pytest教程(2)-用例依赖库-pytest-dependency

news2024/9/29 9:17:23

对于 pytest 的用例依赖管理,可以使用 pytest-dependency 插件。该插件提供了更多的依赖管理功能,使你能够更灵活地定义和控制测试用例之间的依赖关系。

Using pytest-dependency — pytest-dependency 0.5.1 documentation

安装 pytest-dependency 插件:

pip install pytest-dependency

基本使用

依赖方法和被依赖方法都需要使用装饰器 @pytest.mark.dependency

在依赖方法装饰器参数列表里填写依赖的用例名称列表

import pytest

@pytest.mark.dependency()
@pytest.mark.xfail(reason="deliberate fail")
def test_a():
    assert False

@pytest.mark.dependency()
def test_b():
    pass

@pytest.mark.dependency(depends=["test_a"])
def test_c():
    pass

@pytest.mark.dependency(depends=["test_b"])
def test_d():
    pass

@pytest.mark.dependency(depends=["test_b", "test_c"])
def test_e():
    pass

 执行结果:2个通过 3个忽略

被依赖的用例执行失败后,依赖的用例不执行,

a执行失败,所以c和e都被忽略了,a也被忽略了。

 为测试用例命名

使用name为测试用例命名,在依赖调用列表可以使用name调用。

import pytest

@pytest.mark.dependency(name="a")
@pytest.mark.xfail(reason="deliberate fail")
def test_a():
    assert False

@pytest.mark.dependency(name="b")
def test_b():
    pass

@pytest.mark.dependency(name="c", depends=["a"])
def test_c():
    pass

@pytest.mark.dependency(name="d", depends=["b"])
def test_d():
    pass

@pytest.mark.dependency(name="e", depends=["b", "c"])
def test_e():
    pass

测试类中的测试方法 

在 pytest 中,可以将测试用例分组到类中。对于测试类中的方法标记依赖关系的方式与简单的测试函数相同。在下面的示例中,我们定义了两个测试类。每个测试类的工作方式与之前的示例相同:

```python
import pytest

@pytest.mark.dependency
class TestClassA:
    def test_a(self):
        assert False

    @pytest.mark.dependency(depends=["TestClassA::test_a"])
    def test_b(self):
        assert True

@pytest.mark.dependency
class TestClassB:
    def test_c(self):
        assert False

    @pytest.mark.dependency(depends=["TestClassB::test_c"])
    def test_d(self):
        assert True
```

在这个示例中,我们定义了两个测试类 `TestClassA` 和 `TestClassB`。每个测试类中的方法都用 `@pytest.mark.dependency` 进行了标记,以指定它们的依赖关系。依赖关系通过传递类名和方法名来指定,格式为 `"TestClass::test_method"`。

这样,你就可以使用测试类来组织和管理测试用例,并使用 `@pytest.mark.dependency` 来标记它们之间的依赖关系。在运行测试时,pytest 将按照定义的依赖关系顺序执行测试方法。

参数化测试用例

import pytest

@pytest.mark.parametrize("x,y", [
    pytest.param(0, 0, marks=pytest.mark.dependency(name="a1")),
    pytest.param(0, 1, marks=[pytest.mark.dependency(name="a2"),
                              pytest.mark.xfail]),
    pytest.param(1, 0, marks=pytest.mark.dependency(name="a3")),
    pytest.param(1, 1, marks=pytest.mark.dependency(name="a4"))
])
def test_a(x,y):
    assert y <= x

@pytest.mark.parametrize("u,v", [
    pytest.param(1, 2, marks=pytest.mark.dependency(name="b1", 
                                                    depends=["a1", "a2"])),
    pytest.param(1, 3, marks=pytest.mark.dependency(name="b2", 
                                                    depends=["a1", "a3"])),
    pytest.param(1, 4, marks=pytest.mark.dependency(name="b3", 
                                                    depends=["a1", "a4"])),
    pytest.param(2, 3, marks=pytest.mark.dependency(name="b4", 
                                                    depends=["a2", "a3"])),
    pytest.param(2, 4, marks=pytest.mark.dependency(name="b5", 
                                                    depends=["a2", "a4"])),
    pytest.param(3, 4, marks=pytest.mark.dependency(name="b6", 
                                                    depends=["a3", "a4"]))
])
def test_b(u,v):
    pass

@pytest.mark.parametrize("w", [
    pytest.param(1, marks=pytest.mark.dependency(name="c1", 
                                                 depends=["b1", "b2", "b6"])),
    pytest.param(2, marks=pytest.mark.dependency(name="c2", 
                                                 depends=["b2", "b3", "b6"])),
    pytest.param(3, marks=pytest.mark.dependency(name="c3", 
                                                 depends=["b2", "b4", "b6"]))
])
def test_c(w):
    pass

运行时依赖

有时,测试实例的依赖关系太复杂,无法使用 pytest.mark.dependency() 标记在运行之前明确地进行公式化。在运行时编译测试的依赖关系列表可能更容易。在这种情况下,pytest_dependency.depends() 函数非常有用。考虑以下示例:

```python
import pytest
from pytest_dependency import depends

@pytest.mark.dependency
def test_a():
    assert False

@pytest.mark.dependency
def test_b():
    depends(test_a())
    assert True
```

在这个示例中,我们使用 pytest_dependency.depends() 函数定义了 test_b() 依赖于 test_a() 的关系。这样,我们可以在运行时根据 test_b() 的需要动态地编译依赖关系列表。

使用 pytest_dependency.depends() 函数时,只需将需要依赖的测试方法作为函数参数传递给它即可。

指明作用范围

scope的默认范围是module,所以基本使用的例子也可以写为如下,

实现效果没有区别,只是指明了范围

import pytest

@pytest.mark.dependency()
@pytest.mark.xfail(reason="deliberate fail")
def test_a():
    assert False

@pytest.mark.dependency()
def test_b():
    pass

@pytest.mark.dependency(depends=["test_a"], scope='module')
def test_c():
    pass

@pytest.mark.dependency(depends=["test_b"], scope='module')
def test_d():
    pass

@pytest.mark.dependency(depends=["test_b", "test_c"], scope='module')
def test_e():
    pass

跨模块需要指明范围为session

如果一个用例依赖的另一个用例在不同的模块,依赖的用例的scope必须是session或者是package。

# test_mod_01.py

import pytest

@pytest.mark.dependency()
def test_a():
    pass

@pytest.mark.dependency()
@pytest.mark.xfail(reason="deliberate fail")
def test_b():
    assert False

@pytest.mark.dependency(depends=["test_a"])
def test_c():
    pass


class TestClass(object):

    @pytest.mark.dependency()
    def test_b(self):
        pass

# test_mod_02.py

import pytest

@pytest.mark.dependency()
@pytest.mark.xfail(reason="deliberate fail")
def test_a():
    assert False

@pytest.mark.dependency(
    depends=["tests/test_mod_01.py::test_a", "tests/test_mod_01.py::test_c"],
    scope='session'
)
def test_e():
    pass

@pytest.mark.dependency(
    depends=["tests/test_mod_01.py::test_b", "tests/test_mod_02.py::test_e"],
    scope='session'
)
def test_f():
    pass

@pytest.mark.dependency(
    depends=["tests/test_mod_01.py::TestClass::test_b"],
    scope='session'
)
def test_g():
    pass

范围为class

测试依赖关系也可以在类范围的级别上定义。这仅适用于测试类中的方法,并将依赖限制为同一类中的其他测试方法。

import pytest

@pytest.mark.dependency()
@pytest.mark.xfail(reason="deliberate fail")
def test_a():
    assert False


class TestClass1(object):

    @pytest.mark.dependency()
    def test_b(self):
        pass


class TestClass2(object):

    @pytest.mark.dependency()
    def test_a(self):
        pass

    @pytest.mark.dependency(depends=["test_a"])
    def test_c(self):
        pass

    @pytest.mark.dependency(depends=["test_a"], scope='class')
    def test_d(self):
        pass

    @pytest.mark.dependency(depends=["test_b"], scope='class')
    def test_e(self):
        pass

 一组测试使用fixture

pytest 在测试用例中对 fixture 实例进行自动分组。如果有一组测试用例,并且需要针对每个测试用例运行一系列的测试,这将非常有用。

例如:

```python
import pytest

# 定义一个测试用例
@pytest.fixture(params=[1, 2, 3])
def test_case(request):
    return request.param

# 运行多次测验
def test_my_tests(test_case):
    assert test_case > 0

def test_other_tests(test_case):
    assert test_case < 10
```

在这个示例中,我们定义了一个名为 `test_case` 的 fixture,它使用 `@pytest.fixture` 装饰器和 `params` 参数来定义一个包含多个测试用例的列表。然后,我们使用 `test_case` fixture 来运行多个测试方法 `test_my_tests` 和 `test_other_tests`。pytest 会自动将这些测试方法与每个测试用例进行匹配,并为每个测试用例运行对应的测试方法。

通过这种方式,我们可以轻松地为每个测试用例执行一系列的测试,而不需要手动为每个测试用例编写独立的测试方法。

使用夹具为用例分组

pytest具有按夹具实例自动分组测试的功能。如果存在一组测试用例,并且对于每个测试用例都需要运行一系列的测试,这一特性尤其有用。



import pytest
from pytest_dependency import depends

@pytest.fixture(scope="module", params=range(1,10))
def testcase(request):
    param = request.param
    return param

@pytest.mark.dependency()
def test_a(testcase):
    if testcase % 7 == 0:
        pytest.xfail("deliberate fail")
        assert False

@pytest.mark.dependency()
def test_b(request, testcase):
    depends(request, ["test_a[%d]" % testcase])
    pass

if __name__ == '__main__':
    pytest.main(["-sv"])

 因为test_a[7]执行失败,所以test_b[7]被跳过。

 如果多个测试方法依赖于一个测试方法,则可以把pytest_dependency.depends()调用单独写一个fixture

import pytest
from pytest_dependency import depends

@pytest.fixture(scope="module", params=range(1,10))
def testcase(request):
    param = request.param
    return param

@pytest.fixture(scope="module")
def dep_testcase(request, testcase):
    depends(request, ["test_a[%d]" % testcase])
    return testcase

@pytest.mark.dependency()
def test_a(testcase):
    if testcase % 7 == 0:
        pytest.xfail("deliberate fail")
        assert False

@pytest.mark.dependency()
def test_b(dep_testcase):
    pass

@pytest.mark.dependency()
def test_c(dep_testcase):
    pass

test_b[7]和test_c[7] 会被跳过,因为test_a[7]失败了。

依赖参数化测试方法

如果一个测试同时依赖于一个参数化测试的所有实例,逐个列出它们在 pytest.mark.dependency() 标记中可能不是最佳解决方案。但是可以根据参数值动态地编译这些列表,如以下示例所示:

import pytest

def instances(name, params):
    def vstr(val):
        if isinstance(val, (list, tuple)):
            return "-".join([str(v) for v in val])
        else:
            return str(val)
    return ["%s[%s]" % (name, vstr(v)) for v in params]


params_a = range(17)

@pytest.mark.parametrize("x", params_a)
@pytest.mark.dependency()
def test_a(x):
    if x == 13:
        pytest.xfail("deliberate fail")
        assert False
    else:
        pass

@pytest.mark.dependency(depends=instances("test_a", params_a))
def test_b():
    pass

params_c = list(zip(range(0,8,2), range(2,6)))

@pytest.mark.parametrize("x,y", params_c)
@pytest.mark.dependency()
def test_c(x, y):
    if x > y:
        pytest.xfail("deliberate fail")
        assert False
    else:
        pass

@pytest.mark.dependency(depends=instances("test_c", params_c))
def test_d():
    pass

params_e = ['abc', 'def']

@pytest.mark.parametrize("s", params_e)
@pytest.mark.dependency()
def test_e(s):
    if 'e' in s:
        pytest.xfail("deliberate fail")
        assert False
    else:
        pass

@pytest.mark.dependency(depends=instances("test_e", params_e))
def test_f():
    pass

test_b, test_d, and test_f will be skipped because they depend on all instances of test_a, test_c, and test_e respectively, but test_a[13], test_c[6-5], and test_e[def] fail. The list of the test instances is compiled in the helper function instances(). 

缺点

依赖用例执行顺序

这个库非常依赖用例的执行顺序,如在执行被依赖方法时,发现被依赖的方法未被执行,依赖方法会被忽略。

import pytest



@pytest.mark.dependency()
def test_b():
    pass

@pytest.mark.dependency(depends=["test_a"])
def test_c():
    pass

@pytest.mark.dependency(depends=["test_b"])
def test_d():
    pass

@pytest.mark.dependency(depends=["test_b", "test_c"])
def test_e():
    pass
@pytest.mark.dependency()
@pytest.mark.xfail(reason="deliberate fail")
def test_a():
    assert True
if __name__ == '__main__':
    pytest.main(["-sv"])


这个例子最后执行a,但c,e仍被忽略了。 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/906096.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

ipad手写笔有必要买吗?开学便宜又好用电容笔推荐

苹果电容笔之所以能够被iPad用户广泛使用&#xff0c;很大程度上是因为其的优秀性能&#xff0c;具有着独特的重力压感功能。但苹果原装的电容笔&#xff0c;价格相对比较高&#xff0c;所以很多人&#xff0c;都选择了普通的平替电容笔。如今许多人都爱用iPad来画图或写笔记&a…

Go 数组

一、复合类型&#xff1a; 二、数组 如果要存储班级里所有学生的数学成绩&#xff0c;应该怎样存储呢&#xff1f;可能有同学说&#xff0c;通过定义变量来存储。但是&#xff0c;问题是班级有80个学生&#xff0c;那么要定义80个变量吗&#xff1f; 像以上情况&#xff0c;最…

攻防世界-command_execution

原题 解题思路 题目告诉了&#xff0c;这可以执行ping命令且没WAF&#xff0c;那就可以在ping命令后连接其他命令。 服务器一般使用Linux&#xff0c;在Linux中可使用“&”连接命令。 ping 127.0.0.1&find / -name "flag*" ping 127.0.0.1&cat /home/f…

Linux中shell脚本常用命令、条件语句与if、case语句

目录 一.shell脚本常用命令 1.1.echo命令 1.2.date命令 1.3.cal命令 1.4.tr命令 1.5.cut命令 1.6.sort命令 1.7.uniq命令 1.8.cat多行重定向 二.条件语句 2.1.条件测试&#xff08;三种测试方法&#xff09; 2.2.正整数值比较 2.3.字符串比较 2.4.逻辑测试 三.i…

深入了解 Java 中 Files 类的常用方法及抽象类的 final 修饰

文章目录 Files 类常用方法抽象类的 final 修饰 &#x1f389;欢迎来到Java学习路线专栏~深入了解 Java 中 Files 类的常用方法及抽象类的 final 修饰 ☆* o(≧▽≦)o *☆嗨~我是IT陈寒&#x1f379;✨博客主页&#xff1a;IT陈寒的博客&#x1f388;该系列文章专栏&#xff1a…

【C语言学习】二分法查找有序数组中的数

二分查找的基本原理 二分查找的基本逻辑就是每次找区间的中间数&#xff0c;然后与要查找的数进行比较&#xff0c;不断的缩小区间&#xff0c;最后区间中只剩一个数&#xff0c;即为要查找的数。如果不是&#xff0c;则没有该数。 二分查找只适用于有序数组 以数组中的数从左…

计算机视觉领域文献引用

Bag of freebies 炼丹白嫖加油包 Bag of freebies、致力于解决数据集中语义分布可能存在偏差的问题。在处理语义分布偏差问题时&#xff0c;一个非常重要的问题是不同类别之间存在数据不平衡的问题。 一、数据增强篇 Data Augmentation &#xff08;1&#xff09;图片像素调整…

安全模式进不去,解决方法在这!

“我想让电脑进入安全模式&#xff0c;但无论我怎么操作都无法进入。这是怎么回事呢&#xff1f;我怎么才能让电脑进入安全模式呢&#xff1f;请求帮助&#xff01;” 安全模式是Windows操作系统的一种启动选项&#xff0c;用于解决系统问题和故障。然而&#xff0c;有时候用户…

PON测试,“信”助力 | 信而泰测试解决方案浅析

PON介绍 一、什么是PON网络 PON是“Passive Optical Network”的缩写&#xff0c;是一种基于光纤的网络技术。PON网络通过单向的光信号传输来实现数据、语音和视频等信息的传输。PON网络可以支持多个传输速率和距离要求&#xff0c;因此广泛应用于FTTH、FTTB&#xff08;Fibe…

Three.js 实现模型分解,拆解效果

原理&#xff1a;通过修改模型材质的 x,y,z 轴坐标 positon.set( x,y,z) 来实现拆解&#xff0c;分解的效果。 注意&#xff1a;支持模型材质position 修改的材质类型为 type“Mesh” ,其他类型的材质修改了position 可能没有实际效果 在上一篇 Three.js加载外部glb,fbx,gltf…

金融市场中的机器学习;快手推出自研语言模型“快意”

&#x1f989; AI新闻 &#x1f680; OpenAI可能面临《纽约时报》的起诉&#xff0c;侵犯知识产权引发争议 摘要&#xff1a;OpenAI使用《纽约时报》的文章和图片来训练AI模型&#xff0c;违反了《纽约时报》的服务条款&#xff0c;可能面临巨大损失。此前&#xff0c;也有其…

冠达管理:定增获批后会大涨吗?

近年来&#xff0c;跟着我国资本商场的稳步发展&#xff0c;定向增发&#xff08;定增&#xff09;已成为不少上市公司的一种重要融资方法&#xff0c;其比较于揭露发行股票&#xff0c;更能够满足少量出资者的融资需求。然而&#xff0c;对于很多出资者来说&#xff0c;一个问…

游戏服务端性能测试

导语&#xff1a;近期经历了一系列的性能测试&#xff0c;涵盖了Web服务器和游戏服务器的领域。在这篇文章中&#xff0c;我将会对游戏服务端所做的测试进行详细整理和记录。需要注意的是&#xff0c;本文着重于记录&#xff0c;而并非深入的编程讨论。在这里&#xff0c;我将与…

Visual Studio 2022 你必须知道的实用调试技巧

目录 1、什么是bug&#xff1f; 2.调试是什么&#xff1f;有多重要&#xff1f; 2.1我们是如何写代码的&#xff1f; 2.2又是如何排查出现的问题的呢&#xff1f; ​编辑 2.3 调试是什么&#xff1f; 2.4调试的基本步骤 2.5Debug和Release的介绍 3.Windows环境调试介绍…

spark第四课

countByValue 数据源中相同的值有多少个,也就是WordCount countByKey 表的是键值对中的key出现了几次,与Value的值无关 不推荐collect,因为他是将数据放入内存,但是内存不够大的话,就容易崩,所以使用saveAsTextFile更好,直接放入磁盘. 保存成对象文件,需要序列化 启动了2个 J…

前端PWA应用的相关知识和基础Demo

一、什么是PWA应用&#xff1f; 1、PWA简介 ​ 渐进式Web应用&#xff08;Progressive Web App&#xff09;&#xff0c;简称PWA&#xff0c;是 Google 在 2015 年提出的一种使用web平台技术构建的应用程序&#xff0c;官方认为其核心在于Reliable&#xff08;可靠的&#xf…

修改窗口类的属性将影响所有该类的窗口

正如标题所指出的&#xff1a;窗口类的属性将影响所有由该窗口类创建的窗口。这就是它为什么称之为窗口类的原因。 我用这么多话来强调这一点&#xff0c;似乎是一件显而易见的事情&#xff0c;但我看到许多”解决方案”忽视了这个简单的事实。 在 WNDCLASS&#xff08;或 WND…

Autoware.universe与Carla(二进制版)联调

文章目录 一、Carla的使用1.1 二进制Carla的安装1.2 Carla API的使用1.2.1 增加行人流和车流1.2.3 改变服务器的时间&#xff0c;光照&#xff0c;天气1.2.3 手动控制车辆1.1.4 车辆的自动驾驶 二、Universe与Carla联调2.1 设置OpenPlanner2.2 重新编译并运行 本文介绍了 Autow…

Nginx反向代理配置流式响应

Nginx 是通过缓存响应内容来处理请求的。也就是说&#xff0c;当 Nginx 接收到完整的响应后&#xff0c;才会将其发送给客户端&#xff0c;所以默认不支持流式响应&#xff0c;这里讲讲 Nginx 反向代理中怎么配置流式响应&#xff1f; 一、使用背景 最近使用 Egg.js 搭建自动化…

冠达管理:非银金融是什么?

非银金融&#xff08;Non-banking Financial Institutions&#xff0c;简称非银&#xff09;是指除了传统的银行以外的其他金融机构。与银行不同的是&#xff0c;非银金融机构没有颁发钱银的权利&#xff0c;但在金融市场中发挥着重要的效果。在全球范围内&#xff0c;非银金融…