pytest-warns-with-multiple-statements (PT031)
源自 flake8-pytest-style linter。
作用
检查具有多个语句的 pytest.warns
上下文管理器。
此规则允许 pytest.warns
主体包含具有空主体的 for
循环(例如,pass
或 ...
语句),以测试迭代器行为。
为什么这不好?
当 pytest.warns
用作上下文管理器并包含多个语句时,可能会导致测试通过,但实际上应该失败。
pytest.warns
上下文管理器应该只包含一个触发预期警告的简单语句。
示例
import pytest
def test_foo_warns():
with pytest.warns(Warning):
setup() # False negative if setup triggers a warning but foo does not.
foo()
建议改为