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计算圆周长、面积、球体体积并画出圆
Apr 08 Python
python在控制台输出进度条的方法
Jun 20 Python
Python基于socket实现简单的即时通讯功能示例
Jan 16 Python
Python匿名函数及应用示例
Apr 09 Python
基于Python的PIL库学习详解
May 10 Python
pyqt5 实现多窗口跳转的方法
Jun 19 Python
Python编程实现tail-n查看日志文件的方法
Jul 08 Python
python实现键盘输入的实操方法
Jul 16 Python
Python实现TCP通信的示例代码
Sep 09 Python
python如何从文件读取数据及解析
Sep 19 Python
Python3爬虫ChromeDriver的安装实例
Feb 06 Python
Python GUI编程之tkinter 关于 ttkbootstrap 的使用详解
Mar 03 Python
Pytest中skip和skipif的具体使用方法
Python将CSV文件转化为HTML文件的操作方法
如何使用Tkinter进行窗口的管理与设置
Python 语言实现六大查找算法
详解MindSpore自定义模型损失函数
教你用python实现12306余票查询
python实现简易自习室座位预约系统
You might like
攻克CakePHP系列一 连接MySQL数据库
2008/10/22 PHP
用Simple Excel导出xls实现方法
2012/12/06 PHP
php随机取mysql记录方法小结
2014/12/27 PHP
codeigniter中view通过循环显示数组数据的方法
2015/03/20 PHP
thinkPHP商城公告功能开发问题分析
2016/12/01 PHP
Laravel框架实现的使用smtp发送邮件功能示例
2019/03/12 PHP
php依赖注入知识点详解
2019/09/23 PHP
laravel框架 api自定义全局异常处理方法
2019/10/11 PHP
一个js封装的不错的选项卡效果代码
2008/02/15 Javascript
js直接编辑当前cookie的脚本
2008/09/14 Javascript
标题过长使用javascript按字节截取字符串
2014/04/24 Javascript
jquery 新建的元素事件绑定问题解决方案
2014/06/12 Javascript
如何在node的express中使用socket.io
2014/12/15 Javascript
把json格式的字符串转换成javascript对象或数组的方法总结
2016/11/03 Javascript
Vuejs 页面的区域化与组件封装的实现
2017/09/11 Javascript
js封装成插件_Canvas统计图插件编写实例
2017/09/12 Javascript
在vue项目中安装使用Mint-UI的方法
2017/12/27 Javascript
微信小程序基于canvas渐变实现的彩虹效果示例
2019/05/03 Javascript
简单了解Vue + ElementUI后台管理模板
2020/04/07 Javascript
详解ES6新增字符串扩张方法includes()、startsWith()、endsWith()
2020/05/12 Javascript
在vue项目中 实现定义全局变量 全局函数操作
2020/10/26 Javascript
布同 统计英文单词的个数的python代码
2011/03/13 Python
零基础学Python(一)Python环境安装
2014/08/20 Python
python实现数组插入新元素的方法
2015/05/22 Python
使用 Python 实现微信公众号粉丝迁移流程
2018/01/03 Python
python使用Flask操作mysql实现登录功能
2018/05/14 Python
详解python的四种内置数据结构
2019/03/19 Python
python 写一个性能测试工具(一)
2020/10/24 Python
Html5 APP中监听返回事件处理的方法示例
2018/03/15 HTML / CSS
Clarks西班牙官方在线商店:clarks鞋
2019/05/03 全球购物
慕尼黑山地运动、户外服装和体育用品专家:Sporthaus Schuster
2019/08/27 全球购物
学术会议邀请函范文
2014/01/22 职场文书
运动会广播稿200米
2014/01/27 职场文书
八项规定个人对照检查材料思想汇报
2014/09/25 职场文书
违反单位工作制度检讨书
2014/10/25 职场文书
Nginx+Windows搭建域名访问环境的操作方法
2022/03/17 Servers