def test_can_nest(): d = DynamicVariable(1) with d.with_value(2): assert d.value == 2 with d.with_value(3): assert d.value == 3 assert d.value == 2 assert d.value == 1
def assume(condition): # type: (Any) -> bool """Calling ``assume`` is like an :ref:`assert <python:assert>` that marks the example as bad, rather than failing the test. This allows you to specify properties that you *assume* will be true, and let Hypothesis try to avoid similar examples in future. """ if not condition: raise UnsatisfiedAssumption() return True _current_build_context = DynamicVariable(None) def current_build_context(): context = _current_build_context.value if context is None: raise InvalidArgument(u"No build context registered") return context class BuildContext(object): def __init__(self, data, is_final=False, close_on_capture=True): self.data = data self.tasks = [] self.is_final = is_final self.close_on_capture = close_on_capture
return self._database def __enter__(self): default_context_manager = Settings.default_variable.with_value(self) self.defaults_stack().append(default_context_manager) default_context_manager.__enter__() return self def __exit__(self, *args, **kwargs): default_context_manager = self.defaults_stack().pop() return default_context_manager.__exit__(*args, **kwargs) default = DefaultSettings() Settings.default_variable = DynamicVariable(Settings()) Setting = namedtuple( u'Setting', (u'name', u'description', u'default', u'options')) Settings.define_setting( u'min_satisfying_examples', default=5, description=""" Raise Unsatisfiable for any tests which do not produce at least this many values that pass all assume() calls and which have not exhaustively covered the search space. """ )
def __set__(self, obj, value): obj.__dict__[self.name] = value def __delete__(self, obj): raise AttributeError('Cannot delete attribute %s' % (self.name, )) @property def __doc__(self): default = repr(getattr(settings.default, self.name)) if \ self.show_default else '(dynamically calculated)' return '\n'.join((all_settings[self.name].description, 'default value: %s' % (default, ))) default_variable = DynamicVariable(None) class settingsMeta(type): def __init__(self, *args, **kwargs): super(settingsMeta, self).__init__(*args, **kwargs) @property def default(self): v = default_variable.value if v is not None: return v if hasattr(settings, '_current_profile'): settings.load_profile(settings._current_profile) assert default_variable.value is not None return default_variable.value
from hypothesis.internal.compat import escape_unicode_characters from hypothesis.utils.dynamicvariables import DynamicVariable def silent(value): pass def default(value): try: print(value) except UnicodeEncodeError: print(escape_unicode_characters(value)) reporter = DynamicVariable(default) def current_reporter(): return reporter.value def with_reporter(new_reporter): return reporter.with_value(new_reporter) def current_verbosity(): return settings.default.verbosity def to_text(textish):
# # This Source Code Form is subject to the terms of the Mozilla Public License, # v. 2.0. If a copy of the MPL was not distributed with this file, You can # obtain one at http://mozilla.org/MPL/2.0/. # # END HEADER from __future__ import division, print_function, absolute_import import math from hypothesis.utils.dynamicvariables import DynamicVariable from hypothesis.internal.conjecture.data import Status from hypothesis.internal.conjecture.engine import ExitReason collector = DynamicVariable(None) class Statistics(object): def __init__(self, engine): self.passing_examples = len( engine.status_runtimes.get(Status.VALID, ())) self.invalid_examples = len( engine.status_runtimes.get(Status.INVALID, []) + engine.status_runtimes.get(Status.OVERRUN, []) ) self.failing_examples = len(engine.status_runtimes.get( Status.INTERESTING, ())) runtimes = sorted(
return ( type(exception), filename, lineno, # Note that if __cause__ is set it is always equal to __context__, explicitly # to support introspection when debugging, so we can use that unconditionally. get_interesting_origin(exception.__context__) if exception.__context__ else (), # We distinguish exception groups by the inner exceptions, as for __context__ tuple( map(get_interesting_origin, exception.exceptions ) if isinstance(exception, BaseExceptionGroup) else []), ) current_pytest_item = DynamicVariable(None) def _get_exceptioninfo(): # ExceptionInfo was moved to the top-level namespace in Pytest 7.0 if "pytest" in sys.modules: with contextlib.suppress(Exception): # From Pytest 7, __init__ warns on direct calls. return sys.modules["pytest"].ExceptionInfo.from_exc_info if "_pytest._code" in sys.modules: # pragma: no cover # old versions only with contextlib.suppress(Exception): return sys.modules["_pytest._code"].ExceptionInfo return None # pragma: no cover # coverage tests always use pytest def format_exception(err, tb):
def test_can_assign(): d = DynamicVariable(1) assert d.value == 1 with d.with_value(2): assert d.value == 2 assert d.value == 1
def __init__(self, *args, **kwargs): super(SettingsMeta, self).__init__(*args, **kwargs) self.default_variable = DynamicVariable(None)
from collections import namedtuple from hypothesis.control import assume from hypothesis.reporting import current_reporter from hypothesis.specifiers import just from hypothesis.errors import InvalidArgument from hypothesis.internal.verifier import Flaky, Verifier, Unfalsifiable, \ UnsatisfiedAssumption from hypothesis.internal.reflection import arg_string, copy_argspec from hypothesis.utils.dynamicvariables import DynamicVariable [assume] HypothesisProvided = namedtuple('HypothesisProvided', ('value,')) _debugging_return_failing_example = DynamicVariable(False) def given(*generator_arguments, **generator_kwargs): """A decorator for turning a test function that accepts arguments into a randomized test. This is the main entry point to Hypothesis. See the full tutorial for details of its behaviour. """ if 'verifier' in generator_kwargs: verifier = generator_kwargs.pop('verifier') verifier.start_time = time.time() else: verifier = Verifier(