You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Document that with pytest.warns() captures all warnings, not just those it tests were emitted #9288
importpytestimportwarningsdeftest_warning():
withpytest.warns(DeprecationWarning):
withpytest.warns(UserWarning): # catches UserWarning, but also every other type toowarnings.warn("my warning", UserWarning)
warnings.warn("some deprecation warning", DeprecationWarning)
================================== FAILURES ===================================
________________________________ test_warning _________________________________
def test_warning():
> with pytest.warns(DeprecationWarning):
E Failed: DID NOT WARN. No warnings of type (<class 'DeprecationWarning'>,) was emitted. The list of emitted warnings is: [].
changed the title [-]`pytest.warns` hides non-expected warnings, even `DeprecationWarning`[/-][+]`pytest.warns` hides not expected warnings, even `DeprecationWarning`[/+]on Nov 9, 2021
This is the expected behavior, pytest.warns acts as a net which captures all warnings inside the block, ensuring the given warning is raised, or failing otherwise.
The pytest.warns documentation does not say it will filter everything (actually it does not say it will filter anything at all), so it should be fixed too. It preferably should say how to make pytest.warns to not capture everything or an alternative solution, but I still myself have no idea how to achieve that with tools Pytest provides, could you please suggest anything?
The pytest.warns documentation does not say it will filter everything (actually it does not say it will filter anything at all), so it should be fixed too.
True, thanks!
I still myself have no idea how to achieve that with tools Pytest provides, could you please suggest anything?
Hmm I can't think of anything right now, pytest.warns uses warnings.catch_warnings under the covers, which by design captures all warnings.
deftest_raises():
withpytest.raises(RuntimeError): # passes,withpytest.raises(NotImplementedError): # because this will not catch and hide the exceptionraiseRuntimeError("some exception")
changed the title [-]`pytest.warns` hides not expected warnings, even `DeprecationWarning`[/-][+]Document that `with pytest.warns()` captures all warnings, not just those it tests were emitted[/+]on Apr 21, 2022
Alternatively we could make pytest.warns() re-emit all captured warnings on exit, which might better match users' intuitions - including mine, from before I went and looked up warnings.catch_warnings(record=True).
I think it would make sense to re-raise all unmatched warnings. For example, say you are testing your own deprecated code so you add pytest.warns(DeprecationWarning, match='something specific to your project'). Then one of your dependencies (or maybe some other part of your code) adds a deprecation that is only triggered by that original code's tests. Then you will be blind-sided if the second deprecation is removed first because you never saw it.
Reacted by Zac Hatfield-Dodds, Bruno Oliveira, Nikita Kniazev, Tom Eulenfeld and Cecil Curry
Activity
[-]`pytest.warns` hides non-expected warnings, even `DeprecationWarning`[/-][+]`pytest.warns` hides not expected warnings, even `DeprecationWarning`[/+]nicoddemus commentedon Nov 10, 2021
Hi @Kojoley,
This is the expected behavior,
pytest.warnsacts as a net which captures all warnings inside the block, ensuring the given warning is raised, or failing otherwise.We might add a note to https://docs.pytest.org/en/6.2.x/warnings.html#deprecationwarning-and-pendingdeprecationwarning to mention that behavior does not apply for code inside
pytest.warns, what do you think?Kojoley commentedon Nov 10, 2021
The
pytest.warnsdocumentation does not say it will filter everything (actually it does not say it will filter anything at all), so it should be fixed too. It preferably should say how to makepytest.warnsto not capture everything or an alternative solution, but I still myself have no idea how to achieve that with tools Pytest provides, could you please suggest anything?nicoddemus commentedon Nov 10, 2021
True, thanks!
Hmm I can't think of anything right now,
pytest.warnsuseswarnings.catch_warningsunder the covers, which by design captures all warnings.nicoddemus commentedon Nov 10, 2021
Created #9291 to track the docs change.
Kojoley commentedon Nov 10, 2021
If I understand correctly
pytest.deprecated_callhides all warnings by design too?Also, https://docs.pytest.org/en/6.2.x/warnings.html#warns says
You can check that code raises a particular warning using func:pytest.warns, which works in a similar manner to raises:butraisesdoes not hide all exceptions, example:nicoddemus commentedon Nov 10, 2021
Yes. 👍
Good point, we need to mention that difference in the docs too.
[-]`pytest.warns` hides not expected warnings, even `DeprecationWarning`[/-][+]Document that `with pytest.warns()` captures all warnings, not just those it tests were emitted[/+]Zac-HD commentedon Apr 21, 2022
Alternatively we could make
pytest.warns()re-emit all captured warnings on exit, which might better match users' intuitions - including mine, from before I went and looked upwarnings.catch_warnings(record=True).QuLogic commentedon Jan 13, 2023
I think it would make sense to re-raise all unmatched warnings. For example, say you are testing your own deprecated code so you add
pytest.warns(DeprecationWarning, match='something specific to your project'). Then one of your dependencies (or maybe some other part of your code) adds a deprecation that is only triggered by that original code's tests. Then you will be blind-sided if the second deprecation is removed first because you never saw it.zanieb commentedon Apr 4, 2023
Note that this also means that if you pass an incorrect value to
warnings.warnit will hide the error. See #10865Zac-HD commentedon Jul 3, 2023
As of #10937, expected in Pytest 8.0, we re-emit all non-matching warnings.
Fix `test_root_validator_allow_reuse_same_field()` for all warnings
TST: Avoid catching not emitted warnings (#2429)