• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

pytest学习2.9 - 处理失败用例,2.10-管理日志,2.12-处理警告

武飞扬头像
阿_焦
帮助1

2.9 如何重新运行失败的测试,并在测试运行之间维护状态

2.9.1 如何运行失败的用例

该插件提供了两个命令行选项来重新运行上次最小的调用中的失败:
–lf, --last-failed:只重新运行失败
–ff, --failed-first:先运行失败的用例,然后再运行其余的测试
–nf, --new-first:首先运行新的测试,然后运行其余的测试,在这两种情况下,测试也按文件修改的时间排序,首先是最近的文件。

2.9.2 如何跳过失败的用例

# content of test_50.py
import pytest
@pytest.mark.parametrize("i", range(50))
def test_num(i):
    if i in (17, 25):
        pytest.fail("bad luck")
        # pytest.skip("跳过用例")
pytest -q test_moudle.py
pytest -q test_moudle.py --ff
pytest -q test_moudle.py --lf
pytest -q test_moudle.py --nf

C:\Users\Desktop\python>pytest -q test_moudle.py
.................s.......s........................                                                                                                                               [100%]
48 passed, 2 skipped in 0.07s

2.9.3 在上次运行中没有任何失败时的情况

当上次运行中没有测试失败,或者没有找到缓存的最后失败数据时,可以使用选项配置为运行所有测试或没有测试,该选项接受以下值之一:
pytest -q test_moudle.py --last-failed --last-failed-no-failures none
pytest -q test_moudle.py --last-failed --last-failed-no-failures all

2.9.4 设置新的缓存 config.cache 对象

# content of test_caching.py
import pytest
def expensive_computation():
    print("running expensive computation...")
@pytest.fixture
def mydata(request):
    val = request.config.cache.get("example/value", None)
    if val is None:
        expensive_computation()
        val = 42
        request.config.cache.set("example/value", val)
    return val
def test_function(mydata):
    assert mydata == 23

2.9.5 检查缓存内容

pytest --cache-show example/*

2.9.6 清除缓存内容

对于来自连续集成服务器的调用,建议使用这样做,因为隔离和正确性比速度更重要:
pytest -q test_moudle.py --cache-clear

2.9.7 逐步【不深入】

2.10 如何管理日志?

Pytest自动捕获级别警告或以上的日志消息,并以与捕获的stdout和stderr相同的方式在它们自己的部分中显示它们

pytest --log-format="%(asctime)s %(levelname)s %(message)s" --log-date-format="%Y-%m-%d %H:%M:%S"

pytest.ini文件配置:

[pytest]
log_format = %(asctime)s %(levelname)s %(message)s
log_date_format = %Y-%m-%d %H:%M:%S

不捕获失败日志:
pytest --show-capture=no

2.10.1 单倍体夹具

import logging
def test_foo(caplog):
    caplog.set_level(logging.INFO)
    logging.debug("cuol")
    pass

2.10.2 Live日志

通过将log_cli配置选项设置为true,pytest将在日志记录直接发送到控制台时输出它们。
所有的CLI日志选项也可以在配置INI文件中设置。选项名称为:
log_cli_level
log_cli_format
log_cli_date_format
如果您需要记录整个测试套件,请日志记录调用到一个文件:
–log-file=/path/to/log/file
–log-file-level
–log-file-format
–log-file-date-format
–log-format
–log-date-format

2.10.3 自定义颜色

import pytest
@pytest.hookimpl
def pytest_configure(config):
    logging_plugin = config.pluginmanager.get_plugin("logging-plugin")
    # Change color on existing log level
    logging_plugin.log_cli_handler.formatter.add_color_level(logging.INFO, "cyan")
    # Add color to a custom log level (a custom log level `SPAM` is already set up)
    logging_plugin.log_cli_handler.formatter.add_color_level(logging.SPAM, "blue")

2.10.4 Release notes

这个特性是作为最小的捕获日志插件而引入的,它们相互冲突。当引入这个功能时,与小捕获日志的向后兼容API已经被删除,因此如果你仍然需要,
你可以通过添加到你的pytest.ini来禁用内部功能:

[pytest]
addopts=-p no:logging

2.10.5 在测试3.4中不兼容的变化【不用看】

2.11 如何捕获 st 输出/stderr输出【不用看】

2.12 如何捕获 警告

Pytest现在会在测试执行期间自动捕获警告,并在会话结束时显示它们

# content of test_show_warnings.py
import warnings
def api_v1():
    warnings.warn(UserWarning("api v1, should use functions from v2"))
    return 1
def test_one():
    assert api_v1() == 1

pytest test_show_warnings.py

2.12.1 控制警告

Pytest提供了它自己的-W标志来控制哪些警告被忽略、显示或转换为错误。此代码示例显示了如何将任何用户警告类别类别的警告视为错误:
pytest -q test_show_warnings.py -W error::UserWarning
例如,下面的配置将忽略匹配一个正则表达式的所有用户警告和特定的弃用警告,但它将把所有其他警告转换为错误。忽略了警告:userwarning he function,
将其他警告视为error

# pytest.ini
[pytest]
filterwarnings =
    error
    ignore::UserWarning
    ignore:function ham\(\) is deprecated:DeprecationWarning

2.12.2 @pytest.mark.filterwarnings

您可以使用@pytest.mark.filterwarnings向特定的测试项添加警告过滤器,允许您更好地控制应该在测试、类甚至模块级别上捕获哪些警告:

import warnings, pytest
def api_v1():
    warnings.warn(UserWarning("api v1, should use functions from v2"))
    return 1
@pytest.mark.filterwarnings("ignore:api v1")
def test_one():
    assert api_v1() == 1

应用于模块中的所有测试:
pytestmark = pytest.mark.filterwarnings(“error”)

2.12.3 禁用警告摘要

如果您的测试套件使用外部系统处理警告,如何禁用警告:

[pytest]
addopts = -p no:warnings

2.12.5 对弃用警告、废弃警告的忽略

默认情况下,pytest将显示弃用警告和等待弃用警告来自用户代码和第三方库的警告, ,隐藏一些发生在您无法控制的代码中的特定弃用警告是很有用的,
在这种情况下,您可以使用警告过滤器选项(ini或标记)来忽略这些警告。这将忽略所有类型的警告消息的开始与正则表达式匹配的警告

[pytest]
filterwarnings =
    ignore:.*U.*mode is deprecated:DeprecationWarning

2.12.6 确保代码触发弃用警告【不看】

2.12.7 使用警告功能断言警告【不看】

2.12.8 记录警告【不看】

2.12.9 测试中的警告的附加用例【不看】

2.12.10 自定义故障消息【不看】

2.12.11 内部最严重警告【不看】

2.12.12 资源警告【不看】

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhfjajce
系列文章
更多 icon
同类精品
更多 icon
继续加载