pytest.mark.skip可以标记无法在某些平台上运行的测试功能,或者你希望失败的测试功能

skip意味着只有在满足某些条件时才希望测试通过,否则pytest应该跳过运行测试。常见事例时非windows平台上跳过仅限windows的测试,或者跳过测试依赖于当前不可用的外部资源(例如数据库)

xfail意味着你希望测试由于某种原因而失败。一个常见的例子时对功能的测试尚未实施,或尚未修复的错误。当测试通过时尽管预计会失败(标有pytest.mark.xfail),它是一个xpass,将在测试摘要中报告。

pytest计数并分别列出skip和xfail测试,未显示有关跳过 xfailed测试的详细信息默认情况下,以避免混乱输出。可以使用 -r选项查看与“short”字母对应的详细信息显示在测试进度中

pytest -rxXs     显示额外的信息在xfailed,xpassed和skipped 测试中。有关-r的信息,运行pytest -h查看

skip

跳过测试函数的最简单方法是使用跳过装饰器标记它,可以传递一个可选的原因:

#!/usr/bin/env/python
# -*-coding:utf-8-*- import pytest @pytest.mark.skip(reason="input your skip reason")
def test_the_skip():
...

或者,也可以通过调用赖在测试执行或设置期间强制跳过pytest.skip(reason)功能:

def valid_config():
return True def test_function():
if not valid_config():
pytest.skip("reason")

也可以使用pytest.skip(reason,allow_module_level=True)跳过整个模块级别:

import pytest

if not pytest.config.getoption():
pytest.skip("reason",allow_module_level= True)

skipif

如果你希望有条件的跳过某些内容,则可以使用skipif代替,这是标记测试的示例在python3.6之前的揭示其上运行时要跳过的函数

import pytest
import sys
@pytest.mark.skipif(sys.version_info< (3,6), reason="requires python3.6 or higher")
def test_function():
  print("hello")
  ...

你也可以在模块之间共享skipif标记。参考以下的案例,第二个是:导入标记并在另一个测试模块中重复使用它:

# test_skip.py
minversion = pytest.mark.skipif(sys.version_info< (3,6), reason="requires python3.6 or higher") @minversion
def test_function():
print("hello")
...
# test.py
#!/usr/bin/env/python
# -*-coding:utf-8-*- from test_skip import minversion @minversion
def test_fun():
'''hehehe'''
print(test_fun.__doc__)

skip类或模块

可以在类上使用skipif标记(与任何其他标记一样):

#!/usr/bin/env/python
# -*-coding:utf-8-*-
import pytest,sys @pytest.mark.skipif(sys.platform == 'win32',reason="does not run on windows")
class TestPosixCalls(object):
def test_function(self):
"will not be setup or run under 'win32' platform"
print("hello")

如果条件为True,则此标记将为该类的每个测试方法生成跳过结果

警告:强烈建议不要在使用继承的类上使用skipif。 pytest中的一个已知错误标记可能会导致超类中的意外行为。

如果要跳过模块的所有测试功能,可以在全局级别使用pytestmark名称

pytestmark = pytest.mark.skipif(...)

如果将多个skipif装饰器应用于测试函数,则如果任何跳过条件为真,则将跳过它

skip缺少导入依赖项

您可以在模块级别或测试或测试设置功能中使用以下帮助程序

docutils = pytest.importorskip("docutils")

如果无法在此处导入docutils,则会导致测试跳过结果。 你也可以跳过库的版本号

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

将从指定模块的version属性中读取版本。

这是一个快速指南,介绍如何在不同情况下跳过模块中的测试

1.无条件地跳过模块中的所有测试:

pytestmark = pytest.mark.skip(“all tests still WIP”)

2.根据某些条件跳过模块中的所有测试

