Pytest中skip skipif跳过用例详解


Posted in Python onJune 30, 2021

前言

  • pytest.mark.skip可以标记无法在某些平台上运行的测试功能,
  • 或者您希望失败的测试功能希望满足某些条件才执行某些测试用例,否则pytest会跳过运行该测试用例
  • 实际常见场景:跳过非Windows平台上的仅Windows测试,或者跳过依赖于当前不可用的外部资源(例如数据库)的测试

@pytest.mark.skip

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

#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
__title__  = 
__Time__   = 2020/4/9 13:49
__Author__ = 小菠萝测试笔记
__Blog__   = https://www.cnblogs.com/poloyy/
"""
import pytest


@pytest.fixture(autouse=True)
def login():
    print("====登录====")


def test_case01():
    print("我是测试用例11111")


@pytest.mark.skip(reason="不执行该用例!!因为没写好!!")
def test_case02():
    print("我是测试用例22222")


class Test1:

    def test_1(self):
        print("%% 我是类测试用例1111 %%")

    @pytest.mark.skip(reason="不想执行")
    def test_2(self):
        print("%% 我是类测试用例2222 %%")


@pytest.mark.skip(reason="类也可以跳过不执行")
class TestSkip:
    def test_1(self):
        print("%% 不会执行 %%")

执行结果

Pytest中skip skipif跳过用例详解

知识点

  • @pytest.mark.skip可以加在函数上,类上,类方法上
  • 如果加在类上面,类里面的所有测试用例都不会执行
  • 以上小案例都是针对:整个测试用例方法跳过执行,如果想在测试用例执行期间跳过不继续往下执行呢?

pytest.skip()函数基础使用

作用:在测试用例执行期间强制跳过不再执行剩余内容

类似:在Python的循环里面,满足某些条件则break 跳出循环

def test_function():
    n = 1
    while True:
        print(f"这是我第{n}条用例")
        n += 1
        if n == 5:
            pytest.skip("我跑五次了不跑了")

执行结果

Pytest中skip skipif跳过用例详解

pytest.skip(msg="",allow_module_level=False)

当allow_module_level=True时,可以设置在模块级别跳过整个模块

#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
__title__  = 
__Time__   = 2020/4/9 13:49
__Author__ = 小菠萝测试笔记
__Blog__   = https://www.cnblogs.com/poloyy/
"""
import sys
import pytest

if sys.platform.startswith("win"):
    pytest.skip("skipping windows-only tests", allow_module_level=True)


@pytest.fixture(autouse=True)
def login():
    print("====登录====")


def test_case01():
    print("我是测试用例11111")

执行结果

collecting ...
Skipped: skipping windows-only tests
collected 0 items / 1 skipped
============================= 1 skipped in 0.15s ==============================

@pytest.mark.skipif(condition, reason="")

作用:希望有条件地跳过某些测试用例

注意:condition需要返回True才会跳过

@pytest.mark.skipif(sys.platform == 'win32', reason="does not run on windows")
class TestSkipIf(object):
    def test_function(self):
        print("不能在window上运行")

执行结果

collecting ... collected 1 item
07skip_sipif.py::TestSkipIf::test_function SKIPPED                       [100%]
Skipped: does not run on windows
============================= 1 skipped in 0.04s ==============================

跳过标记

  • 可以将pytest.mark.skip和pytest.mark.skipif赋值给一个标记变量
  • 在不同模块之间共享这个标记变量
  • 若有多个模块的测试用例需要用到相同的skip或skipif,可以用一个单独的文件去管理这些通用标记,然后适用于整个测试用例集
# 标记
skipmark = pytest.mark.skip(reason="不能在window上运行=====")
skipifmark = pytest.mark.skipif(sys.platform == 'win32', reason="不能在window上运行啦啦啦=====")


@skipmark
class TestSkip_Mark(object):

    @skipifmark
    def test_function(self):
        print("测试标记")

    def test_def(self):
        print("测试标记")


@skipmark
def test_skip():
    print("测试标记")

执行结果

collecting ... collected 3 items
07skip_sipif.py::TestSkip_Mark::test_function SKIPPED                    [ 33%]
Skipped: 不能在window上运行啦啦啦=====
07skip_sipif.py::TestSkip_Mark::test_def SKIPPED                         [ 66%]
Skipped: 不能在window上运行=====
07skip_sipif.py::test_skip SKIPPED                                       [100%]
Skipped: 不能在window上运行=====
============================= 3 skipped in 0.04s ==============================

pytest.importorskip( modname: str, minversion: Optional[str] = None, reason: Optional[str] = None )

