Python测试框架Pytest的参数化详解

上篇博文介绍过,Pytest是目前比较成熟功能齐全的测试框架,使用率肯定也不断攀升。

在实际工作中,许多测试用例都是类似的重复,一个个写最后代码会显得很冗余。这里,我们来了解一下@pytest.mark.parametrize装饰器,可以很好解决上述问题。

源代码分析

  1. def parametrize(self,argnames, argvalues, indirect=False, ids=None, scope=None):

  2.   """ Add new invocations to the underlying test function using the list

  3.     of argvalues for the given argnames. Parametrization is performed

  4.   during the collection phase. If you need to setup expensive resources

  5. see about setting indirect to do it rather at test setup time.  # 使用给定argnames的argValue列表向基础测试函数添加新的调用,在收集阶段执行参数化。

  6. :arg argnames: a comma-separated string denoting one or more argument

  7. names, or a list/tuple of argument strings.  # 参数名:使用逗号分隔的字符串,列表或元祖,表示一个或多个参数名

  8. :arg argvalues: The list of argvalues determines how often a

  9. test is invoked with different argument values. If only one

  10. argname was specified argvalues is a list of values. If N

  11. argnames were specified, argvalues must be a list of N-tuples,

  12. where each tuple-element specifies a value for its respective

  13. argname.  # 参数值:只有一个argnames,argvalues则是值列表。有N个argnames时,每个元祖对应一组argnames,所有元祖组合成一个列表

  14. :arg indirect: The list of argnames or boolean. A list of arguments'

  15. names (self,subset of argnames). If True the list contains all names from

  16. the argnames. Each argvalue corresponding to an argname in this list will

  17. be passed as request.param to its respective argname fixture

  18. function so that it can perform more expensive setups during the

  19. setup phase of a test rather than at collection time.

  20. :arg ids: list of string ids, or a callable.

  21. If strings, each is corresponding to the argvalues so that they are

  22. part of the test id. If None is given as id of specific test, the

  23. automatically generated id for that argument will be used.

  24. If callable, it should take one argument (self,a single argvalue) and return

  25. a string or return None. If None, the automatically generated id for that

  26. argument will be used.

  27. If no ids are provided they will be generated automatically from

  28. the argvalues.  # ids:字符串列表,可以理解成标题,与用例个数保持一致

  29. :arg scope: if specified it denotes the scope of the parameters.

  30. The scope is used for grouping tests by parameter instances.

  31. It will also override any fixture-function defined scope, allowing

  32. to set a dynamic scope using test context or configuration.  

  33.   # 如果指定,则表示参数的范围。作用域用于按参数实例对测试进行分组。

  34.    它还将覆盖任何fixture函数定义的范围,允许使用测试上下文或配置设置动态范围。

  35. """

argnames

释义:参数名称。

格式:字符串"arg1,arg2,arg3"。

aegvalues

释义:参数值列表。

格式:必须是列表,如[val1,val2,val3]。

  • 单个参数,里面是值的列表,如@pytest.mark.parametrize("name",["Jack","Locus","Bill"]);

  • 多个参数,需要用元祖来存放值,一个元祖对应一组参数的值,如@pytest.mark.parametrize("user,age",[("user1",15),("user2",24),("user3",25)])。

ids

释义:可以理解为用例的id。

格式:字符串列表,如["case1","case2","case3"]。

indirect

释义:当indirect=True时,若传入的argnames是fixture函数名,此时fixture函数名将成为一个可执行的函数,argvalues作为fixture的参数,执行fixture函数,最终结果再存入request.param。

当indirect=False时,fixture函数只作为一个参数名给测试收集阶段调用。

备注:这里可以将the setup phase(测试设置阶段)理解为配置 conftest.py 阶段,将the collection phase(测试收集阶段)理解为用例执行阶段。

