diff --git a/changelog/12564.bugfix.rst b/changelog/12564.bugfix.rst new file mode 100644 index 000000000..344bf8b7e --- /dev/null +++ b/changelog/12564.bugfix.rst @@ -0,0 +1 @@ +Fix the ``stacklevel`` used when warning about marks used on fixtures. diff --git a/src/_pytest/fixtures.py b/src/_pytest/fixtures.py index 0151a4d9c..1f0740d09 100644 --- a/src/_pytest/fixtures.py +++ b/src/_pytest/fixtures.py @@ -1192,7 +1192,7 @@ class FixtureFunctionMarker: ) if hasattr(function, "pytestmark"): - warnings.warn(MARKED_FIXTURE, stacklevel=2) + warnings.warn(MARKED_FIXTURE, stacklevel=4) function = wrap_function_to_error_out_if_called_directly(function, self) diff --git a/testing/deprecated_test.py b/testing/deprecated_test.py index 5d0e69c58..0fd2e9510 100644 --- a/testing/deprecated_test.py +++ b/testing/deprecated_test.py @@ -185,6 +185,8 @@ def test_fixture_disallow_on_marked_functions(): # from applying @fixture twice # ValueError("fixture is being applied more than once to the same function") assert len(record) == 1 + # should point to this file + assert record[0].filename == __file__ def test_fixture_disallow_marks_on_fixtures():