zoukankan      html  css  js  c++  java
  • pytest扫盲15--skip跳过用例

     skip跳过用例:

      跳过意味着你希望只在满足某些条件时测试才能通过,否则pytest应该跳过该测试。

      常见的例子是跳过非windows平台上的仅windows测试,或者跳过依赖于当前不可用的外部资源(例如数据库)的测试。

    # File  : test_demo_15.py
    # IDE   : PyCharm
    
    import pytest
    a = 4
    b = [4, 5, 6]
    c = 4
    d = 0
    
    # 1)最简单用法,用skip装饰器进行标记,@pytest.mark.skip(reason=''),传递reason说明原因
    @pytest.mark.skip(reason="no way of currently testing this")
    def test_1():
        assert a <= c, '断言出错'
    
    # 2)调用 pytest.skip(reason) 函数在执行阶段或配置初始参数阶段跳过
    def test_2():
        if True:
            pytest.skip("初始参数配置不满足")
    
    # # 3)跳过整个模块的测试用例
    # pytest.skip("我要跳过这个模块的用例", allow_module_level=True)
    # 可配合某个条件,满足条件则跳出模块执行(操作系统为 windows 时,跳过整个模块
    import sys
    if sys.platform.startswith("win"):
        pytest.skip("windows不适用此模块的用例", allow_module_level=True)
    
    # 4)用 skipif 装饰器进行标记,@pytest.mark.skipif(expression, reason=''),使用 pytest -rs 查看 reason
    # expression 支持所有的 Boolean 运算,为 True 则执行 skip
    # 下面标记在 python3.8 之后的版本运行时,跳过测试
    import sys
    @pytest.mark.skipif(sys.version_info > (3, 8), reason="requires python3.9 or higher")
    def test_3():
        assert a is c, '断言出错'
    
    # 重点:
    # 1:pytest 建议使用一个文件单独保存标记,然后整个套件中调用这些标记  config = pytest.mark.skipif(...)
    # 2:无条件跳过模块所有测试 pytestmark = pytest.mark.skip("全部跳过")
    # 3:带条件跳过模块所有测试 pytestmark = pytest.mark.skipif(sys.platform == "win32", reason="windows跳过")
    # 4:缺少某个模块 pexpect = pytest.importorskip("pexpect", minversion="0.3") # pexpect 模块名
    # ---- pytestmark 全局标记
    def test_4(): assert a in b, '断言出错' if __name__ == '__main__': pytest.main(['-s', '-q', 'test_demo_15.py'])

    更多关于 skip的用法,见官方文档:  https://docs.pytest.org/en/latest/skipping.html

  • 相关阅读:
    docker基础命令
    oracle 控制文件损坏处理
    mongodb 分片技术
    replcation set (复制集)配置过程 --mongodb
    redis API ---python
    MHA 高可用架构部署
    innoback 参数及使用说明
    Windows服务创建及发布
    DevOps 什么是 CI/CD?
    .NETReflectorVisualStudioExtension
  • 原文地址:https://www.cnblogs.com/xiaohuboke/p/13540849.html
Copyright © 2011-2022 走看看