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

Warnings plugin's "once" filtering not working as expected #2586

Open
renefritze opened this issue Jul 18, 2017 · 3 comments
Open

Warnings plugin's "once" filtering not working as expected #2586

renefritze opened this issue Jul 18, 2017 · 3 comments
Labels
plugin: warnings related to the warnings builtin plugin

Comments

@renefritze
Copy link

renefritze commented Jul 18, 2017

This is probably related to #1043, but that was reported before the pytest_warnings plugin was merged into pytest proper.

What I'm seeing is a warning recorded for every realization of a parameterized fixture instead of only one.

Now given this mwe.py:

import warnings
import pytest
import pprint

@pytest.fixture(params=[1,2,3])
def fixture(request):
    return request.param

def test_me(fixture):
    if fixture < 2:
        pprint.pprint(warnings.filters)
    warnings.warn('some text', DeprecationWarning)

if __name__ == '__main__':
    for i in range(1,4):
        test_me(i)

I expected pytest -s -W once::DeprecationWarning mwe.py to print exactly one notice for the warning.
I get all three, however:

 ============================= test session starts =============================
platform linux -- Python 3.5.3, pytest-3.1.3, py-1.4.34, pluggy-0.4.0
rootdir: /tmp, inifile:
collected 3 items 
 
mwe.py [('once',
  re.compile('', re.IGNORECASE),
  <class 'DeprecationWarning'>,
  re.compile(''),
  0),
 ('ignore', None, <class 'DeprecationWarning'>, None, 0),
 ('ignore', None, <class 'PendingDeprecationWarning'>, None, 0),
 ('ignore', None, <class 'ImportWarning'>, None, 0),
 ('ignore', None, <class 'BytesWarning'>, None, 0),
 ('ignore', None, <class 'ResourceWarning'>, None, 0),
 ('ignore',
  re.compile('', re.IGNORECASE),
  <class 'pkg_resources.PEP440Warning'>,
  re.compile(''),
  0)]
...

============================== warnings summary ===============================
mwe.py::test_me[1]
  /tmp/mwe.py:12: DeprecationWarning: some text
    warnings.warn('some text', DeprecationWarning)

mwe.py::test_me[2]
  /tmp/mwe.py:12: DeprecationWarning: some text
    warnings.warn('some text', DeprecationWarning)

mwe.py::test_me[3]
  /tmp/mwe.py:12: DeprecationWarning: some text
    warnings.warn('some text', DeprecationWarning)

-- Docs: http://doc.pytest.org/en/latest/warnings.html
==================== 3 passed, 3 warnings in 0.01 seconds =====================

Running python -W once::DeprecationWarning mwe.py shows what I was expecting:

[('once',
  re.compile('', re.IGNORECASE),
  <class 'DeprecationWarning'>,
  re.compile(''),
  0),
 ('ignore', None, <class 'DeprecationWarning'>, None, 0),
 ('ignore', None, <class 'PendingDeprecationWarning'>, None, 0),
 ('ignore', None, <class 'ImportWarning'>, None, 0),
 ('ignore', None, <class 'BytesWarning'>, None, 0),
 ('ignore', None, <class 'ResourceWarning'>, None, 0)]
mwe.py:12: DeprecationWarning: some text
  warnings.warn('some text', DeprecationWarning)

edit: improved formatting

@nicoddemus
Copy link
Member

Thanks @renemilk for the report!

@Zac-HD
Copy link
Member

Zac-HD commented Oct 19, 2018

@asottile - I would expect #4104 to have fixed this, but I just reproduced it with pytest 3.9.1 😕

Any idea what's happening?

@asottile
Copy link
Member

#4104 only changed behavior of recwarn on python 2.x so this is probably somewhere else in the stack

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
plugin: warnings related to the warnings builtin plugin
Projects
None yet
Development

No branches or pull requests

4 participants