Python 如何抑制 py.test 内部弃用警告
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40710094/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to suppress py.test internal deprecation warnings
提问by ev-br
Is there a way to suppress the pytest's internal deprecation warnings?
有没有办法抑制 pytest 的内部弃用警告?
Context: I'm looking to evaluate the difficulty of porting a test suite from nose
to pytest
. The suite is fairly large and heavily uses nose
-style yield
based test generators.
语境:我期待评估从移植测试套件的难度nose
来pytest
。该套件相当大并且大量使用基于nose
样式yield
的测试生成器。
I'd like to first make sure the existingtests pass with pytest, and then maybe change test generators to parameterized
.
我想首先确保现有的测试通过 pytest,然后可能将测试生成器更改为parameterized
.
Just running $ pytest path-to-test-folder
with pytest 3.0.4 is completely dominated by pages and pages of
刚$ pytest path-to-test-folder
用pytest 3.0.4运行完全被页面和页面所支配
WC1 ~repos/numpy/numpy/lib/tests/test_twodim_base.py yield tests are deprecated, and scheduled to be removed in pytest 4.0
Is there a way of turning these warnings off?
有没有办法关闭这些警告?
采纳答案by The Compiler
From pytest --help
:
来自pytest --help
:
--disable-pytest-warnings
disable warnings summary, overrides -r w flag
回答by Blaise
pytest -p no:warnings
, or add the following to your pytest.ini or tox.ini:
pytest -p no:warnings
,或将以下内容添加到您的 pytest.ini 或 tox.ini:
[pytest]
addopts = -p no:warnings
The result will be green without any indication of warnings. See documentation at https://docs.pytest.org/en/latest/warnings.html#disabling-warnings-summary.
结果将是绿色的,没有任何警告指示。请参阅https://docs.pytest.org/en/latest/warnings.html#disabling-warnings-summary 中的文档。
This can be a valid use case for a test suite where you want clean output.
对于需要干净输出的测试套件,这可能是一个有效的用例。
Be aware that always hiding all warnings may cause you to miss important warnings. If you want to hide only specific warnings, look at Cloc's answer.
请注意,始终隐藏所有警告可能会导致您错过重要警告。如果您只想隐藏特定警告,请查看 Cloc 的回答。
回答by CloC
I think you do not want to hide all warnings, but just the ones that are not relevant. And in this case, deprectation warnings from imported python modules.
我认为您不想隐藏所有警告,而只想隐藏不相关的警告。在这种情况下,来自导入的 python 模块的弃用警告。
Having a read on pytest documentation about Warnings Capture:
阅读有关警告捕获的pytest 文档:
Both -W command-line option and filterwarnings ini option are based on Python's own -W optionand warnings.simplefilter, so please refer to those sections in the Python documentation for other examples and advanced usage.
-W 命令行选项和 filterwarnings ini 选项都基于 Python 自己的-W 选项和warnings.simplefilter,因此请参阅 Python 文档中的这些部分以获取其他示例和高级用法。
So you can filter warnings with python's -W
option!
所以你可以用 python 的-W
选项过滤警告!
It seems that pytest
completely removes filters, because it shows all those DeprecationWarning
when running, and Python's documentation about Default Warning Filtersclearly says:
似乎pytest
完全删除了过滤器,因为它DeprecationWarning
在运行时显示了所有这些,并且 Python 的关于默认警告过滤器的文档清楚地说:
In regular release builds, the default warning filter has the following entries (in order of precedence):
default::DeprecationWarning:__main__ ignore::DeprecationWarning ignore::PendingDeprecationWarning ignore::ImportWarning ignore::ResourceWarning
在常规发布版本中,默认警告过滤器具有以下条目(按优先顺序):
default::DeprecationWarning:__main__ ignore::DeprecationWarning ignore::PendingDeprecationWarning ignore::ImportWarning ignore::ResourceWarning
So in your case, if you want let say to filter types of warning you want to ignore, such as those DeprecationWarning
, just run the pytest command with -W
option :
因此,在您的情况下,如果您想过滤要忽略的警告类型,例如那些DeprecationWarning
,只需运行带有-W
选项的 pytest 命令:
$ pytest path-to-test-folder -W ignore::DeprecationWarning
EDIT: From colini's comment, it is possible to filter by module. Example to ignore deprecation warnings from all sqlalchemy :
编辑:根据colini的评论,可以按模块过滤。忽略来自所有 sqlalchemy 的弃用警告的示例:
ignore::DeprecationWarning:sqlalchemy.*:
You can then list your installed modules that creates too much noise in the output of pytest
然后,您可以列出在输出中产生过多噪音的已安装模块 pytest
Use with file rather than in command line:
与文件一起使用而不是在命令行中使用:
You may prefer list those filters in pytest.ini file :
您可能更喜欢在 pytest.ini 文件中列出这些过滤器:
[pytest]
filterwarnings =
ignore::DeprecationWarning
回答by Polv
I don't want to hide all warning, so I put this in pytest.ini
我不想隐藏所有警告,所以我把它放在 pytest.ini
[pytest]
filterwarnings =
ignore::DeprecationWarning
回答by pk786
In the pytest.ini file you can add:
在 pytest.ini 文件中,您可以添加:
[pytest]
addopts = -p no:warnings
ORpassing below line in the command-line. This might be useful if your test suites handle warnings using an external system.
或在命令行中传递以下行。如果您的测试套件使用外部系统处理警告,这可能很有用。
-p no:warnings
-p 否:警告
ORif you only want to hide some specific deprecated warning, add below statement in you pytest.ini file
或者,如果您只想隐藏一些特定的已弃用警告,请在 pytest.ini 文件中添加以下语句
[pytest]
filterwarnings =
ignore:.*U.*mode is deprecated:DeprecationWarning
This will ignore all warnings of type DeprecationWarning where the start of the message matches the regular expression ".*U.*mode is deprecated".
这将忽略所有类型为 DeprecationWarning 的警告,其中消息的开头与正则表达式“.*U.*mode is deprecated”相匹配。
OR Although not recommended, you can use the
或 虽然不推荐,但您可以使用
--disable-warnings
--disable-warnings
command-line option to suppress the warning summary entirely from the test run output.
命令行选项以完全从测试运行输出中抑制警告摘要。