diff --git a/hypothesis-python/src/hypothesis/core.py b/hypothesis-python/src/hypothesis/core.py index b7d182d49f..aa53c6b9a7 100644 --- a/hypothesis-python/src/hypothesis/core.py +++ b/hypothesis-python/src/hypothesis/core.py @@ -780,63 +780,65 @@ def test(*args, **kwargs): def run(data): # Set up dynamic context needed by a single test run. - with local_settings(self.settings): - with deterministic_PRNG(): - with BuildContext(data, is_final=is_final) as context: - if self.stuff.selfy is not None: - data.hypothesis_runner = self.stuff.selfy - # Generate all arguments to the test function. - args = self.stuff.args - kwargs = dict(self.stuff.kwargs) - if example_kwargs is None: - a, kw, argslices = context.prep_args_kwargs_from_strategies( - (), self.stuff.given_kwargs - ) - assert not a, "strategies all moved to kwargs by now" - else: - kw = example_kwargs - argslices = {} - kwargs.update(kw) - if expected_failure is not None: - nonlocal text_repr - text_repr = repr_call(test, args, kwargs) - if text_repr in self.xfail_example_reprs: - warnings.warn( - f"We generated {text_repr}, which seems identical " - "to one of your `@example(...).xfail()` cases. " - "Revise the strategy to avoid this overlap?", - HypothesisWarning, - # Checked in test_generating_xfailed_examples_warns! - stacklevel=6, - ) - - if print_example or current_verbosity() >= Verbosity.verbose: - printer = RepresentationPrinter(context=context) - if print_example: - printer.text("Falsifying example:") - else: - printer.text("Trying example:") - - if self.print_given_args: - printer.text(" ") - printer.repr_call( - test.__name__, - args, - kwargs, - force_split=True, - arg_slices=argslices, - leading_comment=( - "# " + context.data.slice_comments[(0, 0)] - if (0, 0) in context.data.slice_comments - else None - ), - ) - report(printer.getvalue()) - return test(*args, **kwargs) + if self.stuff.selfy is not None: + data.hypothesis_runner = self.stuff.selfy + # Generate all arguments to the test function. + args = self.stuff.args + kwargs = dict(self.stuff.kwargs) + if example_kwargs is None: + a, kw, argslices = context.prep_args_kwargs_from_strategies( + (), self.stuff.given_kwargs + ) + assert not a, "strategies all moved to kwargs by now" + else: + kw = example_kwargs + argslices = {} + kwargs.update(kw) + if expected_failure is not None: + nonlocal text_repr + text_repr = repr_call(test, args, kwargs) + if text_repr in self.xfail_example_reprs: + warnings.warn( + f"We generated {text_repr}, which seems identical " + "to one of your `@example(...).xfail()` cases. " + "Revise the strategy to avoid this overlap?", + HypothesisWarning, + # Checked in test_generating_xfailed_examples_warns! + stacklevel=6, + ) - # Run the test function once, via the executor hook. - # In most cases this will delegate straight to `run(data)`. - result = self.test_runner(data, run) + if print_example or current_verbosity() >= Verbosity.verbose: + printer = RepresentationPrinter(context=context) + if print_example: + printer.text("Falsifying example:") + else: + printer.text("Trying example:") + + if self.print_given_args: + printer.text(" ") + printer.repr_call( + test.__name__, + args, + kwargs, + force_split=True, + arg_slices=argslices, + leading_comment=( + "# " + context.data.slice_comments[(0, 0)] + if (0, 0) in context.data.slice_comments + else None + ), + ) + report(printer.getvalue()) + return test(*args, **kwargs) + + # self.test_runner can include the execute_example method, or setup/teardown + # _example, so it's important to get the PRNG and build context in place first. + with local_settings(self.settings): + with deterministic_PRNG(): + with BuildContext(data, is_final=is_final) as context: + # Run the test function once, via the executor hook. + # In most cases this will delegate straight to `run(data)`. + result = self.test_runner(data, run) # If a failure was expected, it should have been raised already, so # instead raise an appropriate diagnostic error. diff --git a/hypothesis-python/tests/cover/test_filtered_strategy.py b/hypothesis-python/tests/cover/test_filtered_strategy.py index 02a821f2f6..8ba40c1753 100644 --- a/hypothesis-python/tests/cover/test_filtered_strategy.py +++ b/hypothesis-python/tests/cover/test_filtered_strategy.py @@ -9,6 +9,7 @@ # obtain one at https://mozilla.org/MPL/2.0/. import hypothesis.strategies as st +from hypothesis.control import BuildContext from hypothesis.internal.conjecture.data import ConjectureData from hypothesis.strategies._internal.strategies import FilteredStrategy @@ -19,7 +20,8 @@ def test_filter_iterations_are_marked_as_discarded(): data = ConjectureData.for_buffer([0, 2, 1, 0]) - assert data.draw(x) == 0 + with BuildContext(data): + assert data.draw(x) == 0 assert data.has_discards