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

2023-11-04

对于 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仍被忽略了。 

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

测试框架pytest教程(2)-用例依赖库-pytest-dependency 的相关文章

  • 是否可以在 pytest_generate_tests() 中使用固定装置?

    我有一些固定装置conftest py在实际测试功能中运行良好 但是 我想使用参数化一些测试pytest generate tests 基于其中一些装置的数据 我想做的 简化 conftest py my fixture returns a
  • 如何提高@patch和MagicMock语句的可读性和可维护性(避免长名称和字符串标识)?

    在我的测试代码中 我有很多样板表达式 Magic return 我还有很长的字符串来标识要模拟的函数的路径 重构期间不会自动替换字符串 我更愿意直接使用导入的函数 示例代码 from mock import patch MagicMock
  • 如何从测试内部访问 py.test capsys?

    py test 文档说我应该将 capsys 参数添加到我的测试方法中 但就我而言 这似乎不可能 class testAll unittest TestCase def setUp self self cwd os path abspath
  • 如何在 pytest 中测试类层次结构?

    我已经使用 pytest 一段时间了 并学会了喜欢参数化和固定装置 我第一次想测试一些具有分支继承结构的类 当然 我想为子类重用测试用例 假设我有以下包结构 mock pkg child py grandchild py parent py
  • 模拟导入失败

    我该如何制作import pkg失败moduleA py 我可以打补丁pkg如果从中导入某些内容则会失败 否则不会失败 test py import os import moduleA from unittest mock import p
  • 如何访问 pytest 夹具中的所有标记?

    我正在使用 pytest 我想用标记来标记我的测试 这些标记将指定固定装置要在驱动程序中加载哪个页面 这可以轻松地与行为上下文对象一起使用 但我找不到如何使用 pytest 来做到这一点 以这段代码为例 import pytest pyte
  • 我无法在 PyCharm 下运行 Django Python pytest

    pytest portal test session starts platform darwin Python 3 6 0 pytest 3 0 6 py 1 4 32 pluggy 0 4 0 Django settings confi
  • 由于 __init__ 构造函数而产生的 Pytest 集合警告

    我一直在使用 Pytest 和 Selenium Web 驱动程序自学测试自动化 我所有的测试函数都在一个名为测试网络 py 它位于名为的目录中tests 我将所有函数分开 并将它们放在一个名为的单独目录中的自己的文件中测试用例 例如 这就
  • 将固定装置传递给 pytest 中的测试类

    考虑以下伪代码来演示我的问题 import pytest pytest fixture def param1 return smth yield wilma pytest fixture def param2 return smth yie
  • 使用 pytest 动态控制测试顺序

    我想使用逻辑来控制测试的顺序 该逻辑将在它们已经运行时动态地重新排序它们 我的用例是这样的 我使用 xdist 并行化我的测试 每个测试都使用来自公共且有限池的外部资源 某些测试比其他测试使用更多的资源 因此在任何给定时间 当只有一小部分资
  • Pytest:如何使用从夹具返回的列表来参数化测试?

    我想使用由固定装置动态创建的列表来参数化测试 如下所示 pytest fixture def my list returning fixture depends on other fixtures return a dynamically
  • Django Channels Postgres InterfaceError:连接已关闭

    我似乎无法理解这里的问题 我正在为我的渠道消费者编写测试文档中的描述 https channels readthedocs io en latest topics testing html 我通常会使用 Django 默认的单元测试 但由于
  • pytest.raises(Error) 如何工作?

    对 Python 来说是新手 但我试图理解这段代码 with pytest raises ValueError group adjust vals grps 1 grps 2 weights 看完之后本教程与 http effbot org
  • 使用 Pytest 的参数化添加测试功能的描述

    当其中一个测试失败时 可以在测试正在测试的内容的参数化中添加描述 快速了解测试失败的原因 有时您不知道测试失败的原因 您必须查看代码 通过每个测试的描述 您就可以知道 例如 pytest mark parametrize num1 num2
  • Pytest报告摘要显示错误信息

    我对 pytest 挂钩和插件相对较新 我无法弄清楚如何让我的 pytest 代码为我提供测试执行摘要以及失败原因 考虑代码 class Foo def init self val self val val def test compare
  • pytest - 测试流程顺序

    我有一个类似于下面的 pytest 代码 如果我使用 count 3 运行它 它将运行 test first 3 次 然后运行 test second 3 次 如果我希望它运行 test first test second 并重复该流程怎么
  • Pytest:取消选择测试

    通过 pytest 我们可以使用装饰器来标记测试 pytest mark slow def some slow test pass 然后 从命令行 可以告诉 pytest 跳过标记为 慢 的测试 pytest k slow 如果我有一个附加
  • 当 py.test 测试失败时,PyCharm 是否可以进入调试状态

    运行测试时py test有一个 pdb失败时输入 pdb 的选项 从 PyCharm 中运行相同的测试时是否有类似的方法进入调试器 有一个 py test 插件 pytest pycharm https github com jlubcke
  • 一旦相关命令更改,如何自动运行 py.test?

    通过autonose或nosy 一旦某些测试文件或相关文件发生更改 它将自动运行nosetests 请问py test是否提供了类似的功能 有没有其他工具可以自动激发py test 您可以安装pytest xdist 插件 http pyp
  • 让tox使用pyenv设置的Python版本

    我似乎无法集中精力管理 Python 版本 当我跑步时tox 我可以立即看到它使用的是 Python 3 7 9 tox py39 commands 0 gt coverage run m pytest test session start