pytestmark = pytest.mark.skipif(sys.platform == “win32”, “tests for linux
˓→ only”

3.如果缺少某些导入,则跳过模块中的所有测试

pexpect = pytest.importorskip(“pexpect”)

pytest 10 skip跳过测试用例的更多相关文章

  1. pytest测试框架 -- skip跳过执行测试用例

      跳过执行测试用例 1.@pytest.mark.skip(reason=" ") -- 跳过执行测试函数 可传入一个非必须参数reason表示原因 import pytest@ ...

  2. Pytest(9)skip跳过用例

    前言 pytest.mark.skip可以标记无法在某些平台上运行的测试功能,或者您希望失败的测试功能 Skip和xfail: 处理那些不会成功的测试用例 你可以对那些在某些特定平台上不能运行的测试用 ...

  3. 【pytest官方文档】解读Skipping test functions,跳过测试用例详解

    有时候,为了满足某些场景的需要,我们知道有些测试函数在这时候肯定不能执行,或者执行了也会失败.那么我们 可以选择去跳过这个测试函数,这样也就不会影响整体的测试函数运行效果,不至于在你运行的众多绿色通过 ...

  4. 『德不孤』Pytest框架 — 4、Pytest跳过测试用例

    目录 1.无条件跳过skip 2.有条件跳过skipif 3.练习 自动化测试执行过程中,我们常常出现这种情况:因为功能阻塞,未实现或者环境有问题等等原因,一些用例执行不了, 如果我们注释掉或删除掉这 ...

  5. pytest八:skip 跳过用例

    这是一个快速指南,介绍如何在不同情况下跳过模块中的测试1.无条件地跳过模块中的所有测试:pytestmark = pytest.mark.skip("all tests still WIP& ...

  6. MOOC(7)- case依赖、读取json配置文件进行多个接口请求-跳过测试用例(6)

    初始化.跳过测试用例 # test_class_6.py import unittest from mock import mock from day_20200208_mooc.base.inter ...

  7. MongoDB limit 选取 skip跳过 sort排序 方法

    MongoDB  limit 选取 skip跳过 sort排序 在mysql里有order by  MongoDB用sort代替order by > db.user.find() { " ...

  8. MongoDB之Limit选取Skip跳过Sort排序

    1.Limit选取 我要从Document中取出多少个 只要2条Document db.Wjs.find().limit(2) 2.Skip跳过 我要跳过多少个Document 我要跳过前两个Docu ...

  9. MongoDB 之 Limit 选取 Skip 跳过 Sort 排序 MongoDB - 7

    我们已经学过MongoDB的 find() 查询功能了,在关系型数据库中的选取(limit),排序(sort) MongoDB中同样有,而且使用起来更是简单 首先我们看下添加几条Document进来 ...

随机推荐

  1. Cs231n-assignment 2作业笔记

    assignment 2 assignment2讲解参见: https://blog.csdn.net/BigDataDigest/article/details/79286510 http://ww ...

  2. 周末班:Python基础之模块

    什么是模块 什么是模块? 常见的场景:一个模块就是一个包含了python定义和声明的文件,文件名就是模块名字加上.py的后缀. 但其实import加载的模块分为四个通用类别: 1 使用python编写 ...

  3. Javascript DOM(2)

    一.value属性操作 1.具有value属性的三个标签:input.select.textarea 2.value的获取:ele.value input=document.getElementByI ...

  4. Redis学习笔记(2)——Redis的下载安装部署

    一.下载Redis Redis的官网下载页上有各种各样的版本,如图 但是官网下载的Redis项目不正式支持Windows.如果需要再windows系统上部署,要去GitHub上下载.我下载的是Redi ...

  5. bsp makefile2

    1. grep "bsp_dir" -r ./  -s  --exclude-dir "*.git" 用这个加快目录定位-- 2.编译所有子目录 for dir ...

  6. kernel笔记——VFS

    vfs(the virtual filesystem, virtual file switch)为应用程序访问文件提供了统一的接口,如read.write.open等. 下面我们看加载文件系统模块.格 ...

  7. 单元测试(qunit)

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http ...

  8. 基于Metronic的Bootstrap开发框架--工作流模块功能介绍(2)

    本篇继续<基于Metronic的Bootstrap开发框架--工作流模块功能介绍>,继续介绍基于Metronic的Bootstrap开发框架的工作模块功能,介绍工作流模块中相关业务表单的界 ...

  9. C#while死循环时候cpu占用比例大

    C#while死循环时候cpu占用比例大 原因:线程等不到释放,windows运行模式是抢占资源 解决方法:休眠一毫秒让垃圾回收可以进来回收资源 while (true) { Thread.Sleep ...

  10. WEB通知和React Native之即时通讯(iOS Android)

    WEB通知和React Native之即时通讯(iOS Android) 一,需求分析 1.1,允许服务器主动发送信息给客户端,客户端能监听到并且能接收. 1.2,为了方便同一个系统内的用户可以指定某 ...