pytest.mark.dependency用例依赖

article/2025/8/26 17:07:27

这是一个pytest第三方插件,主要解决用例之间的依赖关系。如果依赖的上下文失败后续的用例会被标识为跳过执行,相当于执行了pytest.mark.skip。

1.安装

安装命令如下:

pip install pytest-dependency

执行上述命令后,再执行pip install pytest-dependency,能找到该组件即可
在这里插入图片描述

2.基本用法

第一步:需要在用例开始位置写上@pytest.mark.dependency(),代表这条用例作为主条件,如果这条用例失败,关联它的用例会跳过执行。
第二步:在被关联的用例上,也打上带参数的装饰器@pytest.mark.dependency(),参数depends可写关联的依赖用例名。在depends也可以用别名的方式指定用例名。

2.1函数中执行
我们先看在函数中执行:在test_01函数前我们加上了@pytest.mark.dependency(),在test_02函数前也写了@pytest.mark.dependency(depends=[“test_01”]),表示在执行test_02函数依赖test_01函数。

import pytest@pytest.mark.dependency()
def test_01():print('hello')assert 1 == 1@pytest.mark.dependency(depends=["test_01"])
def test_02():print('hello test_02')if __name__ == '__main__':pytest.main(['-sv'])

输出结果:

test_import_02.py::test_01 hello
PASSED
test_import_02.py::test_02 hello test_02
PASSED

若test_01函数断言改成2 == 1,test_01函数会报错,test_02函数会被跳过。输出如下所示:

=========================== short test summary info ===========================
FAILED test_import_02.py::test_01 - assert 2 == 1
================== 1 failed, 1 skipped, 2 warnings in 0.46s ===================

我们也可将test_01用别名,例如:

import pytest@pytest.mark.dependency(name="dep")
def test_01():print('hello')assert 1 == 1@pytest.mark.dependency(depends=["dep"])
def test_02():print('hello test_02')if __name__ == '__main__':pytest.main(['-sv'])

输出:

test_import_02.py::test_01 hello
PASSED
test_import_02.py::test_02 hello test_02
PASSED

若我们将函数test_01放在test_02之后执行,test_02函数执行被跳过。代码如下:

import pytest@pytest.mark.dependency(depends=["test_01"])
def test_02():print('function hello')assert 1 == 1@pytest.mark.dependency()
def test_01():print('function hello')assert 1 == 1if __name__ == '__main__':pytest.main(['-sv'])

输出如下:

test_import_02.py::test_02 SKIPPED (test_02 depends on test_01)
test_import_02.py::test_01 function hello
PASSED

2.2 在类中执行
在类中执行,不能直接写方法名,需要加上类名::方法名或者别名。为什么不能直接写方法名,后面会详细说明。代码如下:

import pytestclass Testdep():@pytest.mark.dependency()def test_01(self):print('cclass hello')assert 1 == 1@pytest.mark.dependency(depends=["Testdep::test_01"])def test_02(self):print('hello test_02')if __name__ == '__main__':pytest.main(['-sv'])

输出如下:

test_import_02.py::Testdep::test_01 cclass hello
PASSED
test_import_02.py::Testdep::test_02 hello test_02
PASSED

3.依赖范围

@pytest.mark.dependency(name=None,depends=[],scope=‘module’)的括号中参数说明如下:

  • name:依赖测试所引用的测试名称,如果未设置,则默认使用pytest定义的节点ID,名称需唯一
  • depends:依赖性,此测试所依赖的测试名称的列表
  • scope:搜索依赖的范围,必须是session,package,module或class,默认为module

3.1 依赖范围为:class

作用于所属的类,外部类不会被关联

