zoukankan      html  css  js  c++  java
  • pytest 10 skip跳过测试用例

    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”)

  • 相关阅读:
    Python并行编程(十三):进程池和mpi4py模块
    Python 列表集合 字典推导式、生成器表达式
    Python 迭代对象、迭代器
    Python 参数,嵌套函数 的变量 使用
    Python 编码进阶
    Python 深浅Copy
    Python 代码块、缓存机制
    Python 列表,字典 相关方法
    初识 python 字符串 相关函数
    初识编码格式
  • 原文地址:https://www.cnblogs.com/peiminer/p/9878588.html
Copyright © 2011-2022 走看看