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跳出循环语句continue与break的区别
Aug 25 Python
Python中一些自然语言工具的使用的入门教程
Apr 13 Python
Python的Django应用程序解决AJAX跨域访问问题的方法
May 31 Python
Python中 Lambda表达式全面解析
Nov 28 Python
Python中你应该知道的一些内置函数
Mar 31 Python
使用 Python 玩转 GitHub 的贡献板(推荐)
Apr 04 Python
详解numpy的argmax的具体使用
May 27 Python
python内置模块collections知识点总结
Dec 19 Python
pytorch forward两个参数实例
Jan 17 Python
解决阿里云邮件发送不能使用25端口问题
Aug 07 Python
python3获取控制台输入的数据的具体实例
Aug 16 Python
python多次执行绘制条形图
Apr 20 Python
Pytest中skip和skipif的具体使用方法
Python将CSV文件转化为HTML文件的操作方法
如何使用Tkinter进行窗口的管理与设置
Python 语言实现六大查找算法
详解MindSpore自定义模型损失函数
教你用python实现12306余票查询
python实现简易自习室座位预约系统
You might like
Fine Uploader文件上传组件应用介绍
2013/01/06 PHP
PHP实现把MySQL数据库导出为.sql文件实例(仿PHPMyadmin导出功能)
2014/05/10 PHP
php+mysql数据库实现无限分类的方法
2014/12/12 PHP
快速解决PHP调用Word组件DCOM权限的问题
2017/12/27 PHP
jquery获取文档高度和窗口高度汇总
2016/01/25 Javascript
使用AJAX实现Web页面进度条的实例分享
2016/05/06 Javascript
jQuery实现产品对比功能附源码下载
2016/08/09 Javascript
js 获取站点应用名的简单实例
2016/08/18 Javascript
微信小程序 progress组件详解及实例代码
2016/10/25 Javascript
bootstrap侧边栏圆点导航
2017/01/11 Javascript
javaScript中的空值和假值
2017/12/18 Javascript
Vue组件系列开发之模态框
2019/04/18 Javascript
vue使用showdown并实现代码区域高亮的示例代码
2019/10/17 Javascript
Postman内建变量常用方法实例解析
2020/07/28 Javascript
Python中的startswith和endswith函数使用实例
2014/08/25 Python
python中的hashlib和base64加密模块使用实例
2014/09/02 Python
Go语言基于Socket编写服务器端与客户端通信的实例
2016/02/19 Python
运行django项目指定IP和端口的方法
2018/05/14 Python
python中使用psutil查看内存占用的情况
2018/06/11 Python
Python3.5基础之函数的定义与使用实例详解【参数、作用域、递归、重载等】
2019/04/26 Python
Python监控服务器实用工具psutil使用解析
2019/12/19 Python
opencv之为图像添加边界的方法示例
2019/12/26 Python
用Python爬取LOL所有的英雄信息以及英雄皮肤的示例代码
2020/07/13 Python
用python给csv里的数据排序的具体代码
2020/07/17 Python
Pyecharts 中Geo函数常用参数的用法说明
2021/02/01 Python
python 将Excel转Word的示例
2021/03/02 Python
仿酷狗html5手机音乐播放器主要部分代码
2013/05/15 HTML / CSS
作文评语大全
2014/04/23 职场文书
社团活动总结模板
2014/06/30 职场文书
艺术学院毕业生求职信
2014/07/09 职场文书
教师工作自我鉴定范文
2014/09/14 职场文书
安全月宣传标语
2014/10/07 职场文书
公司股东出资证明书
2014/11/01 职场文书
导游词之铁岭象牙山
2019/12/06 职场文书
asyncio异步编程之Task对象详解
2022/03/13 Python
《群青的幻想曲》京力秋树角色PV公开
2022/04/08 日漫