Python基础教程之基础教程之pytest参数化详解参数化详解
⽬录⽬录
前⾔
源代码分析
装饰测试类
装饰测试函数
单个数据
⼀组数据
组合数据
标记⽤例
嵌套字典
增加测试结果可读性
总结
前⾔前⾔
上篇博⽂介绍过,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(
测试收集阶段)理解为⽤例执⾏阶段。
装饰测试类装饰测试类