import pytestdef test_02():print('function test_02')assert 1 == 1class Testdep2():@pytest.mark.dependency()def test_01(self):print('class test_01')assert 1 == 1@pytest.mark.dependency()def test_02(self):print('class test_02')class Testdep1():@pytest.mark.dependency()def test_01(self):print('class test_01')assert 1 == 1@pytest.mark.dependency(depends=["test_01"],scope='class')def test_02(self):print('class test_02')@pytest.mark.dependency()
def test_01():print('function test_01')assert 1 == 1if __name__ == '__main__':pytest.main(['-sv'])

类Testdep1的test_02方法只会在当前类中找test_01方法,输出如下:

test_import_02.py::test_02 function test_02
PASSED
test_import_02.py::Testdep2::test_01 class test_01
PASSED
test_import_02.py::Testdep2::test_02 class test_02
PASSED
test_import_02.py::Testdep1::test_01 class test_01
PASSED
test_import_02.py::Testdep1::test_02 class test_02
PASSED
test_import_02.py::test_01 function test_01
PASSED

我们将类Testdep1的test_01方法放到test_02后执行,test_02就会被跳过:
输出如下:

test_import_02.py::test_02 function test_02
PASSED
test_import_02.py::Testdep2::test_01 class test_01
PASSED
test_import_02.py::Testdep2::test_02 class test_02
PASSED
test_import_02.py::Testdep1::test_02 SKIPPED (test_02 depends on tes...)
test_import_02.py::Testdep1::test_01 class test_01
PASSED
test_import_02.py::test_01 function test_01
PASSED

3.2 依赖范围为:module

默认参数是’module’,作用于当前文件。只会查找当前文件的符合条件的文件名,类里同名的方法不会被依赖。代码如下:

import pytestdef test_02():print('function test_02')assert 1 == 1class Testdep2():@pytest.mark.dependency()def test_01(self):print('class test_01')assert 1 == 1@pytest.mark.dependency()def test_02(self):print('class test_02')class Testdep1():@pytest.mark.dependency()def test_01(self):print('class test_01')assert 1 == 1@pytest.mark.dependency(depends=["test_01"],scope='module')def test_02(self):print('class test_02')@pytest.mark.dependency()
def test_01():print('function test_01')assert 1 == 1if __name__ == '__main__':pytest.main(['-sv'])

输出如下:

test_import_02.py::test_02 function test_02
PASSED
test_import_02.py::Testdep2::test_01 class test_01
PASSED
test_import_02.py::Testdep2::test_02 class test_02
PASSED
test_import_02.py::Testdep1::test_01 class test_01
PASSED
test_import_02.py::Testdep1::test_02 SKIPPED (test_02 depends on tes...)
test_import_02.py::test_01 function test_01
PASSED

要想调用当前类或别的类,需使用别名或者使用类名::方法名
例如:

import pytestdef test_02():print('function test_02')assert 1 == 1class Testdep2():@pytest.mark.dependency()def test_01(self):print('class test_01')assert 1 == 1@pytest.mark.dependency()def test_02(self):print('class test_02')class Testdep1():@pytest.mark.dependency()def test_01(self):print('class test_01')assert 1 == 1@pytest.mark.dependency(depends=["Testdep2::test_01"],scope='module')def test_02(self):print('class test_02')@pytest.mark.dependency()
def test_01():print('function test_01')assert 1 == 1if __name__ == '__main__':pytest.main(['-sv'])

输出:

test_import_02.py::test_02 function test_02
PASSED
test_import_02.py::Testdep2::test_01 class test_01
PASSED
test_import_02.py::Testdep2::test_02 class test_02
PASSED
test_import_02.py::Testdep1::test_01 class test_01
PASSED
test_import_02.py::Testdep1::test_02 class test_02
PASSED
test_import_02.py::test_01 function test_01
PASSED

3.3 依赖范围为:package

作用于当前目录同级的依赖函数,跨目录无法找到依赖的函数。
例如在dep2目录下,创建了test_dep_01.py和test_dep_02.py.
在这里插入图片描述
test_dep_01.py的代码如下:

