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

Warn on direct import of hypothesis.given #526

Merged
merged 1 commit into from
May 17, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions brownie/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,7 @@ class BrownieEnvironmentWarning(Warning):

class InvalidArgumentWarning(BrownieEnvironmentWarning):
pass


class BrownieTestWarning(Warning):
pass
19 changes: 18 additions & 1 deletion brownie/test/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from hypothesis.errors import HypothesisDeprecationWarning

from brownie import network
from brownie.exceptions import BrownieTestWarning

from .stateful import state_machine # NOQA: F401
from .strategies import strategy # NOQA: F401
Expand All @@ -30,11 +31,27 @@ def isolation_wrapper(*args, **kwargs):

# hypothesis.given must wrap the target test to correctly
# impersonate the call signature for pytest
hy_given = hypothesis.given(*given_args, **given_kwargs)
hy_given = _hypothesis_given(*given_args, **given_kwargs)
hy_wrapped = hy_given(test)

if hasattr(hy_wrapped, "hypothesis"):
hy_wrapped.hypothesis.inner_test = isolation_wrapper
return hy_wrapped

return outer_wrapper


def _given_warning_wrapper(*args, **kwargs):
warnings.warn(
"Directly importing `hypothesis.given` may result in improper isolation"
" between test runs. You should import `brownie.test.given` instead.",
BrownieTestWarning,
)
return _hypothesis_given(*args, **kwargs)


def _apply_given_wrapper():
hypothesis.given = _given_warning_wrapper


_hypothesis_given = hypothesis.given
3 changes: 2 additions & 1 deletion brownie/test/managers/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@

from brownie._config import CONFIG
from brownie.project.scripts import _get_ast_hash
from brownie.test import coverage, output
from brownie.test import _apply_given_wrapper, coverage, output

from .utils import convert_outcome


class PytestBrownieBase:
def __init__(self, config, project):
_apply_given_wrapper()

self.config = config
# required when brownie project is in a subfolder of another project
Expand Down