有时候,为了满足某些场景的需要,我们知道有些测试函数在这时候肯定不能执行,或者执行了也会失败。那么我们

可以选择去跳过这个测试函数,这样也就不会影响整体的测试函数运行效果,不至于在你运行的众多绿色通过的测试

用例中,给你加点红色的failed或者error。

举个例子,有些测试函数只能在windows上运行,那么换了个Linux平台就不可以,就需要跳过这个测试函数。再比如,

有些测试用例会有一些外部资源的依赖,像数据库,那么当数据库资源不可用的时候,也需要去跳过这个测试函数。

在pytest中提供了这样一个机制来跳过测试函数。

一、skip

用法很简单,只要在需要跳过的测试函数上面安上@pytest.mark.skip()即可。可以传递一个参数reason,填写

跳过的理由,不填也可以。

@pytest.mark.skip(reason="没啥理由,就不想执行")
def test_the_unknown():
...

运行结果:

test_skipif.py                                                          [100%]

============================= 1 skipped in 0.02s ==============================s
Skipped: 没啥理由,就不想执行 Process finished with exit code 0

可以看到,这个测试用例被成功跳过,并且还有reason的输出。不过pytest默认情况下,不会显示跳过的测试函数的详细信息,

避免输出与正常的混在一起,太乱。

二、pytest.skip(reason)

1. 在测试函数或者fixture函数里使用

除了上述用法,还可以用pytest.skip(reason)函数,在测试函数中或者setup函数里进行跳过。比如,当有些参数不符合你

的预期,那么就跳过后面的执行。

上示例代码:

import pytest

@pytest.fixture()
def demo_fixture(request):
test_input = request.param
if test_input == 3:
pytest.skip("传入的值等于3就跳过执行") @pytest.mark.parametrize("demo_fixture", [1, 3], indirect=True)
def test_the_unknown3(demo_fixture):
... if __name__ == "__main__":
pytest.main(["-s", '-r' "test_skipif.py"])

在上述代码里,我在测试函数test_the_unknown3里做了一个参数化,并且在fixture函数demo_fixture拿到这个参数。

按理来说,参数化了1和3,所以测试函数会执行2次。

我在fixture函数里加了判断,当拿到的参数,等于3的时候,就跳过执行。所以最终的运行结果应该是,1执行,3跳过。

test_skipif.py                                                         [100%]

======================== 1 passed, 1 skipped in 0.09s =========================.s
Skipped: 传入的值等于3就跳过执行 Process finished with exit code 0

结果符合预期。

这个用法,刚好解决了我一个实际问题。

那就是别的小组有不少人写case用的测试数据,是会通过别的case或者别的接口调用后传递过来的,那么当这些依赖有问题的时候

case运行就会有问题,导致误报。如果使用pytest.skip(reason)这个函数,那么可以有效缓解case误报的问题。

2. allow_module_level=True跳过整个模块

如果你需要判断某些条件符合时候,就要整个模块都跳过,就可以加上这个参数allow_module_level=True

import pytest

a = 1

if a != 3:
pytest.skip("a不等于3就跳过整个文件模块", allow_module_level=True) @pytest.fixture()
def demo_fixture(request):
test_input = request.param
if test_input == 3:
pytest.skip("传入的值等于3就跳过") @pytest.mark.parametrize("demo_fixture", [1, 3], indirect=True)
def test_the_unknown1(demo_fixture):
... def test_the_unknown2():
... def test_the_unknown3():
...

运行一下:

============================= 1 skipped in 0.02s ==============================
Skipped: a不等于3就跳过整个文件模块 Process finished with exit code 0

三、skipif

1. skipif有条件的跳过

上面提到了在函数里写判断,当满足某个条件时通过pytest.skip函数来跳过,其实还可以直接用skipif,同样可以

达到有条件地跳过某些内容的目的。

import sys

@pytest.mark.skipif(sys.version_info < (4, 0), reason="版本4.0以下就跳过执行")
def test_function():
print(sys.version_info)

运行结果:

test_module1.py                                                         [100%]

============================= 1 skipped in 0.02s ==============================s
Skipped: 版本4.0以下就跳过执行 Process finished with exit code 0

2.模块之间共享skip标记

比如说,我现在有2个测试模块,分别是test_module1.pytest_module2.py

我在test_module1.py当中,定义一个skipif作为marker 共享,也就是not_equal_5

那么在test_module2.py当中导入这个marker,就可以直接使用了,看代码效果:

# content of test_module1.py
import pytest
import sys version_judge = pytest.mark.skipif(
sys.version_info < (4, 0), reason="版本4.0以下就跳过执行"
) @version_judge
def test_the_unknown2():
... if __name__ == "__main__":
pytest.main(["-s", '-r' "test_module1.py"])

