pytest简易教程(20):跳过用例 - skip、skipif

本文详细介绍了pytest中如何使用装饰器和条件控制来管理测试用例的执行,包括skip、skipif、importorskip等技巧,帮助开发者在受限环境或功能未完成时有效地管理测试流程。
摘要由CSDN通过智能技术生成

pytest简易教程汇总,详见https://www.cnblogs.com/uncleyong/p/17982846

应用场景

1、受限环境,某些用例无法运行

2、功能未开发完成,但是用例写了,可以不运行这些用例

实现方案

1、加装饰器,被修饰函数/方法中代码不会被执行,也就是说不会进入方法;也可以加在类上,类中所有方法均跳过
@pytest.mark.skip
@pytest.mark.skip(reason="")
@pytest.mark.skipif(condition, reason="")
2、代码中添加跳过(也就是用例执行过程中跳过),会进入被修饰函数/方法,但是函数/方法中pytest.skip后面代码不会被执行
pytest.skip(reason="")
pytest.skip(reason="",allow_module_level=False)

skip

无条件跳过,也就是始终跳过该测试用例

源码:

    class _SkipMarkDecorator(MarkDecorator):
        @overload  # type: ignore[override,misc,no-overload-impl]
        def __call__(self, arg: Markable) -> Markable:
            ...

        @overload
        def __call__(self, reason: str = ...) -> "MarkDecorator":
            ...

方法:skip(reason=None)
参数:可选参数reason,用于标注跳过的原因,会在测试结果中显示
使用方法:@pytest.mark.skip(reason="xxx")

函数/方法级跳过

示例:无参数reason

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Author : 韧
# @wx :ren168632201
# @Blog :https://www.cnblogs.com/uncleyong/

import pytest

@pytest.mark.skip
def test_case():
    print("代码开发中")

结果:

示例:有参数reason

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Author : 韧
# @wx :ren168632201
# @Blog :https://www.cnblogs.com/uncleyong/

import pytest

@pytest.mark.skip(reason="代码开发中")
def test_case():
    print("---skip")

结果:

函数/方法执行过程中跳过

示例:代码中添加跳过

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Author : 韧
# @wx :ren168632201
# @Blog :https://www.cnblogs.com/uncleyong/

import pytest
import platform

def test_case():
    if platform.system() == "Windows":
        pytest.skip("win下跳过")
        print("---skip")
    else:
        print("不跳过")

结果:pytest.skip后面代码没执行

类级跳过

示例:修饰器加在类上

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Author : 韧
# @wx :ren168632201
# @Blog :https://www.cnblogs.com/uncleyong/

import pytest

@pytest.mark.skip(reason="代码开发中")
class TestCase:
    def test_case1(self):
        print("test_case1---skip")
    def test_case2(self):
        print("test_case2---skip")

结果:

模块级跳过

pytest.skip(msg="原因描述", allow_module_level=False)

allow_module_level为True时,跳过当前模块

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Author : 韧
# @wx :ren168632201
# @Blog :https://www.cnblogs.com/uncleyong/
import sys
import pytest

if sys.platform=="win32":
    pytest.skip('win中该模块跳过', allow_module_level=True)
class TestCase:
    def test_case1(self):
        print("test_case1---skip")
    def test_case2(self):
        print("test_case2---skip")

def test_case3():
    print("test_case3---skip")
def test_case4():
    print("test_case4---skip")

结果:

或者:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Author : 韧
# @wx :ren168632201
# @Blog :https://www.cnblogs.com/uncleyong/

import pytest

pytestmark = pytest.mark.skip(reason="win中该模块跳过")

class TestCase:
    def test_case1(self):
        print("test_case1---skip")
    def test_case2(self):
        print("test_case2---skip")

def test_case3():
    print("test_case3---skip")
def test_case4():
    print("test_case4---skip")

结果:

skipif

condition条件为True跳过该测试用例

源码:

    class _SkipifMarkDecorator(MarkDecorator):
        def __call__(  # type: ignore[override]
            self,
            condition: Union[str, bool] = ...,
            *conditions: Union[str, bool],
            reason: str = ...,
        ) -> MarkDecorator:
            ...


方法:skipif(condition, reason=None)
参数:
  condition:跳过的条件,可选
  reason:标注跳过的原因,可选
