Ignoring DeprectionWarning in pytest.warns #8666
-
The docs read:
It doesn't seem to be the case. My tests use with pytest.warns(None) as record:
function_that_calls_a_lib_that_generates_a_deprecation_warning()
assert not record My understanding of this is limited. Could it be due to changes in Python 3.7 regarding Am I missing something? The workaround I found for now is to filter the records afterwards, which is verbose and not elegant (managing with pytest.warns(None) as record:
function_that_calls_a_lib_that_generates_a_deprecation_warning()
assert not [r for r in record if r.category is not DeprecationWarning] Reproducible test case. Using Python 3.7.3 import warnings
import pytest
def test_deprecation_warning():
with pytest.warns(None) as record:
warnings.warn("Test", DeprecationWarning)
# This fails
# assert not record
print(record)
# WarningsChecker(record=True)
print([r.category for r in record])
# [<class 'DeprecationWarning'>]
# This succeeds
assert not [r for r in record if r.category is not DeprecationWarning] At some point, I thought it had something to do with the use of import warnings
import pytest
def test_deprecation_warning():
with pytest.warns(RuntimeWarning) as record:
warnings.warn("Test", DeprecationWarning)
warnings.warn("my warning", RuntimeWarning)
print(record)
# WarningsChecker(record=True)
print([r.category for r in record])
# [<class 'DeprecationWarning'>, <class 'RuntimeWarning'>] |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
Hmmm strange. Do you have any warning filters active? Can you also provide a reproducible example? |
Beta Was this translation helpful? Give feedback.
Hmmm strange. Do you have any warning filters active? Can you also provide a reproducible example?