test_module2.py中导入marker使用,运行后的预期结果,应该是test_the_unknown1跳过执行。

# content of test_module2.py
import pytest
from interface.demo.test_module1 import version_judge @version_judge
def test_the_unknown1():
... def test_the_unknown2():
... if __name__ == "__main__":
pytest.main(["-s", '-r' "test_module2.py"])

运行下test_module2.py:

test_module2.py                                                        [100%]

======================== 1 passed, 1 skipped in 0.08s =========================s
Skipped: 版本4.0以下就跳过执行
.
Process finished with exit code 0

四、跳过类或模块下的所有测试函数

1. 跳过类下的所有测试函数

如果把skipif放在类上,这个类下面的所有测试函数都会跳过。

import pytest
import sys version_judge = pytest.mark.skipif(
sys.version_info < (4, 0), reason="版本4.0以下就跳过执行"
) @version_judge
class TestDemo(): def test_the_unknown2(self):
... def test_the_unknown3(self):
... def test_the_unknown1():
... if __name__ == "__main__":
pytest.main(["-s", '-r' "test_module1.py"])

运行结果,类TestDemo下的2个测试方法都会被跳过。

test_module1.py                                                       [100%]

======================== 1 passed, 2 skipped in 0.09s =========================s
Skipped: 版本4.0以下就跳过执行
s
Skipped: 版本4.0以下就跳过执行
.
Process finished with exit code 0

2.跳过模块下的所有测试函数

如果想跳过模块的所有测试函数,可以使用全局变量pytestmark:

import pytest
import sys pytestmark = pytest.mark.skipif(sys.version_info < (4, 0), reason="版本4.0以下就跳过执行") class TestDemo(): def test_the_unknown2(self):
... def test_the_unknown3(self):
... def test_the_unknown1():
... if __name__ == "__main__":
pytest.main(["-s", '-r' "test_module1.py"])

运行,模块下的3个测试都会被跳过

test_module1.py                                                       [100%]

============================= 3 skipped in 0.02s ==============================s
Skipped: 版本4.0以下就跳过执行
s
Skipped: 版本4.0以下就跳过执行
s
Skipped: 版本4.0以下就跳过执行 Process finished with exit code 0

另外,如果多个skipif装饰器应用于同一个测试函数,只要任何一个条件为真,该函数将被跳过。

五、跳过文件或目录

有时可能需要跳过一整个文件或目录。例如,有文件里的代码你不想去执行。在这种情况下,必须从pytest搜集到的集合中排除文件和目录。

有关更多信息,请参阅自定义测试集合,后续看情况单独分享。

六、导入依赖失败跳过

当有些依赖的包导入失败的时候,可以通过pytest.importorskip这个函数来跳过。同样,可以用在模块级别,fixture函数或者测试函数里。

import pytest

# docutils = pytest.importorskip("docutils")

class TestDemo():

    def test_the_unknown2(self):
... def test_the_unknown3(self):
pytest.importorskip("docutils") def test_the_unknown1():
... if __name__ == "__main__":
pytest.main(["-s", '-r' "test_module1.py"])

运行结果,应该是跳过一个,执行2个:

test_module1.py                                                       [100%]

======================== 2 passed, 1 skipped in 0.09s =========================.s
Skipped: could not import 'docutils': No module named 'docutils'
.
Process finished with exit code 0

代码里的docutils是一个第三方库,你也可以根据库的版本号跳过:

docutils = pytest.importorskip("docutils", minversion="0.3")

版本将从指定模块的__version__属性中读取,如果不符合条件,也会跳过。

七、总结

  1. 无条件跳过模块中的所有测试
pytestmark = pytest.mark.skip("all tests still WIP")
  1. 基于某些条件跳过模块中的所有测试
pytestmark = pytest.mark.skipif(sys.platform == "win32", reason="tests for linux only")
  1. 如果缺少某些导入,则跳过模块中的所有测试
pexpect = pytest.importorskip("pexpect")

