Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support and unittest for overridden fixtures, solving #2375. #2376

Merged
merged 2 commits into from Mar 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions hypothesis-python/RELEASE.rst
@@ -0,0 +1,4 @@
RELEASE_TYPE: patch

This patch fixes :issue:`2375`, preventing incorrect failure when a function
scoped fixture is overridden with a higher scoped fixture.
19 changes: 11 additions & 8 deletions hypothesis-python/src/hypothesis/extra/pytestplugin.py
Expand Up @@ -156,14 +156,17 @@ def pytest_runtest_call(item):
if argnames is None:
argnames = frozenset(signature(item.function).parameters)
for fx in fx_defs:
if fx.scope == "function" and fx.argname in argnames:
note_deprecation(
"%s uses the %r fixture, but function-scoped fixtures "
"should not be used with @given(...) tests, because "
"fixtures are not reset between generated examples!"
% (item.nodeid, fx.argname),
since="2020-02-29",
)
if fx.argname in argnames:
active_fx = item._request._get_active_fixturedef(fx.argname)
if active_fx.scope == "function":
note_deprecation(
"%s uses the %r fixture, but function-scoped"
" fixtures should not be used with @given(...)"
" tests, because fixtures are not reset "
"between generated examples!"
% (item.nodeid, fx.argname),
since="2020-02-29",
)

if item.get_closest_marker("parametrize") is not None:
# Give every parametrized test invocation a unique database key
Expand Down
23 changes: 23 additions & 0 deletions hypothesis-python/tests/pytest/test_fixtures.py
Expand Up @@ -102,3 +102,26 @@ def test_autouse_function_scoped_fixture(x):
def test_given_plus_function_scoped_non_autouse_fixtures_are_deprecated(testdir):
script = testdir.makepyfile(TESTSUITE)
testdir.runpytest(script, "-Werror").assert_outcomes(passed=1, failed=1)


TESTSCRIPT_OVERRIDE_FIXTURE = """
import pytest
from hypothesis import given, strategies as st

@pytest.fixture(scope="function", name="event_loop")
def event_loop_1():
return

@pytest.fixture(scope="module", name="event_loop")
def event_loop_2():
return

@given(x=st.integers())
def test_override_fixture(event_loop, x):
pass
"""


def test_given_plus_overridden_fixture(testdir):
script = testdir.makepyfile(TESTSCRIPT_OVERRIDE_FIXTURE)
testdir.runpytest(script, "-Werror").assert_outcomes(passed=1, failed=0)