Skip to content

pytest.warns(None) does not fail if warnings are raised. #8644

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Jasha10 opened this issue May 7, 2021 · 2 comments
Closed

pytest.warns(None) does not fail if warnings are raised. #8644

Jasha10 opened this issue May 7, 2021 · 2 comments
Labels
type: docs documentation improvement, missing or needing clarification type: question general question, might be closed after 2 weeks of inactivity

Comments

@Jasha10
Copy link

Jasha10 commented May 7, 2021

I expect that pytest.warns(None) is an assertion that no warnings are raised.

import warnings
import pytest

def test_no_warning():
    with pytest.warns(None):
        warnings.warn("Look out!")

Running pytest on the above does not fail. I would expect a failure, something like "a warning was raised where no warning was expected".

$ pip list
Package    Version
---------- -------------------
attrs      21.2.0
certifi    2020.12.5
iniconfig  1.1.1
packaging  20.9
pip        21.0.1
pluggy     0.13.1
py         1.10.0
pyparsing  2.4.7
pytest     6.2.4
setuptools 52.0.0.post20210125
toml       0.10.2
wheel      0.36.2
$ python --version
Python 3.9.4

OS: Ubuntu 18.04

@Zac-HD Zac-HD added type: docs documentation improvement, missing or needing clarification type: question general question, might be closed after 2 weeks of inactivity labels May 8, 2021
@Zac-HD
Copy link
Member

Zac-HD commented May 8, 2021

See the docs:

To record with pytest.warns without asserting anything about the warnings, pass None as the expected warning type

This StackOverflow question shows how to assert that no warning was raised.

@Zac-HD Zac-HD closed this as completed May 8, 2021
@Jasha10
Copy link
Author

Jasha10 commented May 8, 2021

Awesome, thanks! Your answer to that SO question looks good :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: docs documentation improvement, missing or needing clarification type: question general question, might be closed after 2 weeks of inactivity
Projects
None yet
Development

No branches or pull requests

2 participants