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中的实现
May 04 Python
详解Python3操作Mongodb简明易懂教程
May 25 Python
简单谈谈python中的语句和语法
Aug 10 Python
python 寻找优化使成本函数最小的最优解的方法
Dec 28 Python
面向初学者的Python编辑器Mu
Oct 08 Python
Python动态赋值的陷阱知识点总结
Mar 17 Python
Python常见读写文件操作实例总结【文本、json、csv、pdf等】
Apr 15 Python
Kali Linux安装ipython2 和 ipython3的方法
Jul 11 Python
如何通过python实现全排列
Feb 11 Python
python中68个内置函数的总结与介绍
Feb 24 Python
解决jupyter notebook图片显示模糊和保存清晰图片的操作
Apr 24 Python
python爬取某网站原图作为壁纸
Jun 02 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函数
2010/01/11 PHP
mysqli扩展无法在PHP7下升级问题的解决
2019/09/10 PHP
js写一个弹出层并锁屏效果实现代码
2012/12/07 Javascript
jQuery插件jQuery-JSONP开发ajax调用使用注意事项
2013/11/22 Javascript
IE下双击checkbox反应延迟问题的解决方法
2014/03/27 Javascript
jQuery实现字符串按指定长度加入特定内容的方法
2015/03/11 Javascript
javascript中动态函数用法实例分析
2015/05/14 Javascript
JavaScript获取两个数组交集的方法
2015/06/09 Javascript
jquery读写cookie操作实例分析
2015/12/24 Javascript
JavaScript预解析及相关技巧分析
2016/04/21 Javascript
node.js express安装及示例网站搭建方法(分享)
2016/08/22 Javascript
vue使用vue-cli快速创建工程
2017/07/28 Javascript
angular2系列之路由转场动画的示例代码
2017/11/09 Javascript
手淘flexible.js框架使用和源代码讲解小结
2018/10/15 Javascript
小程序实现五星点评效果
2018/11/03 Javascript
mpvue开发音频类小程序踩坑和建议详解
2019/03/12 Javascript
详解原生JS动态添加和删除类
2019/03/26 Javascript
全面了解JavaScript的作用域链
2019/04/03 Javascript
基于react项目打包css引用路径错误解决方案
2020/10/28 Javascript
python 出现SyntaxError: non-keyword arg after keyword arg错误解决办法
2017/02/14 Python
Python编程实现的简单Web服务器示例
2017/06/22 Python
基于python 爬虫爬到含空格的url的处理方法
2018/05/11 Python
我用Python抓取了7000 多本电子书案例详解
2019/03/25 Python
python中多个装饰器的调用顺序详解
2019/07/16 Python
Python与C/C++的相互调用案例
2021/03/04 Python
美国购买当代和现代家具网站:MODTEMPO
2018/07/20 全球购物
可持续木材、生态和铝制太阳镜:Proof Eyewear
2019/07/24 全球购物
介绍一下Linux文件的记录形式
2012/04/18 面试题
装饰活动策划方案
2014/02/11 职场文书
运动会标语
2014/06/21 职场文书
2015社区六五普法工作总结
2015/04/21 职场文书
工作失职自我检讨书
2015/05/05 职场文书
python解决12306登录验证码的实现
2021/04/18 Python
Python排序算法之插入排序及其优化方案详解
2021/06/11 Python
sql时间段切分实现每隔x分钟出一份高速门架车流量
2022/02/28 SQL Server
Android Flutter实现3D动画效果示例详解
2022/04/07 Java/Android