pytest是目前比较成熟功能齐全的测试框架,使用率肯定也不断攀升。在实际
工作中,许多测试用例都是类似的重复,一个个写最后代码会显得很冗余。这里,我们来了解一下
@pytest.mark.parametrize装饰器,可以很好的解决上述问题。
源代码分析
<span style="color:#444444"><span style="background-color:#f6f6f6">def parametrize(<span style="color:#333333"><strong>self</strong></span>,argnames, argvalues, indirect=<span style="color:#880000">False</span>, ids=<span style="color:#880000">None</span>, scope=<span style="color:#880000">None</span>):
<span style="color:#888888">""</span><span style="color:#888888">" 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函数定义的范围,允许使用测试上下文或配置设置动态范围。
"</span><span style="color:#888888">""</span>
</span></span>
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)])
标识
释义:可以理解为用例的id
格式:字符串列表,如["case1","case2","case3"]
间接
释义:当indirect=True时,若传入的argnames是fixture函数名,此时fixture函数名将成为一个可执行的函数,
argvalues作为固定的参数,执行固定函数,最终结果再存入 request.param;当indirect=False时,fixture
函数只作为一个参数名给测试收集阶段调用。
备注:这里可以将设置阶段(测试设置阶段)理解为配置 conftest.py 阶段,将收集阶段(
测试收集阶段)理解为用例执行阶段。
装饰测试类
<span style="color:#444444"><span style="background-color:#f6f6f6"><span style="color:#333333"><strong>import</strong></span> pytest
data = [
(<span style="color:#880000">2</span>,<span style="color:#880000"