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中 ? : 三元表达式的使用介绍
Oct 09 Python
Python数据类型详解(三)元祖:tuple
May 08 Python
利用Python批量提取Win10锁屏壁纸实战教程
Mar 27 Python
Python实现检测文件MD5值的方法示例
Apr 11 Python
Django rest framework实现分页的示例
May 24 Python
TensorFlow数据输入的方法示例
Jun 19 Python
python同时遍历数组的索引和值的实例
Nov 15 Python
Django处理Ajax发送的Get请求代码详解
Jul 29 Python
python爬虫豆瓣网的模拟登录实现
Aug 21 Python
python 基于pygame实现俄罗斯方块
Mar 02 Python
Python爬虫数据的分类及json数据使用小结
Mar 29 Python
Python打包为exe详细教程
May 18 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
终于听上了直流胆调频
2021/03/02 无线电
隐藏你的.php文件的实现方法
2007/03/19 PHP
php empty函数 使用说明
2009/08/10 PHP
php实现redis数据库指定库号迁移的方法
2015/01/14 PHP
php+mysql实现无限级分类
2015/11/11 PHP
在Mac OS的PHP环境下安装配置MemCache的全过程解析
2016/02/15 PHP
laravel框架之数据库查出来的对象实现转化为数组
2019/10/23 PHP
可以文本显示的公告栏的js代码
2007/03/11 Javascript
jQuery 性能优化指南 (1)
2009/05/21 Javascript
js获取某月的最后一天日期的简单实例
2013/06/22 Javascript
document.forms[].submit()使用介绍
2014/02/19 Javascript
jquery实现通用版鼠标经过淡入淡出效果
2014/06/15 Javascript
jQuery修改li下的样式以及li下的img的src的值的方法
2014/11/02 Javascript
Javascript调用函数方法的几种方式介绍
2015/03/20 Javascript
学习使用bootstrap的modal和carousel
2016/12/09 Javascript
ionic实现底部分享功能
2017/05/11 Javascript
详解VUE 定义全局变量的几种实现方式
2017/06/01 Javascript
微信小程序wx.navigateTo中events属性实现页面间通信传值,数据同步
2019/07/13 Javascript
基于纯JS实现多张图片的懒加载Lazy过程解析
2019/10/14 Javascript
[56:56]VG vs LGD 2019国际邀请赛淘汰赛 胜者组 BO3 第一场 8.22
2019/09/05 DOTA
Python3基础之基本运算符概述
2014/08/13 Python
pymongo为mongodb数据库添加索引的方法
2015/05/11 Python
使用Python神器对付12306变态验证码
2016/01/05 Python
python队列queue模块详解
2018/04/27 Python
python爬虫之模拟登陆csdn的实例代码
2018/05/18 Python
在python中计算ssim的方法(与Matlab结果一致)
2019/12/19 Python
在脚本中单独使用django的ORM模型详解
2020/04/01 Python
建材业务员岗位职责
2013/12/08 职场文书
如何填写个人简历自我评价
2013/12/10 职场文书
暖通工程师岗位职责
2014/06/12 职场文书
群众路线领导干部个人对照检查材料(集锦)
2014/09/23 职场文书
员工工作自我评价
2014/09/26 职场文书
党员干部学习十八届五中全会精神心得体会
2016/01/05 职场文书
2016先进集体事迹材料范文
2016/02/25 职场文书
致创业您:正能量激励人心句子(48条)
2019/08/15 职场文书
三星 3nm 芯片将于第二季度开始量产
2022/04/29 数码科技