Skip to content

Warnings via parametrized tests are not displayed only once #4402

@blueyed

Description

@blueyed

It appears that "grouping" of warnings using "default", "once", or "module" has
no effect with warnings from parametrized tests:

import pytest
import warnings


def f():
    warnings.warn("Foo")


@pytest.mark.parametrize('p', (1, 2))
def test_test(p):
    f()
% pytest -o 'filterwarnings=once' test_warn.py
==================================== test session starts ====================================
platform linux -- Python 3.6.7, pytest-4.0.0, py-1.7.0, pluggy-0.8.0
Django settings: project.settings (from environment variable)
rootdir: …/project/project, inifile: setup.cfg
plugins: requests-mock-1.5.2, xdist-1.24.1, mock-1.10.0, forked-0.2, django-3.4.4, cov-2.6.0, asyncio-0.9.0, celery-4.2.1
collected 2 items                                                                           

test_warn.py ..                                                                       [100%]

===================================== warnings summary ======================================
test_warn.py::test_test[1]
  …/project/test_warn.py:6: UserWarning: Foo
    warnings.warn("Foo")

test_warn.py::test_test[2]
  …/project/test_warn.py:6: UserWarning: Foo
    warnings.warn("Foo")

-- Docs: https://docs.pytest.org/en/latest/warnings.html
=========================== 2 passed, 2 warnings in 0.01 seconds ============================

I would expect the warning to appear only once.

From the warnings doc:

print only the first occurrence of matching warnings, regardless of location

Metadata

Metadata

Assignees

No one assigned

    Labels

    plugin: warningsrelated to the warnings builtin plugintopic: parametrizerelated to @pytest.mark.parametrize

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions