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.
This commit is contained in:
Ran Benita 2024-03-08 23:08:28 +02:00
parent a9d1f55a0f
commit 71671f60b5
2 changed files with 15 additions and 5 deletions

View File

@ -771,8 +771,9 @@ class SubRequest(FixtureRequest):
requested_fixture = self._format_fixturedef_line(requested_fixturedef) requested_fixture = self._format_fixturedef_line(requested_fixturedef)
fail( fail(
f"ScopeMismatch: You tried to access the {requested_scope.value} scoped " f"ScopeMismatch: You tried to access the {requested_scope.value} scoped "
f"fixture {argname} with a {self._scope.value} scoped request object, " f"fixture {argname} with a {self._scope.value} scoped request object. "
f"involved factories:\n{fixture_stack}\n{requested_fixture}", f"Requesting fixture stack:\n{fixture_stack}\n"
f"Requested fixture:\n{requested_fixture}",
pytrace=False, pytrace=False,
) )

View File

@ -1247,8 +1247,9 @@ class TestFixtureUsages:
result = pytester.runpytest() result = pytester.runpytest()
result.stdout.fnmatch_lines( result.stdout.fnmatch_lines(
[ [
"*ScopeMismatch*involved factories*", "*ScopeMismatch*Requesting fixture stack*",
"test_receives_funcargs_scope_mismatch.py:6: def arg2(arg1)", "test_receives_funcargs_scope_mismatch.py:6: def arg2(arg1)",
"Requested fixture:",
"test_receives_funcargs_scope_mismatch.py:2: def arg1()", "test_receives_funcargs_scope_mismatch.py:2: def arg1()",
"*1 error*", "*1 error*",
] ]
@ -1274,7 +1275,13 @@ class TestFixtureUsages:
) )
result = pytester.runpytest() result = pytester.runpytest()
result.stdout.fnmatch_lines( 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: def test_invalid_scope(self, pytester: Pytester) -> None:
@ -2488,8 +2495,10 @@ class TestFixtureMarker:
assert result.ret == ExitCode.TESTS_FAILED assert result.ret == ExitCode.TESTS_FAILED
result.stdout.fnmatch_lines( result.stdout.fnmatch_lines(
[ [
"*ScopeMismatch*involved factories*", "*ScopeMismatch*Requesting fixture stack*",
"test_it.py:6: def fixmod(fixfunc)", "test_it.py:6: def fixmod(fixfunc)",
"Requested fixture:",
"test_it.py:3: def fixfunc()",
] ]
) )