装饰测试类

  1. import pytest

  2. data = [

  3. (2,2,4),

  4. (3,4,12)

  5. ]

  6. def add(a,b):

  7. return a * b

  8. @pytest.mark.parametrize('a,b,expect',data)

  9. class TestParametrize(object):

  10. def test_parametrize_1(self,a,b,expect):

  11. print('\n测试函数1测试数据为\n{}-{}'.format(a,b))

  12. assert add(a,b) == expect

  13. def test_parametrize_2(self,a,b,expect):

  14. print('\n测试函数2测试数据为\n{}-{}'.format(a,b))

  15. assert add(a,b) == expect

  16. if __name__ == "__main__":

  17. pytest.main(["-s","test_07.py"])

 
  1. ============================= test session starts =============================

  2. platform win32 -- Python 3.8.0, pytest-6.2.5, py-1.11.0, pluggy-1.0.0

  3. rootdir: D:\AutoCode

  4. plugins: html-3.1.1, metadata-1.11.0

  5. collecting ... collected 4 items

  6. test_07.py::TestParametrize::test_parametrize_1[2-2-4]

  7. 测试函数1测试数据为

  8. 2-2

  9. PASSED

  10. test_07.py::TestParametrize::test_parametrize_1[3-4-12]

  11. 测试函数1测试数据为

  12. 3-4

  13. PASSED

  14. test_07.py::TestParametrize::test_parametrize_2[2-2-4]

  15. 测试函数2测试数据为

  16. 2-2

  17. PASSED

  18. test_07.py::TestParametrize::test_parametrize_2[3-4-12]

  19. 测试函数2测试数据为

  20. 3-4

  21. PASSED

  22. ============================== 4 passed in 0.12s ==============================

  23. Process finished with exit code 0

由以上代码可以看到,当装饰器装饰测试类时,定义的数据集合会被传递给类的所有方法。

装饰测试函数

单个数据

  1. import pytest

  2. data = ["Rose","white"]

  3. @pytest.mark.parametrize("name",data)

  4. def test_parametrize(name):

  5. print('\n列表中的名字为\n{}'.format(name))

  6. if __name__ == "__main__":

  7. pytest.main(["-s","test_07.py"])

 
  1. ============================= test session starts =============================

  2. platform win32 -- Python 3.8.0, pytest-6.2.5, py-1.11.0, pluggy-1.0.0

  3. rootdir: D:\AutoCode

  4. plugins: html-3.1.1, metadata-1.11.0

  5. collected 2 items

  6. test_07.py

  7. 列表中的名字为

  8. Rose

  9. .

  10. 列表中的名字为

  11. white

  12. .

  13. ============================== 2 passed in 0.09s ==============================

  14. Process finished with exit code 0

当测试用例只需要一个参数时,我们存放数据的列表无序嵌套序列,@pytest.mark.parametrize("name", data) 装饰器的第一个参数也只需要一个变量接收列表中的每个元素,第二个参数传递存储数据的列表,那么测试用例需要使用同名的字符串接收测试数据(实例中的name)且列表有多少个元素就会生成并执行多少个测试用例。

一组数据

  1. import pytest

  2. data = [

  3. [1, 2, 3],

  4. [4, 5, 9]

  5. ] # 列表嵌套列表

  6. # data_tuple = [

  7. # (1, 2, 3),

  8. # (4, 5, 9)

  9. # ] # 列表嵌套元组

  10. @pytest.mark.parametrize('a, b, expect', data)

  11. def test_parametrize_1(a, b, expect): # 一个参数接收一个数据

  12. print('\n测试数据为\n{},{},{}'.format(a, b, expect))

  13. actual = a + b

  14. assert actual == expect

  15. @pytest.mark.parametrize('value', data)

  16. def test_parametrize_2(value): # 一个参数接收一组数据

  17. print('\n测试数据为\n{}'.format(value))

  18. actual = value[0] + value[1]

  19. assert actual == value[2]

  20. if __name__ == "__main__":

  21. pytest.main(["-s","test_07.py"])

 
  1. ============================= test session starts =============================

  2. platform win32 -- Python 3.8.0, pytest-6.2.5, py-1.11.0, pluggy-1.0.0

  3. rootdir: D:\AutoCode

  4. plugins: html-3.1.1, metadata-1.11.0

  5. collected 4 items

  6. test_07.py

  7. 测试数据为

  8. 1,2,3

  9. .

  10. 测试数据为

  11. 4,5,9

  12. .

  13. 测试数据为

  14. [1, 2, 3]

  15. .

  16. 测试数据为

  17. [4, 5, 9]

  18. .

  19. ============================== 4 passed in 0.09s ==============================

  20. Process finished with exit code 0

