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

工作中,许多测试用例都是类似的重复,一个个写最后代码会显得很冗余。这里,我们来了解一下

@pytest.mark.parametrize装饰器,可以很好的解决上述问题。

源代码分析

def parametrize(self,argnames, argvalues, indirect=False, ids=None, scope=None):    
  """ Add new invocations to the underlying test function using the list
    of argvalues for the given argnames. Parametrization is performed
  during the collection phase. If you need to setup expensive resources
see about setting indirect to do it rather at test setup time.  # 使用给定argnames的argValue列表向基础测试函数添加新的调用,在收集阶段执行参数化。 :arg argnames: a comma-separated string denoting one or more argument
names, or a list/tuple of argument strings.  # 参数名:使用逗号分隔的字符串,列表或元祖,表示一个或多个参数名 :arg argvalues: The list of argvalues determines how often a
test is invoked with different argument values. If only one
argname was specified argvalues is a list of values. If N
argnames were specified, argvalues must be a list of N-tuples,
where each tuple-element specifies a value for its respective
argname.  # 参数值:只有一个argnames,argvalues则是值列表。有N个argnames时,每个元祖对应一组argnames,所有元祖组合成一个列表 :arg indirect: The list of argnames or boolean. A list of arguments'
names (self,subset of argnames). If True the list contains all names from
the argnames. Each argvalue corresponding to an argname in this list will
be passed as request.param to its respective argname fixture
function so that it can perform more expensive setups during the
setup phase of a test rather than at collection time.

:arg ids: list of string ids, or a callable.
If strings, each is corresponding to the argvalues so that they are
part of the test id. If None is given as id of specific test, the
automatically generated id for that argument will be used.
If callable, it should take one argument (self,a single argvalue) and return
a string or return None. If None, the automatically generated id for that
argument will be used.
If no ids are provided they will be generated automatically from
the argvalues.  # ids:字符串列表,可以理解成标题,与用例个数保持一致 :arg scope: if specified it denotes the scope of the parameters.
The scope is used for grouping tests by parameter instances.
It will also override any fixture-function defined scope, allowing
to set a dynamic scope using test context or configuration.  
  # 如果指定,则表示参数的范围。作用域用于按参数实例对测试进行分组。
   它还将覆盖任何fixture函数定义的范围,允许使用测试上下文或配置设置动态范围。
"""

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(

测试收集阶段)理解为用例执行阶段。

装饰测试类

import pytest

data = [
(2,2,4),
(3,4,12)
] def add(a,b):
return a * b @pytest.mark.parametrize('a,b,expect',data)
class TestParametrize(object):
def test_parametrize_1(self,a,b,expect):
print('\n测试函数1测试数据为\n{}-{}'.format(a,b))
assert add(a,b) == expect def test_parametrize_2(self,a,b,expect):
print('\n测试函数2测试数据为\n{}-{}'.format(a,b))
assert add(a,b) == expect if __name__ == "__main__":
pytest.main(["-s","test_07.py"])
============================= test session starts =============================
platform win32 -- Python 3.8.0, pytest-6.2.5, py-1.11.0, pluggy-1.0.0
rootdir: D:\AutoCode
plugins: html-3.1.1, metadata-1.11.0
collecting ... collected 4 items test_07.py::TestParametrize::test_parametrize_1[2-2-4]
测试函数1测试数据为
2-2
PASSED
test_07.py::TestParametrize::test_parametrize_1[3-4-12]
测试函数1测试数据为
3-4
PASSED
test_07.py::TestParametrize::test_parametrize_2[2-2-4]
测试函数2测试数据为
2-2
PASSED
test_07.py::TestParametrize::test_parametrize_2[3-4-12]
测试函数2测试数据为
3-4
PASSED ============================== 4 passed in 0.12s ============================== Process finished with exit code 0

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

装饰测试函数

单个数据

import pytest

data = ["Rose","white"]

@pytest.mark.parametrize("name",data)
def test_parametrize(name):
print('\n列表中的名字为\n{}'.format(name)) if __name__ == "__main__":
pytest.main(["-s","test_07.py"])
============================= test session starts =============================
platform win32 -- Python 3.8.0, pytest-6.2.5, py-1.11.0, pluggy-1.0.0
rootdir: D:\AutoCode
plugins: html-3.1.1, metadata-1.11.0
collected 2 items test_07.py
列表中的名字为
Rose
.
列表中的名字为
white
. ============================== 2 passed in 0.09s ============================== Process finished with exit code 0

  当测试用例只需要一个参数时,我们存放数据的列表无序嵌套序列,@pytest.mark.parametrize("name", data)

装饰器的第一个参数也只需要一个变量接收列表中的每个元素,第二个参数传递存储数据的列表,那么测试用

