VOL 138

17

2020-08

今天距2021年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: 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 = 1while True:print("当前的的值为{}".format(n))n += 1if 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')

运行结果为:

以上

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

点亮一下在看,你更好看

Pytest之skip、skipif、xfail相关推荐

  1. pytest —skip和xfail标记

    前言 实际工作中,测试用例的执行可能会依赖于一些外部条件,例如:只能运行在某个特定的操作系统(Windows),或者我们本身期望它们测试失败,例如:被某个已知的Bug所阻塞:如果我们能为这些用例提前打 ...

  2. Pytest跳过执行之@pytest.mark.skip()详解大全

    一.skip介绍及运用 在我们自动化测试过程中,经常会遇到功能阻塞.功能未实现.环境等一系列外部因素问题导致的一些用例执行不了,这时我们就可以用到跳过skip用例,如果我们注释掉或删除掉,后面还要进行 ...

  3. Pytest框架系列——配置文件Pytest.ini

    前言 pytest.ini文件是pytest的主配置文件:可以改变pytest的运行方式:它是一个固定的文件pytest.ini文件,读取配置信息,按指定的方式去运行. pytest.ini文件的位置 ...

  4. 「高效程序员的修炼」快速上手python主流测试框架pytest以及单元测试编写

    如果对你有帮助,就点个赞吧~ 本文主要介绍如果编写Python的单元测试,包括如何使用断言,如何考虑测试哪些情况,如何避免外部依赖对测试的影响,如果用数据驱动的方式简化重复测试的编写等等等等 文章目录 ...

  5. pytest测试框架(六)---使用skip和skipif跳过测试用例

    目录 一.简介 二.使用方法 1.@pytest.mark.skip装饰器 2.pytest.skip方法 3.@pytest.mark.skipif装饰器 4.pytestmark变量 5.conf ...

  6. Pytest系列:csdn最最最详细,听不懂你找我。 skip、skipif跳过用例

    目录 前言 skip skipif skip类或模块 skip文件或目录 小技巧 跳过测试类 跳过方法或测试用例 多个skip时,满足1个条件即跳过 skip赋值给变量,可多处使用装饰器函数的语法格式 ...

  7. pytest测试框架(五)---使用xfail将用例标记为失败

    一.简介 当因为一个确切的原因,我们知道这个用例会执行失败,比如用例所覆盖的功能还未实现,或者这个功能存在阻塞性的已知Bug时,就可以使用xfail将其标记起来. 二.xfail的使用方法 1.@py ...

  8. Python自动化测试框架之Pytest教程【让你小鸡变老鹰】

    Pytest  pytest是一个非常成熟的全功能的Python测试框架,主要有以下几个特点: · 简单灵活,容易上手 · 支持参数化 · 能够支持简单的单元测试和复杂的功能测试,还可以用来做sele ...

  9. pytest测试框架_聊聊 Python 的单元测试框架(三):最火的 pytest

    本文首发于 HelloGitHub 公众号,并发表于 Prodesire 博客. 一.介绍 本篇文章是<聊聊 Python 的单元测试框架>的第三篇,前两篇分别介绍了标准库 unittes ...

最新文章

  1. Azure China (7) 使用WebMetrix将Web Site发布至Azure China
  2. php表单时间转换为时间戳-175
  3. Spring Boot: Tuning your Undertow application for throughput--转
  4. 埃及分数问题——迭代加深搜索
  5. 数据结构(四)之单链表查找中间结点
  6. OMNeT学习之OMNeT安装与运行
  7. BC 2015在百度之星程序设计大赛 - 预赛(1)(矩形区域-旋转卡)
  8. 深入浅出LVM on linux
  9. 关于含光 800,这里有你想要的一切答案!
  10. 广度优先搜索c语言矩阵,算法7-6:图的遍历——广度优先搜索 (C++代码)
  11. 【leetcode】二叉树(python)
  12. Fix Bug的五个阶段
  13. 原画插画零基础自学|原画基础入门教程
  14. Unix平台下的常用命令技巧之资源与性能
  15. 算法系列:Reservoir Sampling
  16. 51单片机系列——定时/计数器
  17. 云计算与大数据技术应用 第三章
  18. 单点登录 Ucenter 分析
  19. 2022.8.11今天回顾了以前c语言的理论知识,我们回顾了计算机的基本结构,存储器的内存组成,数据类型。分享给大家。
  20. 无法像唐骏一样地成功

热门文章

  1. c++嵌入linux指令以查找文件夹
  2. js 给服务器发消息,的Node.js:发送消息至服务器
  3. hbase中为何不能向表中插入数据_生产环境使用HBase,你必须知道的最佳实践 | 百万人学AI...
  4. 电脑开机一会就蓝屏怎么回事_电脑蓝屏怎么回事
  5. html dw map,DW十六 map标签
  6. 笛卡尔坐标系_Shader学习(4)坐标系和矢量的概念
  7. 关于 IdentityServer4 中的 Jwt Token 与 Reference Token
  8. 解决 ImportError: No module named 'pip._internal'问题
  9. react-navigation StackNavigator 快速点击会多次跳转页面
  10. OpenGL入门笔记(六)