Skip to content
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

py.test ignores warnings.simplefilter #1043

Closed
woodscn opened this issue Sep 21, 2015 · 4 comments
Closed

py.test ignores warnings.simplefilter #1043

woodscn opened this issue Sep 21, 2015 · 4 comments

Comments

@woodscn
Copy link

woodscn commented Sep 21, 2015

I have a parametrized test that should emit a warning under certain conditions. After setting simplefilter('always'), py.test still only sees the first instance of the warning. Executing the same test in the interactive interpreter (as part of a for loop) correctly warns the user each time.

In the following simple example, for whatever reason, py.test doesn't capture any of the warnings at all.

from warnings import warn, simplefilter
import pytest

simplefilter('always', MyWarning)

class MyWarning(UserWarning):
    pass

def warn_the_user(ind):
    warn("Iteration # " + str(ind), MyWarning)
    return ind

@pytest.mark.parametrize('ind', range(10))
def test_warn_the_user(ind):
    warn_the_user(ind)

$ py.test pytests_warning.py
============================= test session starts ==============================
platform darwin -- Python 3.4.3 -- py-1.4.30 -- pytest-2.7.2
rootdir: ~, inifile:
collected 10 items

pytests_warning.py ..........

========================== 10 passed in 0.01 seconds ===========================

@RonnyPfannschmidt
Copy link
Member

you are not at all using the pytest integration

did ypu perhaps mean to do a:

from warnings import warn, simplefilter
import pytest

class MyWarning(UserWarning):
    pass

def warn_the_user(ind):
    warn("Iteration # " + str(ind), MyWarning)
    return ind

@pytest.mark.parametrize('ind', range(10))
def test_warn_the_user(ind):
   with pytest.warns(MyWarning):
        warn_the_user(ind)

note that we discovered a bug in 2.8.0 wrt warnings, so you might need to try the 2.7 series until 2.8.1 is released
however since you use a custom warnings class it might be alright,
however you might need to still add in the simplefilter line

see #1041 for details

@woodscn
Copy link
Author

woodscn commented Sep 21, 2015

I'm more concerned that pytest seems to be overriding my simplefilter. Does pytest ignore the usual Python warnings.warn function by default?
I'm using 2.7.2, and pytest.warns does not seem to be present.

@nicoddemus
Copy link
Member

xref: #2452

@Zac-HD
Copy link
Member

Zac-HD commented Oct 19, 2018

Since pytest 3.8, we use the standard Python warnings mechanism. #3954 and #2586 document this problem with the current APIs, so I'm closing this issue as a duplicate (even though it was first!).

@Zac-HD Zac-HD closed this as completed Oct 19, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants