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使用自定义user-agent抓取网页的方法
Apr 15 Python
详解Golang 与python中的字符串反转
Jul 21 Python
python实现人脸识别代码
Nov 08 Python
详解Python判定IP地址合法性的三种方法
Mar 06 Python
基于Python中求和函数sum的用法详解
Jun 28 Python
IntelliJ IDEA安装运行python插件方法
Dec 10 Python
django 外键model的互相读取方法
Dec 15 Python
Python 70行代码实现简单算式计算器解析
Aug 30 Python
pygame实现飞机大战
Mar 11 Python
python 递归调用返回None的问题及解决方法
Mar 16 Python
Python 通过正则表达式快速获取电影的下载地址
Aug 17 Python
python实现Nao机器人的单目测距
Sep 04 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
检查url链接是否已经有参数的php代码 添加 ? 或 &amp;
2010/02/09 PHP
php实现的获取网站备案信息查询代码(360)
2013/09/23 PHP
php中常量DIRECTORY_SEPARATOR用法深入分析
2014/11/14 PHP
PHP实现股票趋势图和柱形图
2015/02/07 PHP
php解析http获取的json字符串变量总是空白null
2015/03/02 PHP
解决Laravel 不能创建 migration 的问题
2019/10/09 PHP
laravel 5.5 关闭token的3种实现方式
2019/10/24 PHP
js鼠标及对象坐标控制属性详细解析
2013/12/14 Javascript
jquery form 加载数据示例
2014/04/21 Javascript
Jquery之Bind方法参数传递与接收的三种方法
2014/06/24 Javascript
推荐8款jQuery轻量级树形Tree插件
2014/11/12 Javascript
在JavaScript中处理时间之getHours()方法的使用
2015/06/10 Javascript
剖析Node.js异步编程中的回调与代码设计模式
2016/02/16 Javascript
用原生js统计文本行数的简单示例
2016/08/19 Javascript
微信小程序 MD5加密登录密码详解及实例代码
2017/01/12 Javascript
Angular.js前台传list数组由后台spring MVC接收数组示例代码
2017/07/31 Javascript
vue自定义filters过滤器
2018/04/26 Javascript
js实现二级菜单点击显示当前内容效果
2018/04/28 Javascript
微信小程序渲染性能调优小结
2019/07/30 Javascript
基于Layui自定义模块的使用方法详解
2019/09/14 Javascript
js中addEventListener()与removeEventListener()用法案例分析
2020/03/02 Javascript
js实现点赞按钮功能的实例代码
2020/03/06 Javascript
Vue-router 报错NavigationDuplicated的解决方法
2020/03/31 Javascript
Electron实现应用打包、自动升级过程解析
2020/07/07 Javascript
javascript中layim之查找好友查找群组
2021/02/06 Javascript
Python 普通最小二乘法(OLS)进行多项式拟合的方法
2018/12/29 Python
学习python的前途 python挣钱
2019/02/27 Python
python实现最大优先队列
2019/08/29 Python
Python函数的定义方式与函数参数问题实例分析
2019/12/26 Python
Python select及selectors模块概念用法详解
2020/06/22 Python
Python利用matplotlib绘制散点图的新手教程
2020/11/05 Python
《满井游记》教学反思
2014/02/26 职场文书
2016年过年放假安排通知
2015/08/18 职场文书
Python Pycharm虚拟下百度飞浆PaddleX安装报错问题及处理方法(亲测100%有效)
2021/05/24 Python
python 常用的异步框架汇总整理
2021/06/18 Python