跳到内容

pytest-warns-without-warning (PT029)

源自 flake8-pytest-style linter。

此规则不稳定且处于预览状态。使用需要 --preview 标志。

作用

检查不带预期警告的 pytest.warns 调用。

为什么这不好?

pytest.warns 期望接收一个预期警告作为其第一个参数。如果省略,pytest.warns 调用将在运行时失败。

示例

import pytest


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

建议改为

import pytest


def test_foo():
    with pytest.warns(SomeWarning):
        do_something()

参考