当测试用例需要多个数据时,我们可以使用嵌套序列(嵌套元组&嵌套列表)的列表来存放测试数据。

装饰器@pytest.mark.parametrize()可以使用单个变量接收数据,也可以使用多个变量接收,同样,测试用例函数也需要与其保持一致。

当使用单个变量接收时,测试数据传递到测试函数内部时为列表中的每一个元素或者小列表,需要使用索引的方式取得每个数据。当使用多个变量接收数据时,那么每个变量分别接收小列表或元组中的每个元素列表嵌套多少个多组小列表或元组,测生成多少条测试用例。

组合数据

  1. import pytest

  2. data_1 = [1,2,3]

  3. data_2 = ['a','b']

  4. @pytest.mark.parametrize('a',data_1)

  5. @pytest.mark.parametrize('b',data_2)

  6. def test_parametrize_1(a,b):

  7. print(f'笛卡尔积测试结果为:{a},{b}')

  8. if __name__ == '__main__':

  9. pytest.main(["-vs","test_06.py"])

 通过测试结果,我们不难分析,一个测试函数还可以同时被多个参数化装饰器装饰,那么多个装饰器中的数据会进行交叉组合的方式传递给测试函数,进而生成n * n个测试用例。

标记用例

  1. import pytest

  2. @pytest.mark.parametrize("test_input,expected",[

  3. ("3+5",8),

  4. ("2+4",6),

  5. pytest.param("6 * 9",42,marks=pytest.mark.xfail),

  6. pytest.param("6 * 6",42,marks=pytest.mark.skip)

  7. ])

  8. def test_mark(test_input,expected):

  9. assert eval(test_input) == expected

  10. if __name__ == '__main__':

  11. pytest.main(["-vs","test_06.py"])

 输出结果显示收集到4个用例,两个通过,一个被跳过,一个标记失败:

  • 当我们不想执行某组测试数据时,我们可以标记skip或skipif;

  • 当我们预期某组数据会执行失败时,我们可以标记为xfail等。

嵌套字典

  1. import pytest

  2. data = (

  3. {

  4. 'user': "name1",

  5. 'pwd': 123

  6. },

  7. {

  8. 'user': "name2",

  9. 'pwd': 456

  10. }

  11. )

  12. @pytest.mark.parametrize('dic',data)

  13. def test_parametrize(dic):

  14. print('\n测试数据为\n{}'.format(dic))

  15. if __name__ == '__main__':

  16. pytest.main(["-vs","test_06.py"])

增加测试结果可读性

参数化装饰器有一个额外的参数ids,可以标识每一个测试用例,自定义测试数据结果的显示,为了增加可读性,我们可以标记每一个测试用例使用的测试数据是什么,适当的增加一些说明。

在使用前你需要知道,ids参数应该是一个字符串列表,必须和数据对象列表的长度保持一致。

  1. import pytest

  2. data_1 = [

  3. (1, 2, 3),

  4. (4, 5, 9)

  5. ]

  6. ids = ["a:{} + b:{} = expect:{}".format(a, b, expect) for a, b, expect in data_1]

  7. def add(a, b):

  8. return a + b

  9. @pytest.mark.parametrize('a, b, expect', data_1, ids=ids)

  10. class TestParametrize(object):

  11. def test_parametrize_1(self, a, b, expect):

  12. print('\n测试函数1测试数据为\n{}-{}'.format(a, b))

  13. assert add(a, b) == expect

  14. def test_parametrize_2(self, a, b, expect):

  15. print('\n测试函数2数据为\n{}-{}'.format(a, b))

  16. assert add(a, b) == expect

  17. if __name__ == '__main__':

  18. pytest.main(["-v","test_06.py"])

  • 不加ids参数的返回结果:

  • 加ids参数的返回结果:

我们可以看到带ids参数的返回结果中的用例都被一个列表明确的标记了,而且通过这种标记可以更加直观的看出来,每个测试用例使用的数据名称及测试内容。

