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

全栈测试笔记 / 2024-02-24 / 原文

 

pytest简易教程汇总,详见

应用场景

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

  

结果:

 

示例:有参数reason

  

结果:

 

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

示例:代码中添加跳过

  

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

 

类级跳过

示例:修饰器加在类上

  

结果:

 

模块级跳过

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

allow_module_level为True时,跳过当前模块

  

结果:

 

或者:

 

结果:

 

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都不填

  

结果:

 

示例:condition和reason都填

  

结果:

 

类级跳过

示例:

  

结果:

 

模块级跳过

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

  

结果:

 

补充: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不满足

  

结果:

 

示例二:minversion不满足

  

结果:

 

示例三:modname和minversion都满足

  

结果: