Skip to content

Commit

Permalink
Improve fixture message
Browse files Browse the repository at this point in the history
This isn't perfect, but it seems like a big improvement over the status quo.

Closes #3018.

Co-Authored-By: Stuart Cook <Zalathar@users.noreply.github.com>
  • Loading branch information
Zac-HD and Zalathar committed Nov 26, 2021
1 parent 221aa1a commit 616b2f3
Showing 1 changed file with 14 additions and 10 deletions.
24 changes: 14 additions & 10 deletions hypothesis-python/src/_hypothesis_pytestplugin.py
Expand Up @@ -44,6 +44,19 @@
SEED_OPTION,
EXPLAIN_OPTION,
]
_FIXTURE_MSG = """Function-scoped fixture {0!r} used by {1!r}
Function-scoped fixtures are not reset between examples generated by
`@given(...)`, which is often surprising and can cause subtle test bugs.
If you were expecting the fixture to run separately for each generated example,
then unfortunately you will need to find a different way to achieve your goal
(e.g. using a similar context manager instead of a fixture).
If you are confident that your test will work correctly even though the
fixture is not reset between generated examples, you can suppress this health
check to assure Hypothesis that you understand what you are doing.
"""


class StoringReporter:
Expand Down Expand Up @@ -222,15 +235,6 @@ def pytest_runtest_call(item):
# Warn about function-scoped fixtures, excluding autouse fixtures because
# the advice is probably not actionable and the status quo seems OK...
# See https://github.com/HypothesisWorks/hypothesis/issues/377 for detail.
msg = (
"%s uses the %r fixture, which is reset between function calls but not "
"between test cases generated by `@given(...)`. You can change it to "
"a module- or session-scoped fixture if it is safe to reuse; if not "
"we recommend using a context manager inside your test function. See "
"https://docs.pytest.org/en/latest/how-to/fixtures.html"
"#scope-sharing-fixtures-across-classes-modules-packages-or-session "
"for details on fixture scope."
)
argnames = None
for fx_defs in item._request._fixturemanager.getfixtureinfo(
node=item, func=item.function, cls=None
Expand All @@ -243,7 +247,7 @@ def pytest_runtest_call(item):
if active_fx.scope == "function":
fail_health_check(
settings,
msg % (item.nodeid, fx.argname),
_FIXTURE_MSG.format(fx.argname, item.nodeid),
HealthCheck.function_scoped_fixture,
)

Expand Down

0 comments on commit 616b2f3

Please sign in to comment.