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 相关文章推荐
Python3读取UTF-8文件及统计文件行数的方法
May 22 Python
python实现数据图表
Jul 29 Python
Python及Django框架生成二维码的方法分析
Jan 31 Python
Python+selenium 获取浏览器窗口坐标、句柄的方法
Oct 14 Python
Python3 利用requests 库进行post携带账号密码请求数据的方法
Oct 26 Python
浅谈python在提示符下使用open打开文件失败的原因及解决方法
Nov 30 Python
解决python xx.py文件点击完之后一闪而过的问题
Jun 24 Python
wxPython实现分隔窗口
Nov 19 Python
python logging添加filter教程
Dec 24 Python
pytorch逐元素比较tensor大小实例
Jan 03 Python
Python-jenkins模块之folder相关操作介绍
May 12 Python
Python替换NumPy数组中大于某个值的所有元素实例
Jun 08 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+AJAX实现无刷新注册(带用户名实时检测)
2007/01/02 PHP
ThinkPHP通过AJAX返回JSON的两种实现方法
2014/12/18 PHP
php基于数组函数实现关联表的编辑操作示例
2017/07/04 PHP
PHP简单验证码功能机制实例详解
2019/03/27 PHP
利用ASP发送和接收XML数据的处理方法与代码
2007/11/13 Javascript
js函数使用技巧之 setTimeout(function(){},0)
2009/02/09 Javascript
jquery的Theme和Theme Switcher使用小结
2010/09/08 Javascript
jquery 取子节点及当前节点属性值的方法
2014/08/24 Javascript
javascript操作ul中li的方法
2015/05/14 Javascript
JS实现图片局部放大或缩小的方法
2016/08/20 Javascript
JS组件系列之MVVM组件构建自己的Vue组件
2017/04/28 Javascript
jQuery 开发之EasyUI 添加数据的实例
2017/09/26 jQuery
结合Vue控制字符和字节的显示个数的示例
2018/05/17 Javascript
JavaScript读写二进制数据的方法详解
2018/09/09 Javascript
Element-ui中元素滚动时el-option超出元素区域的问题
2019/05/30 Javascript
ECharts地图绘制和钻取简易接口详解
2019/07/12 Javascript
javascript写一个ajax自动拦截并下载数据代码实例
2019/09/07 Javascript
vue 返回上一页,页面样式错乱的解决
2019/11/14 Javascript
vue实现数据控制视图的原理解析
2020/01/07 Javascript
js回调函数原理与用法案例分析
2020/03/04 Javascript
使用JavaScript获取Django模板指定键值数据
2020/05/27 Javascript
详解vite2.0配置学习(typescript版本)
2021/02/25 Javascript
Python创建日历实例
2014/08/21 Python
Python字符串和字典相关操作的实例详解
2017/09/23 Python
python3实现随机数
2018/06/25 Python
python模块导入的方法
2019/10/24 Python
15个Pythonic的代码示例(值得收藏)
2020/10/29 Python
大学生学习生活的自我评价
2013/11/01 职场文书
会展中心部门工作职责
2013/11/27 职场文书
艺校音乐专业自我鉴定范文
2014/03/01 职场文书
市场营销方案范文
2014/03/11 职场文书
给校长的建议书500字
2014/05/15 职场文书
大一新生检讨书
2014/10/29 职场文书
女性健康知识讲座通知
2015/04/23 职场文书
Java 获取Word中所有的插入和删除修订的方法
2022/04/06 Java/Android
vue特效之翻牌动画
2022/04/20 Vue.js