例需要使用同名的字符串接收测试数据(实例中的name)且列表有多少个元素就会生成并执行多少个测试用例。

一组数据

import pytest

data = [
[1, 2, 3],
[4, 5, 9]
] # 列表嵌套列表
# data_tuple = [
# (1, 2, 3),
# (4, 5, 9)
# ] # 列表嵌套元组 @pytest.mark.parametrize('a, b, expect', data)
def test_parametrize_1(a, b, expect): # 一个参数接收一个数据
print('\n测试数据为\n{},{},{}'.format(a, b, expect))
actual = a + b
assert actual == expect @pytest.mark.parametrize('value', data)
def test_parametrize_2(value): # 一个参数接收一组数据
print('\n测试数据为\n{}'.format(value))
actual = value[0] + value[1]
assert actual == value[2] if __name__ == "__main__":
pytest.main(["-s","test_07.py"])
============================= test session starts =============================
platform win32 -- Python 3.8.0, pytest-6.2.5, py-1.11.0, pluggy-1.0.0
rootdir: D:\AutoCode
plugins: html-3.1.1, metadata-1.11.0
collected 4 items test_07.py
测试数据为
1,2,3
.
测试数据为
4,5,9
.
测试数据为
[1, 2, 3]
.
测试数据为
[4, 5, 9]
. ============================== 4 passed in 0.09s ============================== Process finished with exit code 0

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

装饰器@pytest.mark.parametrize()可以使用单个变量接收数据,也可以使用多个变量接收,同样,测

试用例函数也需要与其保持一致。

  当使用单个变量接收时,测试数据传递到测试函数内部时为列表中的每一个元素或者小列表,需

要使用索引的方式取得每个数据。

  当使用多个变量接收数据时,那么每个变量分别接收小列表或元组中的每个元素列表嵌套多少个多

组小列表或元组,测生成多少条测试用例。

组合数据

import pytest

data_1 = [1,2,3]
data_2 = ['a','b'] @pytest.mark.parametrize('a',data_1)
@pytest.mark.parametrize('b',data_2)
def test_parametrize_1(a,b):
print(f'笛卡尔积测试结果为:{a},{b}') if __name__ == '__main__':
pytest.main(["-vs","test_06.py"])

  通过测试结果,我们不难分析,一个测试函数还可以同时被多个参数化装饰器装饰,那么多个

装饰器中的数据会进行交叉组合的方式传递给测试函数,进而生成n * n个测试用例。

标记用例

import pytest

@pytest.mark.parametrize("test_input,expected",[
("3+5",8),
("2+4",6),
pytest.param("6 * 9",42,marks=pytest.mark.xfail),
pytest.param("6 * 6",42,marks=pytest.mark.skip)
]) def test_mark(test_input,expected):
assert eval(test_input) == expected if __name__ == '__main__':
pytest.main(["-vs","test_06.py"])

  输出结果显示收集到4个用例,两个通过,一个被跳过,一个标记失败,当我们不想执行某组测试

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

嵌套字典

import pytest

data = (
{
'user': "name1",
'pwd': 123
},
{
'user': "name2",
'pwd': 456
}
) @pytest.mark.parametrize('dic',data)
def test_parametrize(dic):
print('\n测试数据为\n{}'.format(dic)) if __name__ == '__main__':
pytest.main(["-vs","test_06.py"])

增加测试结果可读性

  参数化装饰器有一个额外的参数ids,可以标识每一个测试用例,自定义测试数据结果的显示,

为了增加可读性,我们可以标记每一个测试用例使用的测试数据是什么,适当的增加一些说明。

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

import pytest

data_1 = [
(1, 2, 3),
(4, 5, 9)
] ids = ["a:{} + b:{} = expect:{}".format(a, b, expect) for a, b, expect in data_1] def add(a, b):
return a + b @pytest.mark.parametrize('a, b, expect', data_1, ids=ids)
class TestParametrize(object): def test_parametrize_1(self, a, b, expect):
print('\n测试函数1测试数据为\n{}-{}'.format(a, b))
assert add(a, b) == expect def test_parametrize_2(self, a, b, expect):
print('\n测试函数2数据为\n{}-{}'.format(a, b))
assert add(a, b) == expect if __name__ == '__main__':
pytest.main(["-v","test_06.py"])

不加ids参数的返回结果

加ids参数的返回结果

  我们可以看到带ids参数的返回结果中的用例都被一个列表明确的标记了,而且通过这种标记

可以更加直观的看出来,每个测试用例使用的数据名称及测试内容。

