pytest简易教程(20):跳过用例 - skip、skipif
pytest简易教程汇总,详见
1、受限环境,某些用例无法运行 无条件跳过,也就是始终跳过该测试用例 源码: 方法:skip(reason=None) 示例:无参数reason 结果: 示例:有参数reason 结果: 示例:代码中添加跳过 结果:pytest.skip后面代码没执行 示例:修饰器加在类上 结果: pytest.skip(msg="原因描述", allow_module_level=False) allow_module_level为True时,跳过当前模块 结果: 或者: 结果: condition条件为True跳过该测试用例 源码: 示例:condition和reason都不填 结果: 示例:condition和reason都填 结果: 示例: 结果: 示例:下面只能是pytestmark,不能改为其它的 结果: 缺少模块或者版本低于参数值就跳过 源码: 参数: modname:模块名 minversion:要求的最低版本 reason:跳过原因 示例一:modname不满足 结果: 示例二:minversion不满足 结果: 示例三:modname和minversion都满足 结果: 应用场景
实现方案
@pytest.mark.skip
@pytest.mark.skip(reason="")
@pytest.mark.skipif(condition, reason="")
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":
...
参数:可选参数reason,用于标注跳过的原因,会在测试结果中显示
使用方法:@pytest.mark.skip(reason="xxx")函数/方法级跳过


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

类级跳过

模块级跳过


skipif
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")函数/方法级跳过


类级跳过

模块级跳过


补充: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



__EOF__
关于博主:擅长性能、全链路、自动化、企业级自动化持续集成(DevTestOps)、测开等
加薪必备:项目实战(性能、自动化)、简历笔试,https://www.cnblogs.com/uncleyong/p/15777706.html
测试提升:从测试小白到高级测试修炼之路,https://www.cnblogs.com/uncleyong/p/10530261.html
欢迎分享:如果您觉得文章对您有帮助,欢迎转载、分享,也可以点击文章右下角【推荐】一下!