行动吧,在路上总比一直观望的要好,未来的你肯定会感 谢现在拼搏的自己!如果想学习提升找不到资料,没人答疑解惑时,请及时加入扣群: 320231853,里面有各种软件测试+开发资料和技术可以一起交流学习哦。

最后感谢每一个认真阅读我文章的人,礼尚往来总是要有的,虽然不是什么很值钱的东西,如果你用得到的话可以直接拿走:

这些资料,对于【软件测试】的朋友来说应该是最全面最完整的备战仓库,这个仓库也陪伴上万个测试工程师们走过最艰难的路程,希望也能帮助到你!

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

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

karateclub,一个超酷的 Python 库!

更多资料获取 📚 个人网站:ipengtao.com 大家好,今天为大家分享一个超酷的 Python 库 - karateclub。 Github地址:https://github.com/benedekrozemberczki/karateclub Python karateclub是一个用于图嵌入和图聚类的库&#xff…

git commit 提交报错pre-commit hook failed (add --no-verify to bypass) 解决方法,亲测有效

问题截图 今天在执行 git commit 命令时报错:pre-commit hook failed (add --no-verify to bypass) 解决 参考文章:git commit报错:pre-commit hook failed的解决方法 具体原理什么的就不解释了,可以看看上面的参考文章 解决方…

如何使用高德地图的 Loca 展示 gpx 文件的 3D 路径,Loca.LineLayer

如何使用高德地图的 Loca 展示 gpx 文件的 3D 路径,Loca.LineLayer 找寻了好久,终于将这个展示 3D 路径的功能实现了。 在线实例: http://kylebing.cn/tools/map/#/gpx/gpx-viewer-3d 这里是用于展示 gpx 路径,关于 gpx 的相关知…

Linux migrate_type进一步探索

文章接着上回Linux migrate_type初步探索 1、物理页面添加到buddy系统 我们都知道物理内存一开始是由memblock进行分配管理,后面会切换到buddy系统管理。那么接下来我们看一下,memblock管理的物理页面是怎么添加到buddy系统中的。 start_kernel() -&g…

液晶数显式液压万能试验机WES-300B

一、简介 主机为两立柱、两丝杠、油缸下置式,拉伸空间位于主机的上方,压缩、弯曲试验空间位于主机下横梁和工作台之间。测力仪表采用高清液晶显示屏,实验数据方便直观。 主要性能技术指标 最大试验力(kN) 300 试…

文件删了,回收站清空了怎么恢复?文件恢复软件一览

在日常生活和工作中,我们常常会遇到误删除文件的情况,有时甚至会因为清空了回收站而无法找回这些文件。这些文件可能包含重要的工作数据、个人照片或其他珍贵的回忆。那么,在这种情况下,我们该如何恢复这些被删除且清空回收站的文…

外婆传(封家香传)

余乃民国三十载(公元一千九百四十一)九月初九重阳佳节日出生于衡阳县长塘村封谷里。父封盖梅,在民国二十九年(公元一千九百四十)驾鹤西归,遗世独立,吾未能见其颜。母氏,因丧夫之痛&a…

C++ | Leetcode C++题解之第59题螺旋矩阵II

题目&#xff1a; 题解&#xff1a; class Solution { public:vector<vector<int>> generateMatrix(int n) {int num 1;vector<vector<int>> matrix(n, vector<int>(n));int left 0, right n - 1, top 0, bottom n - 1;while (left < r…

微信公众号排名 SEO的5个策略

随着微信公众号在社交媒体领域的持续发展和普及&#xff0c;如何提升公众号的搜索排名&#xff0c;成为许多运营者关注的焦点。公众号排名SEO&#xff0c;即针对微信公众号进行搜索引擎优化&#xff0c;旨在提高公众号在搜索结果中的曝光率和点击率。下面&#xff0c;我们将深入…

python学习笔记-01

python 在学习之前要了解的事项&#xff1a; 1.python缩进语法要求较为严格 2.是解释型语言 3.python2版本和python3版本不兼容 本系列笔记全部基于python3 1.hello world 安装好python之后&#xff0c;可以直接打开python&#xff0c;也可以通过cmd进入python。 print(&qu…

