我正在运行我的单元测试:

问题描述 投票:0回答:1
pytest -Werror

中止。

在我的情况下,警告是
DeprecationWarning
,所以我无法通过例外类别禁用此警告,因为它太宽了,也影响了其他代码。

我如何压制第三方图书馆警告,以便pytest忽略它们? 我尝试过:

with warnings.catch_warnings(): call_third_party_code_with_warnings()

...但是

pytest

仍然将测试终止为

ERROR
,而忽略了

warnings.catch_warnings

特别警告是

File "/Users/moo/Library/Caches/pypoetry/virtualenvs/trade-executor-8Oz1GdY1-py3.10/lib/python3.10/site-packages/jsonschema/validators.py", line 196, in __init_subclass__ warnings.warn( DeprecationWarning: Subclassing validator classes is not intended to be part of their public API. A future version will make doing so an error, as the behavior of subclasses isn't guaranteed to stay the same between releases of jsonschema. Instead, prefer composition of validators, wrapping them in an object owned entirely by the downstream library.

the the in中的警告输出似乎可以禁用Pytest中的警告输出,但似乎并不影响
pyproject.toml
flag异常。

-Werror

    
您可以在
filterwarnings = [ "ignore:::.*.jsonschema", # DeprecationWarning: Subclassing validator classes is not intended to be part of their public API. A future version will make doing so an error, as the behavior of subclasses isn't guaranteed to stay the same between releases of jsonschema. Instead, prefer composition of validators, wrapping them in an object owned entirely by the downstream library. "ignore:::.*.validators", "ignore::DeprecationWarning:openapi_spec_validator.*:" ]
中添加

filterwarnings

pytest.ini
或如果您喜欢的话,请忽略此库中的所有内容

[pytest] filterwarnings = ignore::DeprecationWarning:THIRD_PARTY_NAME.*:

eDit
使用

filterwarnings = ignore:::.*.THIRD_PARTY_NAME
标志,您可以使用
-Werror
python pytest
1个回答
5
投票

pytest.ini

我知道您不想完全沉默这个警告,它不是从第三方提出的,它是从
[pytest]
filterwarnings = 
    error
    ignore::DeprecationWarning
.

中提出的。 您可以尝试类似
builtins.py
的事情,但我怀疑它会起作用。

看起来这不是在
ignore::jsonschema.validators.DeprecationWarning

中实现的。在Pytest Github repo上有一张售票。

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.