700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > Pytest之skip skipif xfail

Pytest之skip skipif xfail

时间:2022-06-02 23:20:40

相关推荐

Pytest之skip skipif xfail

VOL 138

17

-08

今天距137天

这是ITester软件测试小栈第150次推文

点击上方蓝字“ITester软件测试小栈“关注我,每周一、三、五早上07:30准时推送。

微信公众号后台回复“资源”、“测试工具包”领取测试资源,回复“微信群”一起进群打怪。

本文5539字,阅读约需14分钟

在上一篇Pytest系列文章:Pytest之fixture,主要介绍fixture的介绍、调用方式及作用域。

以下主要介绍pytest中skipskipifxfail的用法。

mark基本介绍

1

mark概念

在pytest当中,给用例打标记,在运行时,通过标记名来过滤测试用例。

2

使用mark的原因

在自动化过程中,我们可以能遇到问题,比如测试用例比较多,且不在一个层级,想将某些用例作为冒烟测试用例,要怎么处理。pytest提供了mark功能,可以解决此问题。

3

mark分类

mark可分为2类:

一类是系统内置的mark,不同的mark标记提供不同的功能。

二类是自定义的mark,该类mark主要用于给测试用例分门别类,使得运行测试时可以指定运行符合哪一类标记的测试用例。

4

内置mark

查看内置的mark,输入命令:pytest --markers

@pytest.mark.allure_label:allurelabelmarker@pytest.mark.allure_link:allurelinkmarker@pytest.mark.allure_display_name:alluretestnamemarker@pytest.mark.allure_description:alluredescription@pytest.mark.allure_description_html:alluredescriptionhtml@pytest.mark.filterwarnings(warning):addawarningfiltertothegiventest.see/en/latest/warnings.html#pytest-mark-filterwarnings@pytest.mark.skip(reason=None):skipthegiventestfunctionwithanoptionalreason.Example:skip(reason="nowayofcurrentlytestingthis")skipsthetest.@pytest.mark.skipif(condition):skipthegiventestfunctionifeval(condition)resultsinaTruevalue.Evaluationhappenswithinthemoduleglobalcontext.Example:skipif('sys.platform=="win32"')skipsthetestifweareonthewin32platform.see/en/latest/skipping.html@pytest.mark.xfail(condition,reason=None,run=True,raises=None,strict=False):markthetestfunctionasanexpectedfailureifeval(condition)hasaTruevalue.Optionallyspecifyareasonforbetterreportingandrun=Falseifyoudon'tevenwanttoexecutethetestfunction.Ifonlyspecificexception(s)areexpected,youcanlisttheminraises,andifthetestfailsinotherways,itwillbereportedasatruefailure.See/en/latest/skipping.html@pytest.mark.parametrize(argnames,argvalues):callatestfunctionmultipletimespassingindifferentargumentsinturn.argvaluesgenerallyneedstobealistofvaluesifargnamesspecifiesonlyonenameoralistoftuplesofvaluesifargnamesspecifiesmultiplenames.Example:@parametrize('arg1',[1,2])wouldleadtotwocallsofthedecoratedtestfunction,onewitharg1=1andanotherwitharg1=2.see/en/latest/parametrize.htmlformoreinfoandexamples.@pytest.mark.usefixtures(fixturename1,fixturename2,...):marktestsasneedingallofthespecifiedfixtures.see/en/latest/fixture.html#usefixtures@pytest.mark.tryfirst:markahookimplementationfunctionsuchthatthepluginmachinerywilltrytocallitfirst/asearlyaspossible.@pytest.mark.trylast:markahookimplementationfunctionsuchthatthepluginmachinerywilltrytocallitlast/aslateaspossible.

以下主要介绍skip、skipif、xfail这三种的用法。

skip

语法:@pytest.mark.skip(reason=None)

说明:跳过执行测试用例,可选参数reason,跳过的原因,会在执行结果中打印。

用法:在类、方法或函数上添加@pytest.mark.skip

1

类使用 @pytest.mark.skip

作用于类上,则类下面的所有方法都跳过测试。

现有如下类:

test_demo.py

classTestDemo():deftest_demo01(self):print("这是test_demo01")deftest_demo02(self):print("这是test_demo02")

目前因为TestDemo类功能并未完成,想跳过用例执行,在类上方添加@pytest.mark.skip即可。

