如何使pytest在发生警告时崩溃?

时间:2019-02-22 16:29:40

标签: python pandas warnings pytest

这是How do I find where a "Sorting because non-concatenation" warning is coming from?的扩展。

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

import warnings
warnings.filterwarnings('error')

这在How do I catch a numpy warning like it's an exception (not just for testing)?中建议

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

2 个答案:

答案 0 :(得分:0)

正如Warnings Capture的pytest文档所说,您可以通过以下几种方法来做到这一点:

1)使用-W error::Warning运行pytest,它将在每次发生警告时使测试崩溃

2)使用此部分修改您的pytest.ini

[pytest]
filterwarnings =
    error

3)设置适用于模块中所有测试的变量pytestmark

pytestmark = pytest.mark.filterwarnings("error")

答案 1 :(得分:0)

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

pytest -W error::RuntimeWarning

指定要上报错误的警告类型,例如弃用警告,未来警告,用户警告。

相关问题