随机推荐

  • 海康录像机识别不到硬盘_海康威视硬盘录像机常见问题解决方式

    海康威视客户端 4 01 使用配置相关注意事项 1 安装客户端软件选择单机版还是网络版 目前 4 01 客户端分成 2 个版本 分别是单机版和网络版 单机版即以前的互相独立的分控模式 每个安装客户端的分控点分别独立的对设备进行操 作 客户端
  • linux安装yum报错Unable to locate package yum解决方案

    为什么80 的码农都做不了架构师 gt gt gt 问题 apt get install yum Reading package lists Done Building dependency tree Reading state infor
  • Flutter 实现九宫格抽奖动画效果

    一 本文实现的九宫格抽奖动画效果如下 二 主要分享下怎么一步一步来实现这个效果 源代码地址 布局可以通过GridView轻松实现 只需在数据源的第五个位置插入一个元素用来标识是开始按钮 抽奖动画的实现 需要按顺时针循环选中奖品而且还需要从慢
  • 小程序酷炫动态登录页源码(动态水滴)

    1 页面效果 登陆页面一般都要酷炫好看一点 这里分享一个动态登录页面 页面有三个流动的小水滴 一个水滴放登录框 剩下两个水滴跳转页面和打开弹窗 2 代码内容
  • opencv KCF追踪报错

    tracker gt update dstImage1 roi 刷新ROI的位置 在这一句报错 后来发现update 中的dstimage1没有经过处理 把之前对image进行的 转换颜色空间 二值化 开闭运算 canny运算 又进行一遍
  • ssh无法远程连接ubuntu系统,提示"System is booting up. See pam_nologin(8)"

    问题 使用ssh xshell或者putty 远程连接Linux ubuntu 系统时 提示 System is booting up See pam nologin 8 Connection closing Socket close 无法
  • 浅聊便利蜂

    便利蜂是一家以新型便利店为主体的科技创新零售企业 公司以科技为核心驱动运营 以 品质生活 便利中国 为己任 怀抱 小小幸福 在你身边 的初心 为中国消费者提供优质 健康 安心的产品和高效 便捷 满意的服务 便利蜂由北京自由蜂电子商务有限公司
  • 网络流dinic算法复杂度

    Dinic算法的时间复杂度的理论上界是O N2 M N是结点数 M是边数 但实际上Dinic算法比这个理论上界好得多 如果所有边容量均为1 那么时间复杂度是O min N0 67 M0 5 M 对于二分图最大匹配这样的特殊图 时间复杂度是O
  • antV-G2图标的label如何显示,如何自定义样式

    chart interval position 号位 平均分 这里开始设置label label 平均分 val gt return position middle offset 0 content originData gt return
  • 常见架构对比

    1 单体架构 概念 所有的模块集中在一个项目中 打包到一起并放在一个web容器中运行 适合场景 项目初创期 业务简单且响应要求高 优点 开发 测试 部署运维简单 响应快 缺点 1 资源没法隔离 2 部署周期长 3 可靠性低所有模块都集中在一
  • nestjs:docker build时执行npm install sharp提示downloading libvips socket hang up

    问题 如题 参考 sharp High performance Node js image processing 参考chinese mirror处理 原因 默认是从github上下载libvips库 但是使用socket协议 linux下
  • STM32 进阶教程 6 -  汇编与C混合编程

    前言 在嵌入式开发过程中 有时候为了追求代码性能与效率不得不采用汇编语言来编写代码 但是汇编代码在阅读时表现不如C语言友好 本节给大家介绍一种新的方法 关键代码用汇编语言时行实现 然后用C语言时行接封装 用C语言与汇编语言混合编程的方式 在
  • DELL T420塔式服务器RAID配置及OS安装

    一 DELL T420塔式服务器RAID配置 1 服务器RAID卡配置 开机看到CTRL R的提示后及时按下CTRL R进到阵列卡配置界面 进去之后光标默认就在阵列卡型号上 比如 PERC H310 PERC H710 PERC H710P
  • C++装饰者模式:Decorator Pattern

    设计原则 类应该对扩展开发 对修改关闭 装饰者可以在所委托被装饰者的行为之后加上自己的行为 以达到特定的目的 装饰者模式 动态地将责任附加到对象上 若要扩展功能 装饰者提供了比继承更有弹性的替代方案 装饰者和被装饰者必须是一样的类型 也就是
  • 多目标跟踪算法之SORT

    本文首发于微信公众号 DeepDriving 欢迎关注 简介 SORT是2016年发表的一篇文章 Simple Online and Realtime Tracking 中提出的一个经典的多目标跟踪算法 该算法结合常用的卡尔曼滤波器和匈牙利
  • 服务器128g内存显示64g,64g内存服务器

    64g内存服务器 内容精选 换一换 华为云帮助中心 为用户提供产品简介 价格说明 购买指南 用户指南 API参考 最佳实践 常见问题 视频帮助等技术文档 帮助您快速上手使用华为云服务 接口名称GetCloudPhoneServerModel
  • httprunner 3.x学习1 - 环境安装与准备

    前言 httprunner 3 x最大的改变是执行用例用的是 python 的 pytest 框架 支持3种格式的用例 YAML JSON pytest 代码 对比 httprunner 2 x 以前版本 早期版本用的是 unittest
  • el-select 默认值显示value不显示label问题

    el select 默认值显示value不显示label问题 在做编辑页面时 需要取到列表项数据回显默认值 在做select回显的时候遇到了这个问题 明明拿到的值和value中的值一样 但是就是不转换成label 这种情况一般出现在valu
  • 清除苹果服务器位置,苹果桌面三大清理工具_服务器评测与技术-中关村在线

    值得庆幸的是 Mac App Store的有三个应用程序 可以帮助你整理桌面 保证你的桌面整洁干净 前两者都是免费的 在桌面上扫描文件和文件夹时 只要一个命令 就能按照你的原设置整理文件夹 第三虽然是付费但是功能强大 能够隐藏你的文件 即保
  • 测试框架pytest教程(2)-用例依赖库-pytest-dependency

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