作用:如果缺少某些导入,则跳过模块中的所有测试

参数列表

  • modname:模块名
  • minversion:版本号
  • reasone:跳过原因,默认不给也行
pexpect = pytest.importorskip("pexpect", minversion="0.3")


@pexpect
def test_import():
    print("test")

执行结果一:如果找不到module

Skipped: could not import 'pexpect': No module named 'pexpect'
collected 0 items / 1 skipped

执行结果一:如果版本对应不上

Skipped: module 'sys' has __version__ None, required is: '0.3'
collected 0 items / 1 skipped

到此这篇关于Pytest中skip skipif跳过用例详解的文章就介绍到这了,更多相关skip skipif跳过用例内容请搜索三水点靠木以前的文章或继续浏览下面的相关文章希望大家以后多多支持三水点靠木!

Python 相关文章推荐
使用Python下载Bing图片(代码)
Nov 07 Python
分数霸榜! python助你微信跳一跳拿高分
Jan 08 Python
Python实现判断并移除列表指定位置元素的方法
Apr 13 Python
Python socket实现的简单通信功能示例
Aug 21 Python
利用python-pypcap抓取带VLAN标签的数据包方法
Jul 23 Python
详解python pandas 分组统计的方法
Jul 30 Python
python redis连接 有序集合去重的代码
Aug 04 Python
python3获取url文件大小示例代码
Sep 18 Python
有关pycharm登录github时有的时候会报错connection reset的问题
Sep 15 Python
python 实现弹球游戏的示例代码
Nov 17 Python
Python项目打包成二进制的方法
Dec 30 Python
python如何实现递归转非递归
Feb 25 Python
Pytest中skip和skipif的具体使用方法
Python将CSV文件转化为HTML文件的操作方法
如何使用Tkinter进行窗口的管理与设置
Python 语言实现六大查找算法
详解MindSpore自定义模型损失函数
教你用python实现12306余票查询
python实现简易自习室座位预约系统
You might like
PHP中基本符号及使用方法
2010/03/23 PHP
PHP5中使用PDO连接数据库的方法
2010/08/01 PHP
PHP中通过加号合并数组的一个简单方法分享
2011/01/27 PHP
php数组中包含中文的排序方法
2014/06/03 PHP
教你如何开启shopnc b2b2c 伪静态
2014/10/21 PHP
PHP5.6新增加的可变函数参数用法分析
2017/08/25 PHP
PHP基于openssl实现的非对称加密操作示例
2019/01/11 PHP
Javascript与vbscript数据共享
2007/01/09 Javascript
用jscript实现列出安装的软件列表
2007/06/18 Javascript
什么是json和jsonp,jQuery json实例详详细说明
2012/12/11 Javascript
js获取指定日期前后的日期代码
2013/08/20 Javascript
jQuery.prop() 使用详解
2015/07/19 Javascript
使用jquery获取url以及jquery获取url参数的实现方法
2016/05/25 Javascript
Three.js学习之正交投影照相机
2016/08/01 Javascript
Vue父组件调用子组件事件方法
2018/02/23 Javascript
关于Node.js中频繁修改代码重启服务器的问题
2020/10/15 Javascript
[02:08]什么藏在DOTA2 TI9“小紫本”里?斧王历险记告诉你!
2019/05/17 DOTA
在阿里云服务器上配置CentOS+Nginx+Python+Flask环境
2016/06/18 Python
使用实现pandas读取csv文件指定的前几行
2018/04/20 Python
解决安装python库时windows error5 报错的问题
2018/10/21 Python
一篇文章了解Python中常见的序列化操作
2019/06/20 Python
Python实现个人微信号自动监控告警的示例
2019/07/03 Python
Django文件存储 默认存储系统解析
2019/08/02 Python
Python 实现加密过的PDF文件转WORD格式
2020/02/04 Python
容易被忽略的Python内置类型
2020/09/03 Python
python使用bs4爬取boss直聘静态页面
2020/10/10 Python
大学生毕业自荐信
2013/10/10 职场文书
成品仓管员工作职责
2013/12/29 职场文书
大三学生入党思想汇报
2014/01/02 职场文书
教师校本培训方案
2014/02/26 职场文书
村党支部对照检查材料思想汇报
2014/09/28 职场文书
费用申请报告范文
2015/05/15 职场文书
IDEA 链接Mysql数据库并执行查询操作的完整代码
2021/05/20 MySQL
python基础入门之普通操作与函数(三)
2021/06/13 Python
在Spring-Boot中如何使用@Value注解注入集合类
2021/08/02 Java/Android
python读取并查看npz/npy文件数据以及数据显示方法
2022/04/14 Python