【pytest官方文档】解读Skipping test functions,跳过测试用例详解的更多相关文章

  1. webpack官方文档分析(三):Entry Points详解

    1.有很多种方法可以在webpack的配置中定义entry属性,为了解释为什么它对你有用,我们将展现有哪些方法可以配置entry属性. 2.单一条目语法 用法: entry: string|Array ...

  2. 【pytest官方文档】解读fixtures - 1.什么是fixtures

    在深入了解fixture之前,让我们先看看什么是测试. 一.测试的构成 其实说白了,测试就是在特定的环境.特定的场景下.执行特定的行为,然后确认结果与期望的是否一致. 就拿最常见的登录来说,完成一次正 ...

  3. 【pytest官方文档】解读fixtures - 2. fixtures的调用方式

    既然fixtures是给执行测试做准备工作的,那么pytest如何知道哪些测试函数 或者 fixtures要用到哪一个fixtures呢? 说白了,就是fixtures的调用. 一.测试函数声明传参请 ...

  4. 【pytest官方文档】解读fixtures - 3. fixtures调用别的fixtures、以及fixture的复用性

    pytest最大的优点之一就是它非常灵活. 它可以将复杂的测试需求简化为更简单和有组织的函数,然后这些函数可以根据自身的需求去依赖别的函数. fixtures可以调用别的fixtures正是灵活性的体 ...

  5. Cuda 9.2 CuDnn7.0 官方文档解读

    目录 Cuda 9.2 CuDnn7.0 官方文档解读 准备工作(下载) 显卡驱动重装 CUDA安装 系统要求 处理之前安装的cuda文件 下载的deb安装过程 下载的runfile的安装过程 安装完 ...

  6. 【pytest官方文档】解读- 插件开发之hooks 函数(钩子)

    上一节讲到如何安装和使用第三方插件,用法很简单.接下来解读下如何自己开发pytest插件. 但是,由于一个插件包含一个或多个钩子函数开发而来,所以在具体开发插件之前还需要先学习hooks函数. 一.什 ...

  7. 【pytest官方文档】解读fixtures - 8. yield和addfinalizer的区别(填坑)

    在上一章中,文末留下了一个坑待填补,疑问是这样的: 目前从官方文档中看到的是 We have to be careful though, because pytest will run that fi ...

  8. 【pytest官方文档】解读fixtures - 7. Teardown处理,yield和addfinalizer

    当我们运行测试函数时,我们希望确保测试函数在运行结束后,可以自己清理掉对环境的影响. 这样的话,它们就不会干扰任何其他的测试函数,更不会日积月累的留下越来越多的测试数据. 用过unittest的朋友相 ...

  9. 【pytest官方文档】解读fixtures - 10. fixture有效性、跨文件共享fixtures

    一.fixture有效性 fixture有效性,说白了就是fixture函数只有在它定义的使用范围内,才可以被请求到.比如,在类里面定义了一个fixture, 那么就只能是这个类中的测试函数才可以请求 ...

  10. 【pytest官方文档】解读- 开发可pip安装的第三方插件

    在上一篇的 hooks 函数分享中,开发了一个本地插件示例,其实已经算是在编写插件了.今天继续跟着官方文档学习更多知识点. 一个插件包含一个或多个钩子函数,pytest 正是通过调用各种钩子组成的插件 ...

随机推荐

  1. 关于Python 编码的一点认识

    在计算机中,所有数据的存储.运算以及传输都必须是二进制数字,因为计算机只认识0和1. 当一个人把一份数据传给另一个人时,计算机传递的是其实是二进制数字,但这些数字需要被还原为原始信息. 这个工作当然是 ...

  2. JDK源码阅读-FileDescriptor

    本文转载自JDK源码阅读-FileDescriptor 导语 操作系统使用文件描述符来指代一个打开的文件,对文件的读写操作,都需要文件描述符作为参数.Java虽然在设计上使用了抽象程度更高的流来作为文 ...

  3. Redis高频面试题总结

    通过面试多家大型互联网企业,总结了如下的高频面试题目: 1.redis 过期键的删除策略? (1)定时删除:在设置键的过期时间的同时,创建一个定时器 timer). 让定时器在键的过期时间来临时,立即 ...

  4. 阿里云linux安装nginx,亲测有效

    系统平台:CentOS release 6.6 (Final) 64位. 一.安装编译工具及库文件 yum -y install make zlib zlib-devel gcc-c++ libtoo ...

  5. linux之docker 安装 mysql

    首先进入docker : 命令:systemctl start docker 查詢一下docker的状态: 命令:docker images   现在开始安装mysql了,第一步拉取镜像 命令:doc ...

  6. PowerDesigner 设计数据库中常用脚本

    PowerDesigner 设计数据库中常用脚本 数据库设计 物理模型设置 Name转Comment脚本 '********************************************** ...

  7. vue 递归调用组件出错

    报错信息: Avoid mutating an injected value directly since the changes will be overwritten whenever the p ...

  8. monkey稳定性测试的步骤及策略

    1.adb的作用是什么?adb的全称:android debug bridge 安卓调试桥梁,包含在 Android SDK 平台工具软件包中.通过该命令与设备进行通信,以便进行调试adb可以同时管理 ...

  9. 微信小程序弹出层

    1.消息提示     wx.showToast wx.showToast({ title: '成功', icon: 'success', duration: 2000 })2.模态弹窗 wx.show ...

  10. jquery ajax error 函数的参数及使用

    使用jquery的ajax方法向服务器发送请求的时候,可选的回调函数有success.complete.beforeSend.error函数等.error函数常用来进行错误信息的处理,这里着重提一下e ...