跳到内容

pytest-warns-too-broad (PT030)

源自 flake8-pytest-style linter。

作用

检查缺少 match 参数的 pytest.warns 调用。

为什么这不好?

pytest.warns(Warning) 将捕获任何 Warning,并且可能会捕获与被测代码无关的警告。为避免这种情况,应使用 match 参数调用 pytest.warns。可以通过 lint.flake8-pytest-style.warns-require-match-forlint.flake8-pytest-style.warns-extend-require-match-for 设置来配置需要 match 参数的警告名称。

示例

import pytest


def test_foo():
    with pytest.warns(Warning):
        ...

    # empty string is also an error
    with pytest.warns(Warning, match=""):
        ...

建议改为

import pytest


def test_foo():
    with pytest.warns(Warning, match="expected message"):
        ...

Options (选项)

参考