pytest篇8-pytest之skip/skipif跳过用例

01

引言

上一篇总结了pytest.fixture()中scope参数四种不同纬度的运用,今天我们一起总结一下pytest中用例的跳过。其实前面unittest单元测试框架中有总结skip用例的跳过,大家可以一并看看前面的内容,一起灵活运用起来。

skip的用法
@pytest.mark.skip(reason="不想执行的缘由,执行时会输出reason内容")
skipif的用法
@pytest.mark.skipif(条件表达式, reason="")
当条件表达式返回True时,会跳过用例

02

函数级别跳过

#!/usr/bin/env python
-*- coding:utf-8 -*-
@Time : 2021/4/13 11:06 下午
@Author : Maynard
@File : test_07skip.py
@Software: PyCharm

import pytest
def test_pytest_1():
    print('case1')

@pytest.mark.skip()
def test_pytestt_2():
    print('case2')

@pytest.mark.skip(reason='功能暂时不支持')
def test_pytestt_3():
    print('case3')

@pytest.mark.skipif(1 == 1,reason='满足条件跳过此用例')
def test_pytest_4():
    print('case4')

@pytest.mark.skipif(1 == 2,reason='不满足条件不会跳过此用例')
def test_pytest_5():
    print('case5')

class TestDemo():
    def test_pytest_6(self):
        print('case6')

    @pytest.mark.skip()
    def test_pytest_7(self):
        print('case7')

    @pytest.mark.skip(reason='功能还不支持')
    def test_pytest_8(self):
        print('case8')

    @pytest.mark.skipif(1 == 1,reason='满足条件,跳过')
    def test_pytest_9(self):
        print('case9')

    @pytest.mark.skipif(1 == 2,reason='不满足条件不会跳过')
    def test_pytest_10(self):
        print('case10')

if __name__ == '__main__':
    pytest.main(['-s','test_07skip.py'])

运行结果

pytest篇8-pytest之skip/skipif跳过用例

4条(case1、case5、case6、case10)用例执行通过,其余6条用例被跳过

03

类级别跳过

跳过类中skip和skipif的用法一样,唯一的就是将skip或者skipif的装饰器放在要跳过的类上面,详见如下:

#!/usr/bin/env python
-*- coding:utf-8 -*-
@Time : 2021/4/14 10:27 下午
@Author : Maynard
@File : test_08skipclass.py
@Software: PyCharm

import pytest
def test_pytest_1():
    print('case1')

def test_pytest_2():
    print('case2')

class TestDemo1():
    def test_pytest_3(self):
        print('case3')

    def test_pytest_4(self):
        print('case4')

@pytest.mark.skip('跳过类TestDemo2')
class TestDemo2():
    def test_pytest_5(self):
        print('case5')

    def test_pytest_6(self):
        print('case6')

@pytest.mark.skipif(1==1,reason='条件满足,跳过TestDemo3')
class TestDemo3():
    def test_pytest_7(self):
        print('case7')

    def test_pytest_8(self):
        print('case8')

@pytest.mark.skipif(1==2,reason='不条件满足,不会跳过TestDemo4')
class TestDemo4():
    def test_pytest_9(self):
        print('case9')

    def test_pytest_10(self):
        print('case10')

if __name__ == '__main__':
    pytest.main(['-s','test_08skipclass.py'])

运行结果

pytest篇8-pytest之skip/skipif跳过用例

04

模块级别跳过

目录层级
pytestskipmodule包
-- test_09skipmodule01.py
-- test_10skipmodule02.py
-- test_11skipmodule03.py

test_09skipmodule01.py

#!/usr/bin/env python
-*- coding:utf-8 -*-
@Time : 2021/4/14 10:38 下午
@Author : Maynard
@File : test_09skipmodule01.py
@Software: PyCharm

import pytest
def test_pytest_1():
    print('case1')

def test_pytest_2():
    print('case2')

class TestDemo1():
    def test_pytest_3(self):
        print('case3')

    def test_pytest_4(self):
        print('case4')

test_10skipmodule02.py

#!/usr/bin/env python
-*- coding:utf-8 -*-
@Time : 2021/4/14 10:39 下午
@Author : Maynard
@File : test_10skipmodule02.py
@Software: PyCharm

import pytest
pytestmark = pytest.mark.skip(reason ='跳过test_10skipmodule02.py模块')
def test_pytest_5():
    print('case5')

def test_pytest_6():
    print('case6')

class TestDemo3():
    def test_pytest_7(self):
        print('case7')

    def test_pytest_8(self):
        print('case8')

test_11skipmodule03.py

#!/usr/bin/env python
-*- coding:utf-8 -*-
@Time : 2021/4/14 10:39 下午
@Author : Maynard
@File : test_11skipmodule03.py
@Software: PyCharm

import pytest
pytestmark = pytest.mark.skipif(1==1,reason='条件满足,跳过test_11skipmodule03.py模块')
def test_pytest_9():
    print('case9')

def test_pytest_10():
    print('case10')

class TestDemo4():
    def test_pytest_11(self):
        print('case11')

    def test_pytest_12(self):
        print('case12')

运行

# 先cd 进入pytestskipmodule包
pytest -s

运行结果:跳过了两个模块的case

pytest篇8-pytest之skip/skipif跳过用例

05

