Pytest如何使用skip跳过执行测试


Posted in Python onAugust 13, 2020

1、@pytest.mark.skip(reason=" ") -- 跳过执行测试函数

可传入一个非必须参数reason表示原因

import pytest
@pytest.mark.skip(reason="no reason")
def test_01():
  print("---用例a执行---")
class TestCase():
  @pytest.mark.skip(reason="no reason")
  def test_02(self):
    print("---用例b执行---")

  def test_03(self):
    print("---用例c执行---")

输出结果:

test_fixture2.py ss---用例c执行---

2、@pytest.mark.skipif(condition...) -- 若满足condition,则跳过测试函数

传入condition参数为判断条件,可以选择传入非必须参数reason;如果多个标签一起使用,满足其中一个跳过条件则会跳过该测试函数。

import pytest
def test_01():
  print("---用例a执行---")
class TestCase():
  #当多个@pytest.mark.skipif()标签时,若满足一个,则跳过测试函数
  @pytest.mark.skipif(condition='a' >= 'b', reason="no reason")
  @pytest.mark.skipif(condition='a' <= 'b', reason="no reason")
  def test_02(self):
    print("---用例b执行---")

  def test_03(self):
    print("---用例c执行---")

输出结果:

test_fixture2.py ---用例a执行---
.s---用例c执行---

3、自定义@pytest.mark.skip()标签

myskip = pytest.mark.skip() 或 myskip = pytest.mark.skipif(condition=...)

装饰时用该变量代替标签即可:@myskip

import pytest
# myskip = pytest.mark.skip()
myskip = pytest.mark.skipif(condition=2>1, reason="no reason")

@myskip
def test_01():
  print("---用例a执行---")

class TestCase():

  @myskip
  def test_02(self):
    print("---用例b执行---")

  def test_03(self):
    print("---用例c执行---")

输出结果:

test_fixture2.py ss---用例c执行---

4、通过pytest.skip()方法跳过测试函数

import pytest

def test_01():
  pytest.skip(msg="no reason")
  print("---用例a执行---")

class TestCase():

  def test_02(self):
    pytest.skip()
    print("---用例b执行---")

  def test_03(self):
    print("---用例c执行---")

输出结果:

test_fixture2.py ss---用例c执行--

5、跳过测试类

跳过测试类其实和跳过测试方法一样,使用@pytest.mark.skip()和@pytest.mark.skipif()两个标签,用他们装饰测试类就好啦。

import pytest
myskip = pytest.mark.skip(reason="no reason")
def test_01():
  print("---用例a执行---")
@myskip
class TestCase():
  def test_02(self):
    print("---用例b执行---")
  def test_03(self):
    print("---用例c执行---")

输出结果:

test_fixture2.py ---用例a执行---

6、跳过模块

使用pytestmark(不可更改变量名)变量,让他等于标签即可。

import pytest

pytestmark = pytest.mark.skip(condition=2>1, reason='no reason')

def test_01():
  print("---用例a执行---")

class TestCase():

  def test_02(self):
    print("---用例b执行---")

  def test_03(self):
    print("---用例c执行---")

输出结果:

test_fixture2.py sss

7、pycharm中运行多个测试文件

依次将要运行的文件名写在后面即可,用逗号隔开,无需链表元组等形式。

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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
python解析模块(ConfigParser)使用方法
Dec 10 Python
Python实现国外赌场热门游戏Craps(双骰子)
Mar 31 Python
Python实现基于二叉树存储结构的堆排序算法示例
Dec 08 Python
Python实现的用户登录系统功能示例
Feb 05 Python
Python定时任务sched模块用法示例
Jul 16 Python
在PyCharm下使用 ipython 交互式编程的方法
Jan 17 Python
python中的decimal类型转换实例详解
Jun 26 Python
Python数据类型之列表和元组的方法实例详解
Jul 08 Python
在Django下创建项目以及设置settings.py教程
Dec 03 Python
解决python ThreadPoolExecutor 线程池中的异常捕获问题
Apr 08 Python
Python自带的IDE在哪里
Jul 01 Python
Python使用永中文档转换服务
May 06 Python
matplotlib基础绘图命令之bar的使用方法
Aug 13 #Python
Python logging模块原理解析及应用
Aug 13 #Python
matplotlib基础绘图命令之imshow的使用
Aug 13 #Python
使用jupyter notebook运行python和R的步骤
Aug 13 #Python
matplotlib基础绘图命令之errorbar的使用
Aug 13 #Python
Python如何读写CSV文件
Aug 13 #Python
区分python中的进程与线程
Aug 13 #Python
You might like
PHP合并两个或多个数组的方法
2019/01/20 PHP
基于jquery的bankInput银行卡账号格式化
2012/08/22 Javascript
解决javascript:window.close()在chrome,Firefox下失效的问题
2013/05/07 Javascript
javascript检查浏览器是否支持flash的实现代码
2014/08/14 Javascript
jQuery选择器源码解读(四):tokenize方法的Expr.preFilter
2015/03/31 Javascript
浅谈JavaScript中的string拥有方法的原因
2015/08/28 Javascript
基于Flowplayer打造一款免费的WEB视频播放器附源码
2015/09/06 Javascript
JS实现的自定义显示加载等待图片插件(loading.gif)
2016/06/17 Javascript
Javascript实现异步编程的过程
2018/06/18 Javascript
解决vue动态为数据添加新属性遇到的问题
2018/09/18 Javascript
vue 弹框产生的滚动穿透问题的解决
2018/09/21 Javascript
小程序实现横向滑动日历效果
2019/10/21 Javascript
[01:03:37]Secret vs VGJ.S Supermajor小组赛C组 BO3 第二场 6.3
2018/06/04 DOTA
python登录QQ邮箱发信的实现代码
2013/02/10 Python
简单实现python爬虫功能
2015/12/31 Python
浅谈django中的认证与登录
2016/10/31 Python
python操作oracle的完整教程分享
2018/01/30 Python
安装python时MySQLdb报错的问题描述及解决方法
2018/03/20 Python
解决python os.mkdir创建目录失败的问题
2018/10/16 Python
pow在python中的含义及用法
2019/07/11 Python
django之静态文件 django 2.0 在网页中显示图片的例子
2019/07/28 Python
python ctypes库2_指定参数类型和返回类型详解
2019/11/19 Python
Python3 Tensorlfow:增加或者减小矩阵维度的实现
2020/05/22 Python
将pycharm配置为matlab或者spyder的用法说明
2020/06/08 Python
python实现文件分片上传的接口自动化
2020/11/19 Python
selenium3.0+python之环境搭建的方法步骤
2021/02/01 Python
Python命令行参数argv和argparse该如何使用
2021/02/08 Python
HTML5调用手机摄像头拍照的实现思路及代码
2014/06/15 HTML / CSS
中学实习教师自我鉴定
2013/12/12 职场文书
最新奶茶店创业计划书
2014/01/25 职场文书
英语一分钟演讲稿
2014/04/29 职场文书
学习礼仪心得体会
2014/09/01 职场文书
民政局标准版离婚协议书
2014/12/01 职场文书
机动车交通事故协议书
2015/01/29 职场文书
详解Python为什么不用设计模式
2021/06/24 Python
JS数组方法some、every和find的使用详情
2021/10/05 Javascript