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 version 2.7 required, which was not found in the registry
Aug 26 Python
python实现删除文件与目录的方法
Nov 10 Python
Python中使用不同编码读写txt文件详解
May 28 Python
简单介绍使用Python解析并修改XML文档的方法
Oct 15 Python
Tornado协程在python2.7如何返回值(实现方法)
Jun 22 Python
火车票抢票python代码公开揭秘!
Mar 08 Python
Django开发中复选框用法示例
Mar 20 Python
python合并已经存在的sheet数据到新sheet的方法
Dec 11 Python
浅谈python requests 的put, post 请求参数的问题
Jan 02 Python
python实现诗歌游戏(类继承)
Feb 26 Python
python实现DEM数据的阴影生成的方法
Jul 23 Python
基于python的BP神经网络及异或实现过程解析
Sep 30 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
简单分析ucenter 会员同步登录通信原理
2014/08/25 PHP
总结一些js自定义的函数
2006/08/05 Javascript
JS弹出层的显示与隐藏示例代码
2013/12/27 Javascript
JavaScript中Math对象方法使用概述
2014/01/02 Javascript
js用typeof方法判断undefined类型
2014/07/15 Javascript
JS实现双击编辑可修改状态的方法
2015/08/14 Javascript
jQuery改变form表单的action,并进行提交的实现代码
2016/05/25 Javascript
node.js请求HTTPS报错:UNABLE_TO_VERIFY_LEAF_SIGNATURE\的解决方法
2016/12/18 Javascript
vue2笔记 — vue-router路由懒加载的实现
2017/03/03 Javascript
JS使用正则表达式验证身份证号码
2017/06/23 Javascript
浅谈express 中间件机制及实现原理
2017/08/31 Javascript
JavaScript事件对象event用法分析
2018/07/27 Javascript
vue 进阶之实现父子组件间的传值
2019/04/26 Javascript
微信小程序用户拒绝授权的处理方法详解
2019/09/20 Javascript
js中的面向对象之对象常见创建方法详解
2019/12/16 Javascript
Vue的状态管理vuex使用方法详解
2020/02/05 Javascript
原生js实现下拉框选择组件
2021/01/20 Javascript
在Python中利用Into包整洁地进行数据迁移的教程
2015/03/30 Python
python3.4用循环往mysql5.7中写数据并输出的实现方法
2017/06/20 Python
Python中的 enum 模块源码详析
2019/01/09 Python
python把ipynb文件转换成pdf文件过程详解
2019/07/09 Python
如何在windows下安装Pycham2020软件(方法步骤详解)
2020/05/03 Python
利用PyTorch实现VGG16教程
2020/06/24 Python
python logging 重复写日志问题解决办法详解
2020/08/04 Python
解决pytorch 的state_dict()拷贝问题
2021/03/03 Python
党课培训主持词
2014/04/01 职场文书
机械系毕业生求职信
2014/05/28 职场文书
国际商务英语专业求职信
2014/07/08 职场文书
入党积极分子对十八届四中全会期盼的思想汇报
2014/10/17 职场文书
学习党章的体会
2014/11/07 职场文书
违反工作规定检讨书范文
2014/12/14 职场文书
公司员工违纪检讨书
2015/05/05 职场文书
关于幸福的感言
2015/08/03 职场文书
初中政治教学反思
2016/02/23 职场文书
手写实现JS中的new
2021/11/07 Javascript
MySQL创建管理HASH分区
2022/04/13 MySQL