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实现杨辉三角思路
Jul 14 Python
Python实现动态加载模块、类、函数的方法分析
Jul 18 Python
Python实现求两个csv文件交集的方法
Sep 06 Python
Python异常对代码运行性能的影响实例解析
Feb 08 Python
python网络爬虫学习笔记(1)
Apr 09 Python
Python实现的txt文件去重功能示例
Jul 07 Python
pandas每次多Sheet写入文件的方法
Dec 10 Python
Python3内置模块之base64编解码方法详解
Jul 13 Python
如何基于python操作json文件获取内容
Dec 24 Python
python判断变量是否为int、字符串、列表、元组、字典的方法详解
Feb 13 Python
利用python生成照片墙的示例代码
Apr 09 Python
python爬取网页版QQ空间,生成各类图表
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中鲜为人知的10个函数
2014/02/28 PHP
PHP错误Warning: Cannot modify header information - headers already sent by解决方法
2014/09/27 PHP
[原创]PHP简单开启curl的方法(测试可行)
2016/01/11 PHP
JS解密入门之凭直觉解
2008/06/25 Javascript
Jquery操作Select 简单方便 一个js插件搞定
2009/11/12 Javascript
jquery 问答知识整理
2010/02/11 Javascript
JavaScript 原型链学习总结
2010/10/29 Javascript
IE的事件传递-event.cancelBubble示例介绍
2014/01/12 Javascript
一个JavaScript的求爱小特效
2014/05/09 Javascript
JQuery弹出炫丽对话框的同时让背景变灰色
2014/05/22 Javascript
QQ登录背景闪动效果附效果演示源码下载
2015/09/22 Javascript
Jquery实现的简单轮播效果【附实例】
2016/04/19 Javascript
jQuery基础的工厂函数以及定时器的经典实例分析
2016/05/20 Javascript
分享javascript实现的冒泡排序代码并优化
2016/06/05 Javascript
Bootstrap 实现查询的完美方法
2016/10/26 Javascript
微信小程序 删除项目工程实现步骤
2016/11/10 Javascript
关于webpack2和模块打包的新手指南(小结)
2017/08/07 Javascript
Vue的MVVM实现方法
2017/08/16 Javascript
ECMAScript6变量的解构赋值实例详解
2017/09/19 Javascript
详解基于vue-router的动态权限控制实现方案
2017/09/28 Javascript
vue slot 在子组件中显示父组件传递的模板
2018/03/02 Javascript
Vue中util的工具函数实例详解
2019/07/08 Javascript
Moment.js实现多个同时倒计时
2019/08/26 Javascript
layui的表单提交以及验证和修改弹框的实例
2019/09/09 Javascript
Python用UUID库生成唯一ID的方法示例
2016/12/15 Python
Python3使用pandas模块读写excel操作示例
2018/07/03 Python
python 实现矩阵按对角线打印
2019/11/29 Python
python中执行smtplib失败的处理方法
2020/07/01 Python
python 实现弹球游戏的示例代码
2020/11/17 Python
公共事业管理本科生求职信
2013/10/07 职场文书
yy结婚证婚词
2014/01/10 职场文书
动漫专业高职生职业生涯规划书
2014/02/15 职场文书
新年晚会主持词
2014/03/24 职场文书
2014年大学生党课心得体会范文
2014/03/29 职场文书
2016大学生暑期社会实践心得体会
2016/01/14 职场文书
HTML5+CSS+JavaScript实现捉虫小游戏设计和实现
2021/10/16 HTML / CSS