总结

  1. skipif 中的条件表达式可以使用变量,根据变量返回是True/False来判断是否跳过用例;
  2. 可以灵活运用跳过case、跳过类、跳过模块的纬度来进行实操;
  3. 当某些case不想运行在某些平台上,可以使用skip/skipif;
  4. 开发某些功能未实现,需要跳过某些case;

06

PS

  1. 后续会继续总结pytest框架,个人觉得最强py单元测试框架;
  2. 大家有问题,可以通过公众号首页添加作者微信,多交流、多沟通;
  3. 万丈高楼平地起,利用业余时间多总结,加油!!
  4. 最近本人工作比较忙,所以更新的比较少,后续会挤出时间常更新;

Original: https://blog.csdn.net/m0_47127594/article/details/115744236
Author: 拉菲学测试
Title: pytest篇8-pytest之skip/skipif跳过用例

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/773306/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

  • 新装Ubuntu系统基本环境安装配置(conda)

    nvcc –V 显示Cuda compilation tools, release 11.4, V11.4.48 则表示成功。 下载anaconda安装包,然后运行 bash Mi…

    Python 2023年9月8日
    056
  • 正则表达式

    文章目录 一、正则初体验 二、匹配规则 三、字符串方法的使用 ; 一、正则初体验 正则表达式是特殊的字符序列,利用事先定义好的特定字符以及他们的组合组成了一个规则,然后检查一个字符…

    Python 2023年9月30日
    059
  • golang开发:go并发的建议(完)

    上次说了一下Go语言布道师 Dave Cheney对Go并发的建议,个人觉得最重要的一条,这次主要想说一下这个。8.3. Never start a goroutine witho…

    Python 2023年10月19日
    042
  • python数据分析学习day08:柱状图

    柱状图: 柱状图是一种用矩形柱来表示数据分类的图表。 柱状图可以垂直绘制,也可以水平绘制。 它的高度与其所表示的数值成正比关系。 柱状图显示了不同类别之间的比较关系,图表的水平轴 …

    Python 2023年8月30日
    068
  • 毕业设计opencv 图像识别 指纹识别 – python

    文章目录 0 前言 1 课题背景 2 效果展示 3 具体实现 * 3.1 图像对比过滤 3.2 图像二值化 3.3 图像侵蚀细化 3.4 图像增强 3.5 特征点检测 4 Open…

    Python 2023年8月1日
    041
  • Python代码阅读(第34篇):列表元素出现频率字典

    本篇阅读的代码实现了从一个列表生成以其元素为key,以该元素出现频率为value的字典。 本篇阅读的代码片段来自于30-seconds-of-python。 Python 代码阅读…

    Python 2023年5月25日
    073
  • 学习groupby-agg用法

    groupby 分组数据如下 import pandas as pd df=pd.read_excel(‘销售明细.xlsx’) print(df) name 品种 数量 单价 金…

    Python 2023年8月20日
    062
  • 【无标题】

    帮助人们作出判断,以便采取适当行动 环境安装1.condahttps://www.anaconda.com/download/ 1395122641@qq.comSd7rx.pJb…

    Python 2023年8月27日
    052
  • 【自考】数据结构第四章树和二叉树,期末不挂科指南,第6篇

    章节简介 前5篇博客写的都是线性结构,对于有层级结构的数据需要用树形结构来描述 本章的重要知识点 理解有关树的基本概念和二叉树的基本概念 掌握二叉树的存储结构以及遍历方法 掌握树的…

    Python 2023年6月3日
    085
  • Python matplotlib 绘图代码

    啊哦~你想找的内容离你而去了哦 内容不存在,可能为如下原因导致: ① 内容还在审核中 ② 内容以前存在,但是由于不符合新 的规定而被删除 ③ 内容地址错误 ④ 作者删除了内容。 可…

    Python 2023年9月1日
    064
  • 爬取多页资讯到mysql_利用Scrapy框架爬取博客信息并存到mysql数据库

    一、所需要的库 (1)Scrapy (2)pymysql 二、 创建数据库和表 Create database hexun; Use hexun; Create table myh…

    Python 2023年10月5日
    036
  • 利用OpenCV检测人脸(python实现)

    摘要:人脸识别( Face Recognition)是当前一项非常热门的研究领域。这里介绍采用图像处理中的强大工具 OpenCV_实现简单的图片中人脸的检测,并在图像中标记出感兴趣…

    Python 2023年10月29日
    024
  • Tensorflow 窗口时间序列数据的处理

    Tensorflow 时间序列数据的处理 数据集简介 数据来源:Kaggle Ubiquant Market Prediction 数据集描述了多个投资项目在一个时间序列下的300…

    Python 2023年10月28日
    050
  • 【PyTorch】torch.utils.data.Dataset 介绍与实战

    训练模型一般都是先处理 数据的输入问题 和 预处理问题 。Pytorch提供了几个有用的工具:torch.utils.data.Dataset 类和 torch.utils.dat…

    Python 2023年8月2日
    069
  • matplotlib之直方图

    文本以及后续的系列文章中均会使用到numpy这个库,numpy是Python的一种开源的数值计算扩展,主要用来生产一些随机数作为绘图的原始数据。具体和安装matplotlib类似,…

    Python 2023年8月30日
    049
  • python3.8+pytest+selenium 上传文件方法

    1.安装pywinauto 2.安装pypiwin32 4.安装PyUserInput,pip install PyUserInput 5.代码: import pykeyboar…

    Python 2023年9月13日
    040
亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球