@pytest.mark.*
priority seems wrong · Issue #10406 · pytest-dev/pytest (original) (raw)
Originally asked about in #10391 and deemed to be a bug by @RonnyPfannschmidt
Suppose I have a test class for which I want to turn warnings into errors. However, some of the parametrizations I'm going to test will emit warnings and I just want to ignore them. I thought, I can write something like
import pytest import warnings
@pytest.mark.filterwarnings("error") class TestMarkClass: @pytest.mark.parametrize("_", [pytest.param(None, marks=pytest.mark.filterwarnings("ignore"))]) def test_foo(self, _): warnings.warn("foo")
However, it seems that the class wide mark takes priority over the one in the parametrization. On the flip side, if I mark the individual tests like
class TestMarkMethods: @pytest.mark.filterwarnings("error") @pytest.mark.parametrize("_", [pytest.param(None, marks=pytest.mark.filterwarnings("ignore"))]) def test_bar(self, _): warnings.warn("bar")
@pytest.mark.parametrize("_", [pytest.param(None, marks=pytest.mark.filterwarnings("ignore"))])
@pytest.mark.filterwarnings("error")
def test_baz(self, _):
warnings.warn("baz")
the mark in the parametrization takes priority. Running everything together with -rA
set gives
======================================= FAILURES =======================================
_____________________________ TestMarkClass.test_foo[None] _____________________________
Traceback (most recent call last):
File "/home/user/main.py", line 9, in test_foo
warnings.warn("foo")
UserWarning: foo
======================================== PASSES ========================================
=============================== short test summary info ================================
PASSED main.py::TestMarkMethods::test_bar[None]
PASSED main.py::TestMarkMethods::test_baz[None]
FAILED main.py::TestMarkClass::test_foo[None] - UserWarning: foo
============================= 1 failed, 2 passed in 0.68s ==============================
This order seems wrong to me. I would expect the marker in the parametrization takes the highest priority, followed by the marker on the test and lastly the marker on the class.
$ pytest --version pytest 7.1.3