importpytest@pytest.mark.skip(reason="功能未实现,暂不执行")classTestDemo():deftest_demo01(self):print("这是test_demo01")deftest_demo02(self):print("这是test_demo02")

运行结果如下:

2

方法使用@pytest.mark.skip

作用于方法上,则此方法跳过测试。

现在有如下类:

test_demo.py

classTestDemo():deftest_demo01(self):print("这是test_demo01")deftest_demo02(self):print("这是test_demo02")

目前因为test_demo02方法功能并未完成,想跳过用例执行,在test_demo02方法上添加@pytest.mark.skip即可。

importpytestclassTestDemo():deftest_demo01(self):print("这是test_demo01")@pytest.mark.skip(reason="功能未实现,暂不执行")deftest_demo02(self):print("这是test_demo02")

运行结果如下:

3

函数使用@pytest.mark.skip

现有如下函数:

test_demo.py

deftest_demo01():print("这是test_demo01")deftest_demo02():print("这是test_demo02")

目前因为test_demo02函数功能并未完成,想跳过用例执行,在函数上方添加@pytest.mark.skip即可。

importpytestdeftest_demo01():print("这是test_demo01")@pytest.mark.skip(reason="功能未实现,暂不执行")deftest_demo02():print("这是test_demo02")

执行结果如下:

补充:除了通过使用标签的方式,还可以在测试用例中调用pytest.skip()方法来实现跳过,传入msg参数来说明跳过原因。

deftest_demo01():n=1whileTrue:print("当前的的值为{}".format(n))n+=1ifn==4:pytest.skip("跳过的值为{}".format(n))

skipif

语法:@pytest.mark.skipif(self,condition, reason=None)

说明:跳过执行测试用例,condition参数为条件,可选参数reason,跳过的原因,会在执行结果中打印。

从之前的运行结果可以看出一些软件版本信息。

比如当前的python版本为3.6,要求python版本必须大于3.7,否则跳过测试。

importpytestimportsysdeftest_demo01():print("这是test_demo01")@pytest.mark.skipif(sys.version<'3.7',reason="python版本必须大于3.7")deftest_demo02():print("这是test_demo02")

运行结果如下:

xfail

应用场景:用例功能不完善或者用例执行失败,可以标记为xfail。

语法:@pytest.mark.xfail(self,condition=None, reason=None, raises=None, run=True, strict=False)

说明:期望测试用例是失败的,但是不会影响测试用例的的执行。如果测试用例执行失败的则结果是xfail(不会额外显示出错误信息);如果测试用例执行成功的则结果是xpass。

来个小例子实战下,用例断言失败,且标记为xfail。

test_demo.py

importpytestdeftest_demo01():print("这是test_demo01")@pytest.mark.xfail()deftest_demo02():print("这是test_demo02")assert1==2

运行结果为:

接下将用例断言成功,标记为xfail。

importpytestdeftest_demo01():print("这是test_demo01")@pytest.mark.xfail()deftest_demo02():print("这是test_demo02")assert1==1

运行结果为:

补充:

pytest中,pytest.xfail()方法也可以将用例标记为失败。

语法:pytest.xfail(reason: str = "")

举个小例子,比如断言时,断言失败,我们就标记为xfail。

importpytestclassTestDemo():deftest_001(self):#断言是否相等except_result='hello'real_result='helloworld'ifexcept_result==real_result:print('断言成功')else:pytest.xfail('断言失败,标记为xfail')deftest_002(self):#断言包含或不包含assert'hello'in'helloworld'print('这是test_002')

运行结果为:

以上

That‘s all

更多系列文章

敬请期待

ITester软件测试小栈

往期内容宠幸

1.Python接口自动化-接口基础(一)

2.Python接口自动化-接口基础(二)

3.Python接口自动化-requests模块之get请求

4.Python接口自动化-requests模块之post请求

5.Python接口自动化之cookie、session应用

6.Python接口自动化之Token详解及应用

7.Python接口自动化之requests请求封装

8.Python接口自动化之pymysql数据库操作

9.Python接口自动化之logging日志

10.Python接口自动化之logging封装及实战

想获取更多最新干货内容

快来星标 置顶 关注我

每周一、三、五 07:30见

<< 滑动查看下一张图片 >>

后台回复"资源"取干货

回复"微信群"一起打怪升级

测试交流Q群:727998947

点亮一下在看,你更好看

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。