import pytestclass Testna():@pytest.mark.dependency()def test_01(self):print('function test_01')assert 1 == 1@pytest.mark.dependency()def test_02(self):print('function test_02')

test_dep_02.py的代码如下:

import pytestdef test_02():print('function test_02')assert 1 == 1class Testdep1():@pytest.mark.dependency()def test_01(self):print('class test_01')assert 1 == 1@pytest.mark.dependency(depends=["test_dep_01.py::Testna::test_01"], scope='package')def test_02(self):print('class test_02')

输出结果为:

D:\pythonProject\dependency\dep\dep2>pytest -sv
======================================================================== test session starts ========================================================================
platform win32 -- Python 3.7.6, pytest-6.2.4, py-1.10.0, pluggy-0.13.1 -- d:\python3.7.6\python.exe
cachedir: .pytest_cache
rootdir: D:\pythonProject\dependency\dep\dep2
plugins: allure-pytest-2.9.41, dependency-0.5.1
collected 6 items                                                                                                                                                    test_dep_01.py::Testna::test_01 function test_01
PASSED
test_dep_01.py::Testna::test_02 function test_02
PASSED
test_dep_02.py::test_02 function test_02
PASSED
test_dep_02.py::Testdep1::test_01 class test_01
PASSED
test_dep_02.py::Testdep1::test_02 class test_02
PASSED
test_dep_02.py::test_01 function test_01
PASSED========================================================================= 6 passed in 0.05s =========================================================================

3.4 依赖范围为:session

作用域全局,可跨目录调用。但被依赖的用例必须先执行
例如结构如下:
在这里插入图片描述

在dep1目录下创建了test_dep_01.py,代码如下:

import pytestclass Testna():@pytest.mark.dependency()def test_01(self):print('function test_01')assert 1 == 1@pytest.mark.dependency()def test_02(self):print('function test_02')

在dep2目录下创建了test_dep_02.py,代码如下:

import pytestdef test_02():print('function test_02')assert 1 == 1class Testdep1():@pytest.mark.dependency()def test_01(self):print('class test_01')assert 1 == 1@pytest.mark.dependency(depends=["dep1/test_dep_01.py::Testna::test_01"], scope='session')def test_02(self):print('class test_02')@pytest.mark.dependency()
def test_01():print('function test_01')assert 1 == 1

在上一级目录dep下执行

D:\pythonProject\dependency\dep>pytest -sv
======================================================================== test session starts ========================================================================
platform win32 -- Python 3.7.6, pytest-6.2.4, py-1.10.0, pluggy-0.13.1 -- d:\python3.7.6\python.exe
cachedir: .pytest_cache
rootdir: D:\pythonProject\dependency\dep
plugins: allure-pytest-2.9.41, dependency-0.5.1
collected 6 items                                                                                                                                                    dep1/test_dep_01.py::Testna::test_01 function test_01
PASSED
dep1/test_dep_01.py::Testna::test_02 function test_02
PASSED
dep2/test_dep_02.py::test_02 function test_02
PASSED
dep2/test_dep_02.py::Testdep1::test_01 class test_01
PASSED
dep2/test_dep_02.py::Testdep1::test_02 class test_02
PASSED
dep2/test_dep_02.py::test_01 function test_01
PASSED

在dep2目录下执行,结果如下:

D:\pythonProject\dependency\dep\dep2>pytest -sv
======================================================================== test session starts ========================================================================
platform win32 -- Python 3.7.6, pytest-6.2.4, py-1.10.0, pluggy-0.13.1 -- d:\python3.7.6\python.exe
cachedir: .pytest_cache
rootdir: D:\pythonProject\dependency\dep\dep2
plugins: allure-pytest-2.9.41, dependency-0.5.1
collected 4 items                                                                                                                                                    test_dep_02.py::test_02 function test_02
PASSED
test_dep_02.py::Testdep1::test_01 class test_01
PASSED
test_dep_02.py::Testdep1::test_02 SKIPPED (test_02 depends on dep1/test_dep_01.py::Testna::test_01)
test_dep_02.py::test_01 function test_01
PASSED=================================================================== 3 passed, 1 skipped in 0.04s ====================================================================

