IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 开发测试 -> Pytest之skip、skipif、xfail -> 正文阅读

[开发测试]Pytest之skip、skipif、xfail

在上一篇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:?allure?label?marker
@pytest.mark.allure_link:?allure?link?marker
@pytest.mark.allure_display_name:?allure?test?name?marker
@pytest.mark.allure_description:?allure?description
@pytest.mark.allure_description_html:?allure?description?html
@pytest.mark.filterwarnings(warning):?add?a?warning?filter?to?the?given?test.?see?https://docs.pytest.org/en/latest/warnings.html#pytest-mark-filterwarnings
@pytest.mark.skip(reason=None):?skip?the?given?test?function?with?an?optional?reason.?Example:?skip(reason="no?way?of?currently?testing?this")?skips?the?test.
@pytest.mark.skipif(condition):?skip?the?given?test?function?if?eval(condition)?results?in?a?True?value.??Evaluation?happens?within?the?module?global?context.?Example:?skipif('sys.platform?==?"win32"')?skips?the?test?if?we?are?on?the?win32?platform.?see?https://docs.pytest.org/en/latest/skipping.html
@pytest.mark.xfail(condition,?reason=None,?run=True,?raises=None,?strict=False):?mark?the?test?function?as?an?expected?failure?if?eval(condition)?has?a?True?value.?Optionally?specify?a?reason?for?better?reporting?and?run=False?if?you?don't?even?want?to?execute?the?test?function.?If?only?specific?exception(s)?are?expected,?you?can?list?them?in?raises,?and?if?the?test?fails?in?other?ways,?it?will?be?reported?as?a?true?failure.?See?https://docs.pytest.org/en/latest/skipping.html
@pytest.mark.parametrize(argnames,?argvalues):?call?a?test?function?multiple?times?passing?in?different?arguments?in?turn.?argvalues?generally?needs?to?be?a?list?of?values?if?argnames?specifies?only?one?name?or?a?list?of?tuples?of?values?if?argnames?specifies?multiple?names.?Example:?@parametrize('arg1',?[1,2])?would?lead?to?two?calls?of?the?decorated?test?function,?one?with?arg1=1?and?another?with?arg1=2.see?https://docs.pytest.org/en/latest/parametrize.html?for?more?info?and?examples.
@pytest.mark.usefixtures(fixturename1,?fixturename2,?...):?mark?tests?as?needing?all?of?the?specified?fixtures.?see?https://docs.pytest.org/en/latest/fixture.html#usefixtures
@pytest.mark.tryfirst:?mark?a?hook?implementation?function?such?that?the?plugin?machinery?will?try?to?call?it?first/as?early?as?possible.
@pytest.mark.trylast:?mark?a?hook?implementation?function?such?that?the?plugin?machinery?will?try?to?call?it?last/as?late?as?possible.

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

二、skip

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

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

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

1、类使用 @pytest.mark.skip

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

现有如下类:

test_demo.py

class?TestDemo():
????def?test_demo01(self):
????????print("这是test_demo01")
????def?test_demo02(self):
????????print("这是test_demo02")

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

import?pytest
@pytest.mark.skip(reason="功能未实现,暂不执行")
class?TestDemo():
????def?test_demo01(self):
????????print("这是test_demo01")
????def?test_demo02(self):
????????print("这是test_demo02")

运行结果如下:

2、方法使用@pytest.mark.skip

?

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

现在有如下类:

test_demo.py

class?TestDemo():
????def?test_demo01(self):
????????print("这是test_demo01")
????def?test_demo02(self):
????????print("这是test_demo02")

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

import?pytest
class?TestDemo():
????def?test_demo01(self):
????????print("这是test_demo01")
????@pytest.mark.skip(reason="功能未实现,暂不执行")
????def?test_demo02(self):
????????print("这是test_demo02")

运行结果如下:

3、函数使用@pytest.mark.skip

?现有如下函数:

test_demo.py

def?test_demo01():
????print("这是test_demo01")
def?test_demo02():
????print("这是test_demo02")

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

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

执行结果如下:

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

def?test_demo01():
????n?=?1
????while?True:
????????print("当前的的值为{}".format(n))
????????n?+=?1
????????if?n?==?4:
????????????pytest.skip("跳过的值为{}".format(n))

三、skipif

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

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

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

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

?

import?pytest
import?sys
def?test_demo01():
????print("这是test_demo01")
@pytest.mark.skipif(sys.version?<?'3.7',?reason="python版本必须大于3.7")
def?test_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

import?pytest
def?test_demo01():
????print("这是test_demo01")
@pytest.mark.xfail()
def?test_demo02():
????print("这是test_demo02")
????assert?1?==?2

运行结果为:

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

import?pytest
def?test_demo01():
????print("这是test_demo01")
@pytest.mark.xfail()
def?test_demo02():
????print("这是test_demo02")
????assert?1?==?1

运行结果为:

补充:?

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

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

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

import?pytest
class?TestDemo():
????def?test_001(self):
????????#?断言是否相等
????????except_result?=?'hello'
????????real_result?=?'hello?world'
????????if?except_result?==?real_result:
????????????print('断言成功')
????????else:
????????????pytest.xfail('断言失败,标记为xfail')
????def?test_002(self):
????????#?断言包含或不包含
????????assert?'hello'?in?'hello?world'
????????print('这是test_002')

运行结果为:

感谢每一个认真阅读我文章的人,看着粉丝一路的上涨和关注,礼尚往来总是要有的,虽然不是什么很值钱的东西,如果你用得到的话可以直接拿走:

① 2000多本软件测试电子书(主流和经典的书籍应该都有了)

② 软件测试/自动化测试标准库资料(最全中文版)

③ 项目源码(四五十个有趣且经典的练手项目及源码)

④ Python编程语言、API接口自动化测试、web自动化测试、App自动化测试(适合小白学习)

⑤ Python学习路线图(告别不入流的学习)

在我的QQ技术交流群里(技术交流和资源共享,广告进来腿给你打断)

可以自助拿走,群号953306497(备注“csdn111”)群里的免费资料都是笔者十多年测试生涯的精华。还有同行大神一起交流技术哦。

  开发测试 最新文章
pytest系列——allure之生成测试报告(Wind
某大厂软件测试岗一面笔试题+二面问答题面试
iperf 学习笔记
关于Python中使用selenium八大定位方法
【软件测试】为什么提升不了?8年测试总结再
软件测试复习
PHP笔记-Smarty模板引擎的使用
C++Test使用入门
【Java】单元测试
Net core 3.x 获取客户端地址
上一篇文章      下一篇文章      查看所有文章
加:2021-09-03 12:13:08  更:2021-09-03 12:13:24 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/11 22:36:32-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码