使用方法:@pytest.mark.skipif(condition, reason="xxx")

函数/方法级跳过

示例:condition和reason都不填

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Author : 韧
# @wx :ren168632201
# @Blog :https://www.cnblogs.com/uncleyong/

import pytest

@pytest.mark.skipif
def test_case1():
    print("---skipif")
    assert 1==1

结果:

示例:condition和reason都填

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Author : 韧
# @wx :ren168632201
# @Blog :https://www.cnblogs.com/uncleyong/

import pytest
import sys


@pytest.mark.skipif(sys.platform.startswith("win"), reason="win环境中跳过")
def test_case1():
    pass


@pytest.mark.skipif(sys.version_info < (3, 9), reason="python3.9以下跳过")
def test_case2():
    pass

结果:

类级跳过

示例:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Author : 韧
# @wx :ren168632201
# @Blog :https://www.cnblogs.com/uncleyong/
import sys
import pytest

@pytest.mark.skipif(sys.platform.startswith("win"), reason="win环境中跳过")
class TestCase:
    def test_case1(self):
        print("test_case1---skip")
    def test_case2(self):
        print("test_case2---skip")

def test_case3():
    print("test_case3---skip")
def test_case4():
    print("test_case4---skip")

结果:

模块级跳过

示例:下面只能是pytestmark,不能改为其它的

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Author : 韧
# @wx :ren168632201
# @Blog :https://www.cnblogs.com/uncleyong/
import sys
import pytest

pytestmark = pytest.mark.skipif(sys.platform=="win32", reason="win中该模块跳过")
def test_case1():
    print("test_case1---skip")
def test_case2():
    print("test_case2---skip")

结果:

补充:importorskip

缺少模块或者版本低于参数值就跳过

源码:

def importorskip(
    modname: str, minversion: Optional[str] = None, reason: Optional[str] = None
) -> Any:
    """Import and return the requested module ``modname``, or skip the
    current test if the module cannot be imported.

    :param modname:
        The name of the module to import.
    :param minversion:
        If given, the imported module's ``__version__`` attribute must be at
        least this minimal version, otherwise the test is still skipped.
    :param reason:
        If given, this reason is shown as the message when the module cannot
        be imported.

    :returns:
        The imported module. This should be assigned to its canonical name.

    Example::

        docutils = pytest.importorskip("docutils")
    """
    import warnings

    __tracebackhide__ = True
    compile(modname, "", "eval")  # to catch syntaxerrors

    with warnings.catch_warnings():
        # Make sure to ignore ImportWarnings that might happen because
        # of existing directories with the same name we're trying to
        # import but without a __init__.py file.
        warnings.simplefilter("ignore")
        try:
            __import__(modname)
        except ImportError as exc:
            if reason is None:
                reason = f"could not import {modname!r}: {exc}"
            raise Skipped(reason, allow_module_level=True) from None
    mod = sys.modules[modname]
    if minversion is None:
        return mod
    verattr = getattr(mod, "__version__", None)
    if minversion is not None:
        # Imported lazily to improve start-up time.
        from packaging.version import Version

        if verattr is None or Version(verattr) < Version(minversion):
            raise Skipped(
                f"module {modname!r} has __version__ {verattr!r}, required is: {minversion!r}",
                allow_module_level=True,
            )
    return mod

参数:

  modname:模块名

  minversion:要求的最低版本

  reason:跳过原因

示例一:modname不满足

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Author : 韧
# @wx :ren168632201
# @Blog :https://www.cnblogs.com/uncleyong/

import pytest
@pytest.importorskip("requestsx", minversion="2.31.0")
def test_case():
    print("---importorskip")

结果:

示例二:minversion不满足

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Author : 韧
# @wx :ren168632201
# @Blog :https://www.cnblogs.com/uncleyong/

import pytest
@pytest.importorskip("requests", minversion="2.31.0")
def test_case():
    print("---importorskip")

结果:

示例三:modname和minversion都满足

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Author : 韧
# @wx :ren168632201
# @Blog :https://www.cnblogs.com/uncleyong/

import pytest
@pytest.importorskip("requests", minversion="2.26.0")
def test_case():
    print("---importorskip")

结果:

  • 3
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值