From 71671f60b570e631d0b663ec4285319ade3c1ce5 Mon Sep 17 00:00:00 2001 From: Ran Benita Date: Fri, 8 Mar 2024 23:08:28 +0200 Subject: [PATCH] fixtures: improve fixture scope mismatch message - Separate the requesting from the requested. - Avoid the term "factory", I think most people don't distinguish between "fixture" and "fixture function" (i.e. "factory") and would find the term "factory" unfamiliar. --- src/_pytest/fixtures.py | 5 +++-- testing/python/fixtures.py | 15 ++++++++++++--- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/src/_pytest/fixtures.py b/src/_pytest/fixtures.py index e322c3f18..40568a4a4 100644 --- a/src/_pytest/fixtures.py +++ b/src/_pytest/fixtures.py @@ -771,8 +771,9 @@ class SubRequest(FixtureRequest): requested_fixture = self._format_fixturedef_line(requested_fixturedef) fail( f"ScopeMismatch: You tried to access the {requested_scope.value} scoped " - f"fixture {argname} with a {self._scope.value} scoped request object, " - f"involved factories:\n{fixture_stack}\n{requested_fixture}", + f"fixture {argname} with a {self._scope.value} scoped request object. " + f"Requesting fixture stack:\n{fixture_stack}\n" + f"Requested fixture:\n{requested_fixture}", pytrace=False, ) diff --git a/testing/python/fixtures.py b/testing/python/fixtures.py index 6edff6ecd..8d59b36d3 100644 --- a/testing/python/fixtures.py +++ b/testing/python/fixtures.py @@ -1247,8 +1247,9 @@ class TestFixtureUsages: result = pytester.runpytest() result.stdout.fnmatch_lines( [ - "*ScopeMismatch*involved factories*", + "*ScopeMismatch*Requesting fixture stack*", "test_receives_funcargs_scope_mismatch.py:6: def arg2(arg1)", + "Requested fixture:", "test_receives_funcargs_scope_mismatch.py:2: def arg1()", "*1 error*", ] @@ -1274,7 +1275,13 @@ class TestFixtureUsages: ) result = pytester.runpytest() result.stdout.fnmatch_lines( - ["*ScopeMismatch*involved factories*", "* def arg2*", "*1 error*"] + [ + "*ScopeMismatch*Requesting fixture stack*", + "* def arg2(arg1)", + "Requested fixture:", + "* def arg1()", + "*1 error*", + ], ) def test_invalid_scope(self, pytester: Pytester) -> None: @@ -2488,8 +2495,10 @@ class TestFixtureMarker: assert result.ret == ExitCode.TESTS_FAILED result.stdout.fnmatch_lines( [ - "*ScopeMismatch*involved factories*", + "*ScopeMismatch*Requesting fixture stack*", "test_it.py:6: def fixmod(fixfunc)", + "Requested fixture:", + "test_it.py:3: def fixfunc()", ] )