如何使 pytest 因警告而崩溃?

Bri*_*tow 14 python warnings pytest

这是如何找到“因非串联而排序”警告的来源的扩展?。

在我的 pytest 中,我仍然收到相同的警告。我在这里查看了几个问题并完成了:

import warnings
warnings.filterwarnings('error')
Run Code Online (Sandbox Code Playgroud)

这是在如何捕获 numpy 警告中建议的,就像它是一个异常(不仅仅是为了测试)?

然而,当我运行 pytest 时,它仍然给我错误,但实际上没有任何错误......

Chr*_*ong 16

-W尝试在运行 pytest 时传递标志,如下所示:

pytest -W error::RuntimeWarning
Run Code Online (Sandbox Code Playgroud)

指定要转为错误的警告类型,例如 DeprecationWarning、FutureWarning、UserWarning。

  • 如何在配置文件中设置它,这样我就不需要在每个 pytest 调用中手动传递它?我没有看到“-W”选项的简写名称? (5认同)
  • 可以使用“filterwarnings” ini 选项在 pytest.ini 或 pyproject.toml 文件中设置相同的选项。[链接到文档](https://docs.pytest.org/en/latest/how-to/capture-warnings.html#controlling-warnings),其中包含 pytest.ini 和 pyproject.toml 的语法示例。 (4认同)