http://chatgpt.dhexx.cn/article/OQcPCV3m.shtml

相关文章

dependencyManagement使用简介

dependencyManagement使用简介 Maven中的dependencyManagement元素提供了一种管理依赖版本号的方式。在dependencyManagement元素中声明所依赖的jar包的版本号等信息,那么所有子项目再次引入此依赖jar包时则无需显式的列出版本号。Maven会沿着父子层级向上寻找拥有…

Dependency-check

文章目录 前言工具简介工具原理原理检测过程NVDCVSS 工具安装工具地址环境依赖工具安装Jenkins PluginCommand LineOn *nixOn WindowsOn Mac (rec) Maven PluginAnt Task 使用示例命令行方式(Mac)插件方式 报告解读CSV格式报告HTML格式报告 工具对比参考 前言 公司…

maven中dependency的属性(依赖)配置

groupId,artfactId,version,type,classifier,scope,systemPath,exclusions,optional 是 maven的9种依赖属性, 其中groupId,artfactId,version是三…

Maven -- dependency详解

PS&#xff1a;部分来源官网文档&#xff0c;翻译不到位&#xff0c;请移步官网 一 &#xff1a;type&#xff1a;个人理解&#xff1a;依赖<dependency>通过其子标签 定位了某个特定的唯一构件&#xff0c;所以type--依赖类型&#xff0c;更准确的说应该是依赖的构件…

MySQL中“full outer join“的实现

一: 先创建两个表 二: 使用【left join】 union 【right join】 select t1.dim_a, t1.qty qty_a, t2.dim_a dim_b, t2.qty qty_b from ta t1 left join tb t2 on t1.dim_at2.dim_a union select t1.dim_a, t1.qty qty_a, t2.dim_a dim_b, t2.qty qty_b from ta t1 right join…

left join 和 left outer join 的区别

通俗的讲&#xff1a; A left join B 的连接的记录数与A表的记录数同 A right join B 的连接的记录数与B表的记录数同 A left join B 等价B right join A table A: Field_K, Field_A 1 a 3 b 4 c table B: …

SQL中inner join、outer join和cross join的区别

缺省情况下是inner join,开发中使用的left join和right join属于outer join,另外outer join还包括full join.下面我通过图标让大家认识它们的区别。 现有两张表&#xff0c;Table A 是左边的表。Table B 是右边的表。其各有四条记录&#xff0c;其中有两条记录name是相同的&…

SQL Server中CROSS APPLY和OUTER APPLY应用

1.Cross Apply和Outer Apply的理解 新增的APPLY表运算符把右表表达式应用到左表表达式中的每一行。 它不像JOIN那样先计算哪个表表达式都可以&#xff0c;APPLY必选先逻辑地计算左表达式。这种计算输入的逻辑顺序允许把右表达式关联到左表表达式。 APPLY有两种形式&#xff0…

深夜学习:有关Inner、Outer等相关词汇的理解

快速链接: . 👉👉👉 个人博客笔记导读目录(全部) 👈👈👈 付费专栏-付费课程 【购买须知】:【精选】ARMv8/ARMv9架构入门到精通-[目录] 👈👈👈引流关键词: 内存屏障, DSB,DMB,ISB,inner,outer,memory barrier,Non-cacheable,Cacheable, non-shareable,inner…

外连接(OUTER JOIN)

9.3.4 外连接&#xff08;OUTER JOIN&#xff09; 不管是内连接还是带WHERE子句的多表查询&#xff0c;都组合自多个表&#xff0c;并生成结果表。换句话说&#xff0c;如果任何一个源表中的行在另一个源表中没有匹配&#xff0c;DBMS将不把该行放在最后的结果表中。 而外连…

