pytest是目前比较成熟功能齐全的测试框架,使用率肯定也不断攀升。在实际 工作中,许多测试用例都是类似的重复,一个个写最后代码会显得很冗余。这里,我们来了解一下 pytest。mark。parametrize装饰器,可以很好的解决上述问题。源代码分析defparametrize(self,argnames,argvalues,indirectFalse,idsNone,scopeNone):Addnewinvocationstotheunderlyingtestfunctionusingthelistofargvaluesforthegivenargnames。Parametrizationisperformedduringthecollectionphase。Ifyouneedtosetupexpensiveresourcesseeaboutsettingindirecttodoitratherattestsetuptime。使用给定argnames的argValue列表向基础测试函数添加新的调用,在收集阶段执行参数化。:argargnames:acommaseparatedstringdenotingoneormoreargumentnames,oralisttupleofargumentstrings。参数名:使用逗号分隔的字符串,列表或元祖,表示一个或多个参数名:argargvalues:Thelistofargvaluesdetermineshowoftenatestisinvokedwithdifferentargumentvalues。Ifonlyoneargnamewasspecifiedargvaluesisalistofvalues。IfNargnameswerespecified,argvaluesmustbealistofNtuples,whereeachtupleelementspecifiesavalueforitsrespectiveargname。参数值:只有一个argnames,argvalues则是值列表。有N个argnames时,每个元祖对应一组argnames,所有元祖组合成一个列表:argindirect:Thelistofargnamesorboolean。Alistofargumentsnames(self,subsetofargnames)。IfTruethelistcontainsallnamesfromtheargnames。Eachargvaluecorrespondingtoanargnameinthislistwillbepassedasrequest。paramtoitsrespectiveargnamefixturefunctionsothatitcanperformmoreexpensivesetupsduringthesetupphaseofatestratherthanatcollectiontime。:argids:listofstringids,oracallable。Ifstrings,eachiscorrespondingtotheargvaluessothattheyarepartofthetestid。IfNoneisgivenasidofspecifictest,theautomaticallygeneratedidforthatargumentwillbeused。Ifcallable,itshouldtakeoneargument(self,asingleargvalue)andreturnastringorreturnNone。IfNone,theautomaticallygeneratedidforthatargumentwillbeused。Ifnoidsareprovidedtheywillbegeneratedautomaticallyfromtheargvalues。ids:字符串列表,可以理解成标题,与用例个数保持一致:argscope:ifspecifieditdenotesthescopeoftheparameters。Thescopeisusedforgroupingtestsbyparameterinstances。Itwillalsooverrideanyfixturefunctiondefinedscope,allowingtosetadynamicscopeusingtestcontextorconfiguration。如果指定,则表示参数的范围。作用域用于按参数实例对测试进行分组。它还将覆盖任何fixture函数定义的范围,允许使用测试上下文或配置设置动态范围。argnames 释义:参数名称 格式:字符串arg1,arg2,arg3aegvalues 释义:参数值列表 格式:必须是列表,如〔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 释义:当indirectTrue时,若传入的argnames是fixture函数名,此时fixture函数名将成为一个可执行的函数, argvalues作为fixture的参数,执行fixture函数,最终结果再存入request。param;当indirectFalse时,fixture 函数只作为一个参数名给测试收集阶段调用。 备注:这里可以将thesetupphase(测试设置阶段)理解为配置conftest。py阶段,将thecollectionphase( 测试收集阶段)理解为用例执行阶段。装饰测试类importpytestdata〔(2,2,4),(3,4,12)〕defadd(a,b):returnabpytest。mark。parametrize(a,b,expect,data)classTestParametrize(object):deftestparametrize1(self,a,b,expect):print(测试函数1测试数据为{}{}。format(a,b))assertadd(a,b)expectdeftestparametrize2(self,a,b,expect):print(测试函数2测试数据为{}{}。format(a,b))assertadd(a,b)expectifnamemain:pytest。main(〔s,test07。py〕)testsessionstartsplatformwin32Python3。8。0,pytest6。2。5,py1。11。0,pluggy1。0。0rootdir:D:AutoCodeplugins:html3。1。1,metadata1。11。0collecting。。。collected4itemstest07。py::TestParametrize::testparametrize1〔224〕测试函数1测试数据为22PASSEDtest07。py::TestParametrize::testparametrize1〔3412〕测试函数1测试数据为34PASSEDtest07。py::TestParametrize::testparametrize2〔224〕测试函数2测试数据为22PASSEDtest07。py::TestParametrize::testparametrize2〔3412〕测试函数2测试数据为34PASSED4passedin0。12sProcessfinishedwithexitcode0 由以上代码可以看到,当装饰器装饰测试类时,定义的数据集合会被传递给类的所有方法。装饰测试函数单个数据importpytestdata〔Rose,white〕pytest。mark。parametrize(name,data)deftestparametrize(name):print(列表中的名字为{}。format(name))ifnamemain:pytest。main(〔s,test07。py〕)testsessionstartsplatformwin32Python3。8。0,pytest6。2。5,py1。11。0,pluggy1。0。0rootdir:D:AutoCodeplugins:html3。1。1,metadata1。11。0collected2itemstest07。py列表中的名字为Rose。列表中的名字为white。2passedin0。09sProcessfinishedwithexitcode0 当测试用例只需要一个参数时,我们存放数据的列表无序嵌套序列,pytest。mark。parametrize(name,data) 装饰器的第一个参数也只需要一个变量接收列表中的每个元素,第二个参数传递存储数据的列表,那么测试用 例需要使用同名的字符串接收测试数据(实例中的name)且列表有多少个元素就会生成并执行多少个测试用例。一组数据importpytestdata〔〔1,2,3〕,〔4,5,9〕〕列表嵌套列表datatuple〔(1,2,3),(4,5,9)〕列表嵌套元组pytest。mark。parametrize(a,b,expect,data)deftestparametrize1(a,b,expect):一个参数接收一个数据print(测试数据为{},{},{}。format(a,b,expect))actualabassertactualexpectpytest。mark。parametrize(value,data)deftestparametrize2(value):一个参数接收一组数据print(测试数据为{}。format(value))actualvalue〔0〕value〔1〕assertactualvalue〔2〕ifnamemain:pytest。main(〔s,test07。py〕)testsessionstartsplatformwin32Python3。8。0,pytest6。2。5,py1。11。0,pluggy1。0。0rootdir:D:AutoCodeplugins:html3。1。1,metadata1。11。0collected4itemstest07。py测试数据为1,2,3。测试数据为4,5,9。测试数据为〔1,2,3〕。测试数据为〔4,5,9〕。4passedin0。09sProcessfinishedwithexitcode0 当测试用例需要多个数据时,我们可以使用嵌套序列(嵌套元组嵌套列表)的列表来存放测试数据。 装饰器pytest。mark。parametrize()可以使用单个变量接收数据,也可以使用多个变量接收,同样,测 试用例函数也需要与其保持一致。 当使用单个变量接收时,测试数据传递到测试函数内部时为列表中的每一个元素或者小列表,需 要使用索引的方式取得每个数据。 当使用多个变量接收数据时,那么每个变量分别接收小列表或元组中的每个元素列表嵌套多少个多 组小列表或元组,测生成多少条测试用例。组合数据importpytestdata1〔1,2,3〕data2〔a,b〕pytest。mark。parametrize(a,data1)pytest。mark。parametrize(b,data2)deftestparametrize1(a,b):print(f笛卡尔积测试结果为:{a},{b})ifnamemain:pytest。main(〔vs,test06。py〕) 通过测试结果,我们不难分析,一个测试函数还可以同时被多个参数化装饰器装饰,那么多个 装饰器中的数据会进行交叉组合的方式传递给测试函数,进而生成nn个测试用例。标记用例importpytestpytest。mark。parametrize(testinput,expected,〔(35,8),(24,6),pytest。param(69,42,markspytest。mark。xfail),pytest。param(66,42,markspytest。mark。skip)〕)deftestmark(testinput,expected):asserteval(testinput)expectedifnamemain:pytest。main(〔vs,test06。py〕) 输出结果显示收集到4个用例,两个通过,一个被跳过,一个标记失败,当我们不想执行某组测试 数据时,我们可以标记skip或skipif;当我们预期某组数据会执行失败时,我们可以标记为xfail等。嵌套字典importpytestdata({user:name1,pwd:123},{user:name2,pwd:456})pytest。mark。parametrize(dic,data)deftestparametrize(dic):print(测试数据为{}。format(dic))ifnamemain:pytest。main(〔vs,test06。py〕) 增加测试结果可读性 参数化装饰器有一个额外的参数ids,可以标识每一个测试用例,自定义测试数据结果的显示, 为了增加可读性,我们可以标记每一个测试用例使用的测试数据是什么,适当的增加一些说明。 在使用前你需要知道,ids参数应该是一个字符串列表,必须和数据对象列表的长度保持一致。importpytestdata1〔(1,2,3),(4,5,9)〕ids〔a:{}b:{}expect:{}。format(a,b,expect)fora,b,expectindata1〕defadd(a,b):returnabpytest。mark。parametrize(a,b,expect,data1,idsids)classTestParametrize(object):deftestparametrize1(self,a,b,expect):print(测试函数1测试数据为{}{}。format(a,b))assertadd(a,b)expectdeftestparametrize2(self,a,b,expect):print(测试函数2数据为{}{}。format(a,b))assertadd(a,b)expectifnamemain:pytest。main(〔v,test06。py〕) 不加ids参数的返回结果 加ids参数的返回结果 我们可以看到带ids参数的返回结果中的用例都被一个列表明确的标记了,而且通过这种标记 可以更加直观的看出来,每个测试用例使用的数据名称及测试内容。