Python基础之pytest参数化的更多相关文章

  1. Python测试框架pytest入门基础

    Pytest简介 Pytest is a mature full-featured Python testing tool that helps you write better programs.T ...

  2. Python基础-week05

    本节大纲:Author:http://www.cnblogs.com/Jame-mei 模块介绍 time & datetime模块 random os sys shutil json &am ...

  3. Python基础(12)--模块

    本文地址:http://www.cnblogs.com/archimedes/p/python-modules.html,转载请注明源地址. 模块简介 如果你退出 Python 解释器重新进入,以前创 ...

  4. Python 中如何实现参数化测试?

    Python 中如何实现参数化测试? 之前,我曾转过一个单元测试框架系列的文章,里面介绍了 unittest.nose/nose2 与 pytest 这三个最受人欢迎的 Python 测试框架. 本文 ...

  5. python基础全部知识点整理,超级全(20万字+)

    目录 Python编程语言简介 https://www.cnblogs.com/hany-postq473111315/p/12256134.html Python环境搭建及中文编码 https:// ...

  6. pytest「conftest、pytest参数化、重运行、出测试报告」

    文章总览图 一.conftest问题整理: 1.这个conftest.py分路径吗?如果在TestCases下建这个包可以直接用吗? TestCases这里有ModeA和ModeB,想在ModeA或M ...

  7. python测试框架-pytest

    一.pytest 介绍.运行.参数化和数据驱动.Fixture pytest安装与介绍 官网 : pip install -U pytest 查看版本号:pytest --version 为何选择py ...

  8. python之最强王者(2)——python基础语法

    背景介绍:由于本人一直做java开发,也是从txt开始写hello,world,使用javac命令编译,一直到使用myeclipse,其中的道理和辛酸都懂(请容许我擦干眼角的泪水),所以对于pytho ...

  9. Python开发【第二篇】:Python基础知识

    Python基础知识 一.初识基本数据类型 类型: int(整型) 在32位机器上,整数的位数为32位,取值范围为-2**31-2**31-1,即-2147483648-2147483647 在64位 ...

随机推荐

  1. Can references refer to invalid location in C++?

    在C++中,引用比指针更加的安全,一方面是因为引用咋定义时必须进行初始化,另一方面是引用一旦被初始化就无法使其与其他对象相关联. 但是,在使用引用的地方仍然会有一些例外. (1)Reference t ...

  2. java foreach循环抛出异常java.util.ConcurrentModificationException

    代码如下: for (Iterator<String> iter = list.iterator(); iter.hasNext(); ) { if (Integer.parseInt(i ...

  3. idea maven 项目 遇到 "Module not specified" 解决方法

    1. 原因:我这边出现的原因是 其他同事在提交代码是 将  这个文件夹也提交了,idea 会加载 .idea 里的配置(即 他的配置),而我的 maven 配置不同,导致出错. 2. 解决方法:删除这 ...

  4. 关于for与forEach遍历集合中对集合进行操作的问题

    遍历List集合,在循环中再对List集合进行操作,有时候会遇到ConcurrentModificationException(并发修改异常);其实只有在forEach循环集合再对集合操作会发生异常: ...

  5. 【力扣】123. 买卖股票的最佳时机 III

    给定一个数组,它的第 i 个元素是一支给定的股票在第 i 天的价格. 设计一个算法来计算你所能获取的最大利润.你最多可以完成 两笔 交易. 注意:你不能同时参与多笔交易(你必须在再次购买前出售掉之前的 ...

  6. Redis集群断电恢复

    再集群整体断点或关闭后,默认启动集群后,会成为孤立的单点,需要删除每个节点的pid文件,node.conf.并将RDB和AOF文件移动出来,再挨个启动每个节点,并用create创建集群脚本,重新创建集 ...

  7. RDS备份到OSS增量+全量

    一.前言 阿里云的RDS备份是占用使用量的,你购买200G那备份使用量是100G左右,导致备份一般也就存半个月,2个全备份. 那半个月后之前的也就删除了,如果要持续保留更久将花费不少的金钱.所以这里用 ...

  8. Jenkins安全加固

    1.jenkins未授权访问 描述 jenkins不当配置可导致未授权访问管理控制台,可以通过脚本命令行执行系统命令.通过该漏洞,可以后台管理服务,通过脚本命令行功能执行系统命令,如反弹shell,w ...

  9. Mac配置apache,mysql

    ===========Apache=============================== 1. 启动关闭Apache MAC系统已经预装了apache,启动.关闭.查看版本等命令如下: 启动a ...

  10. [BUUCTF]REVERSE——CrackRTF

    CrackRTF 附件 步骤: 例行查壳儿,32位程序,无壳儿 32位ida载入,main函数开始分析程序 破解第一个密码 sub_40100A()是一个加密函数,具体的写的算法没去分析,但是Cryp ...