CROSS APPLY和OUTER APPLY的区别

CROSS APPLY和OUTER APPLY的区别 APPLY语法 在SQL中&#xff0c;有这样的一种查询方式&#xff0c;APPLY语法&#xff1a;微软添加了这个新的运算符用于关联一个带有函数的结果集&#xff0c;并把函数应用于表/视图中的每一个限定行中。这个运算符就是APPLY&#xff0c;APPLY的…

图解inner join和outer join的区别

一张图可以完美诠释区别&#xff1a;

lateral view 和 lateral view outer的区别

lateral view 为侧视图&#xff0c;一般用于行转列的处理中 通常会与explode 搭配使用 1、explode 可以拆分map or array 示例 select explode(split("1,2,3",,)) 结果 123 explode 不适用于多个字段 其中一个字段需要炸开的场景&#xff0c;此时需要搭配late…

left join和left outer join 有什么区别?这样解释通俗易懂

作为一名java程序员&#xff0c;求职面试时也会遇到mysql问题&#xff0c;有次我到一家软件面试&#xff0c;就遇到有一道笔试题是考核关于sql 的。 left join和left outer join 有什么区别&#xff1f; 关于left join我平时编写sql表关联时用得比较多&#xff0c;但对于left o…

Oracle 数据库中 FULL OUTER JOIN 的作用 ?

当需要同时显示两个表中所有记录时&#xff0c;FULL OUTER JOIN 就非常有用。 FULL OUTER JOIN 返回左表&#xff08;Table A&#xff09;和右表&#xff08;Table B&#xff09;的所有行&#xff0c;并且如果左表或右表中没有匹配的行&#xff0c;则使用 NULL 值填充缺失的部分…

四种连接类型:inner(内连接),left[outer](左外连接),right[outer](右外连接),full[outer](完全外连接)

在from子句中表示连接操作有四种&#xff1a;inner&#xff08;内连接&#xff09;&#xff0c;left[outer]&#xff08;左外连接&#xff09;&#xff0c;right[outer]&#xff08;右外连接&#xff09;&#xff0c;full[outer]&#xff08;完全外连接&#xff09;。 一、理论…

内连接(inner join)与外连接(outer join)小结

转载自 : 内连接&#xff08;inner join&#xff09;与外连接(outer join)小结_蝉 沐 风的博客-CSDN博客_inner join mySQL包含两种联接&#xff0c;分别是内连接(inner join)和外连接(out join),但我们又同时听说过左连接&#xff0c;交叉连接等术语&#xff0c;本文旨在总结这…

np.dot、np.outer、np.matmul、np.multipy、np.inner、np.outer与np.cross几个函数之间的区别

np.dot、np.outer、np.matmul、np.multipy、np.inner、np.outer与np.cross几个函数之间的区别 一、数学上关于【内积】、【外积】的定义和计算1.1 数学上关于【内积/数量积】的定义和计算1.2 数学上关于【外积/叉积】的定义和计算 二、numpy中关于np.dot、np.outer、np.matmul、…

R语言 作图 outer()函数

outer()函数&#xff1a; outer(x,y,func):数组x与y的outer()函数会产生一个二维数组A&#xff0c;它的形状是 c(dim(x),dim(y)).A中对应元素A[x.index,y.index] func(x.index,y.index) outer(x,y):默认为两向量外积,即outer(x,y,x*y) x (x1,x2,x3),y (y1,y2,y3) #绘制曲…

[SPI]SPI协议详解

1.SPI协议简介 1.1.SPI协议概括 SPI&#xff0c;是英语Serial Peripheral interface的缩写&#xff0c;顾名思义就是串行外围设备接口。是Motorola首先在其MC68HCXX系列处理器上定义的。SPI接口主要应用在 EEPROM&#xff0c;FLASH&#xff0c;实时时钟&#xff0c;AD转换器&a…