kafka日志存储

前言 kafka的主题(topic)可以对应多个分区(partition)&#xff0c;而每个分区(partition)可以有多个副本(replica)&#xff0c;我们提生产工单创建topic的时候也是要预设这些参数的。但是它究竟是如何存储的呢&#xff1f;我们在使用kafka发送消息时&#xff0c;实际表现是提交…

Flutter分模块开发、模块可单独启动、包含Provider

前言 目前Flutter都是在一个项目中&#xff0c;创建不同目录进行模块开发&#xff0c;我进行Android原生开发时&#xff0c;发现原生端&#xff0c;是可以将每个模块独立运行起来的&#xff0c;灵感来自这&#xff1b; 折腾了几天&#xff0c;终于给整出来了。 1、创建根目录…

Go语言基本语法(三)指针

什么是指针 在Go语言中&#xff0c;"指针是一种存储变量内存地址的数据类型"&#xff0c;意味着指针本身是一个特殊的变量&#xff0c;它的值不是数据本身&#xff0c;而是另一个变量在计算机内存中的位置&#xff08;地址&#xff09;。形象地说&#xff0c;就像存…

multipass和multipassd命令的区别

multipassd通常是multipass服务的后台守护进程&#xff0c;它负责管理和控制虚拟机实例。 命令区别 例&#xff1a; multipass restart my-vm 这个命令用于重启Multipass中的虚拟机实例。例如有一个名为my-vm的虚拟机实例。 multipassd restart 这会重新启动Multipass后台…

一文学会最强大的 node.js 后端框架 nest.js

文章目录 nest cli项目基本结构IOC & DI基础注册值注册时 key 的管理动态注册类工厂函数方式注册设置别名导出 provider 模块功能模块模块的导入导出模块类中使用注入全局模块动态模块 中间件定义中间件注册中间件MiddlewareConsumer 类全局中间件 异常过滤器抛出异常自定义…

华三配置DHCP(基础)

华三交换机配置DHCP&#xff08;基础&#xff09; 1.组网拓扑图&#xff08;交换机-PC&#xff09; 2.通过交换机开启DHCP功能&#xff0c;使PC自动获取192.168.10.0&#xff08;vlan10&#xff09;网段地址 2.使用命令 <H3C>system-view [H3C]vlan 10&#xff08;建立…

Python_4-远程连接Linux

文章目录 使用Python通过SSH自动化Linux主机管理代码执行ls结果&#xff1a;文件传输&#xff1a; 使用Python通过SSH自动化Linux主机管理 在系统管理与自动化运维中&#xff0c;SSH&#xff08;Secure Shell&#xff09;是一个常用的协议&#xff0c;用于安全地访问远程计算机…

【0day】湖南建研工程质量检测系统InstrumentUsageRecordExport接口处存在任意文件读取漏洞

免责声明&#xff1a;文章来源互联网收集整理&#xff0c;请勿利用文章内的相关技术从事非法测试&#xff0c;由于传播、利用此文所提供的信息或者工具而造成的任何直接或者间接的后果及损失&#xff0c;均由使用者本人负责&#xff0c;所产生的一切不良后果与文章作者无关。该…

发那科Fanuc数控网络IP配置设定教程

1.在主面板如图按system键&#xff0c;进入系统界面 2.按右翻页切换键&#xff0c;切换到内嵌选项&#xff0c;按内嵌按钮跳转至设置IP界面&#xff0c;设置ip 3.按Focas2按钮&#xff0c;跳转至设置端口号和超时时间界面。设置端口号和时间之后&#xff0c;重启设备。注意&…

MES生产系统与数字孪生双重结合:智慧制造工厂的新引擎

随着数字化浪潮的推动&#xff0c;制造行业正在经历着前所未有的变革。在这个变革的浪潮中&#xff0c;MES生产制造系统与数字孪生技术的深度融合成为了制造工厂未来发展的核心驱动力。这种结合不仅提升了生产效率&#xff0c;优化了资源配置&#xff0c;降低了运营成本&#x…
最新文章