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 相关文章推荐
在Python3中初学者应会的一些基本的提升效率的小技巧
Mar 31 Python
Django中几种重定向方法
Apr 28 Python
python对数组进行反转的方法
May 20 Python
python中getaddrinfo()基本用法实例分析
Jun 28 Python
利用Python如何生成便签图片详解
Jul 09 Python
python+splinter实现12306网站刷票并自动购票流程
Sep 25 Python
PyQt5基本控件使用详解:单选按钮、复选框、下拉框
Aug 05 Python
python 字典 setdefault()和get()方法比较详解
Aug 07 Python
Python 线程池用法简单示例
Oct 02 Python
Python aiohttp百万并发极限测试实例分析
Oct 26 Python
让你的Python代码实现类型提示功能
Nov 19 Python
Keras使用ImageNet上预训练的模型方式
May 23 Python
Pytest中skip和skipif的具体使用方法
Python将CSV文件转化为HTML文件的操作方法
如何使用Tkinter进行窗口的管理与设置
Python 语言实现六大查找算法
详解MindSpore自定义模型损失函数
教你用python实现12306余票查询
python实现简易自习室座位预约系统
You might like
PHP随机数生成代码与使用实例分析
2011/04/08 PHP
简单谈谈favicon
2015/06/10 PHP
用 JSON 处理缓存
2007/04/27 Javascript
jQuery 页面载入进度条实现代码
2009/02/08 Javascript
Area 区域实现post提交数据的js写法
2014/04/22 Javascript
javascript版的in_array函数(判断数组中是否存在特定值)
2014/05/09 Javascript
nodejs的10个性能优化技巧
2014/07/15 NodeJs
jquery实现红色竖向多级向右展开的导航菜单效果
2015/08/31 Javascript
JavaScript生成验证码并实现验证功能
2016/09/24 Javascript
JS实现超简单的汉字转拼音功能示例
2016/12/22 Javascript
js实现下一页页码效果
2017/03/07 Javascript
Angular angular-file-upload文件上传的示例代码
2018/08/23 Javascript
bootstrap table插件动态加载表头
2019/07/19 Javascript
使用layui的layer组件做弹出层的例子
2019/09/27 Javascript
node.js使用http模块创建服务器和客户端完整示例
2020/02/10 Javascript
微信小程序上传帖子的实例代码(含有文字图片的微信验证)
2020/07/11 Javascript
vue监听滚动事件的方法
2020/12/21 Vue.js
python中enumerate的用法实例解析
2014/08/18 Python
python进程管理工具supervisor使用实例
2014/09/17 Python
python使用PIL缩放网络图片并保存的方法
2015/04/24 Python
Python实现周期性抓取网页内容的方法
2015/11/04 Python
Python如何为图片添加水印
2016/11/25 Python
python利用sklearn包编写决策树源代码
2017/12/21 Python
用Python实现KNN分类算法
2017/12/22 Python
python GUI实现小球满屏乱跑效果
2019/05/09 Python
Python Gitlab Api 使用方法
2019/08/28 Python
Python脚本导出为exe程序的方法
2020/03/25 Python
初中科学教学反思
2014/01/21 职场文书
爱国卫生月实施方案
2014/02/21 职场文书
领导干部廉政自律承诺书
2014/05/26 职场文书
应届生求职信
2014/05/31 职场文书
五四青年节的活动方案
2014/08/20 职场文书
2016年秋季新学期致辞
2015/07/30 职场文书
篮球拉拉队口号
2015/12/25 职场文书
python opencv旋转图片的使用方法
2021/06/04 Python
世界无敌的ICOM IC-R9500宽频接收机
2022/03/25 无线电