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应用的教程
Apr 16 Python
Python加载带有注释的Json文件实例
May 23 Python
Python Logging 日志记录入门学习
Jun 02 Python
Python实现提取XML内容并保存到Excel中的方法
Sep 01 Python
Python重新加载模块的实现方法
Oct 16 Python
使用urllib库的urlretrieve()方法下载网络文件到本地的方法
Dec 19 Python
python pytest进阶之conftest.py详解
Jun 27 Python
python 设置输出图像的像素大小方法
Jul 04 Python
关于Keras Dense层整理
May 21 Python
Spring @Enable模块驱动原理及使用实例
Jun 23 Python
python实现简单的名片管理系统
Apr 26 Python
python 爬取哔哩哔哩up主信息和投稿视频
Jun 07 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下获取Discuz论坛登录用户名、用户组、用户ID等信息的实现代码
2010/12/29 PHP
php函数array_merge用法一例(合并同类数组)
2013/02/03 PHP
php实现读取和写入tab分割的文件
2015/06/01 PHP
javascript基于jQuery的表格悬停变色/恢复,表格点击变色/恢复,点击行选Checkbox
2008/08/05 Javascript
jQuery 学习 几种常用方法
2009/06/11 Javascript
Javascript 异步加载详解(浏览器在javascript的加载方式)
2012/05/20 Javascript
JavaScript获取一个范围内日期的方法
2015/04/24 Javascript
jQuery平滑旋转幻灯片特效代码分享
2015/09/07 Javascript
JS中多步骤多分步的StepJump组件实例详解
2016/04/01 Javascript
浅谈js中startsWith 函数不能在任何浏览器兼容的问题
2017/03/01 Javascript
浅谈js获取ModelAndView值的问题
2018/03/28 Javascript
JS实现读取xml内容并输出到div中的方法示例
2018/04/19 Javascript
浅谈Webpack打包优化技巧
2018/06/12 Javascript
JavaScript实现动态添加、移除元素或属性的方法分析
2019/01/03 Javascript
js实现点击生成随机div
2020/01/16 Javascript
一篇文章看懂JavaScript中的回调
2021/01/05 Javascript
Python中使用动态变量名的方法
2014/05/06 Python
使用XML库的方式,实现RPC通信的方法(推荐)
2017/06/14 Python
Python OpenCV实现图片上输出中文
2018/01/22 Python
python 去除txt文本中的空格、数字、特定字母等方法
2018/07/24 Python
python导入坐标点的具体操作
2019/05/10 Python
python实现感知机线性分类模型示例代码
2019/06/02 Python
Python使用itchat 功能分析微信好友性别和位置
2019/08/05 Python
vim自动补全插件YouCompleteMe(YCM)安装过程解析
2019/10/21 Python
python 实现线程之间的通信示例
2020/02/14 Python
Python3创建Django项目的几种方法(3种)
2020/06/03 Python
如何基于python把文字图片写入word文档
2020/07/31 Python
用 Django 开发一个 Python Web API的方法步骤
2020/12/03 Python
纯css3实现的竖形无限级导航
2014/12/10 HTML / CSS
DC Shoes官网:美国滑板鞋和服饰品牌
2017/09/03 全球购物
TIME时代杂志台湾总代理:台时亚洲
2018/10/22 全球购物
应届生会计电算化求职信
2013/10/03 职场文书
自我鉴定写作要点
2014/01/17 职场文书
假面舞会策划方案
2014/05/29 职场文书
2014年医生工作总结
2014/11/21 职场文书
高中化学教学反思
2016/02/22 职场文书