def __call__(self, specifier, settings=None): if isinstance(specifier, SearchStrategy): return specifier if settings is None: settings = Settings() note_deprecation( ('Calling strategy with non-strategy object %s is deprecated ' 'and will be removed in Hypothesis 2.0. Use the functions in ' 'hypothesis.strategies instead.') % (repr(specifier), ), settings) result = super(StrategyExtMethod, self).__call__(specifier, settings) assert isinstance(result, SearchStrategy) return result
def __call__(self, specifier, settings=None): if isinstance(specifier, SearchStrategy): return specifier if settings is None: settings = Settings() note_deprecation(( 'Calling strategy with non-strategy object %s is deprecated ' 'and will be removed in Hypothesis 2.0. Use the functions in ' 'hypothesis.strategies instead.') % ( repr(specifier), ), settings) result = super(StrategyExtMethod, self).__call__(specifier, settings) assert isinstance(result, SearchStrategy) return result
def load_entry_points(name=None): for entry_point in pkg_resources.iter_entry_points( group=u'hypothesis.extra', name=name): if entry_point.name in (u'hypothesisdatetime', u'hypothesisdjango', u'hypothesisfakefactory', u'hypothesisnumpy'): base_name = entry_point.name.replace(u'hypothesis', u'') note_deprecation( u'Ignoring obsolete extra package hypothesis-%s. This ' u'functionality is now included in hypothesis core. You ' u'should uninstall the extra package.' % (base_name, ), Settings.default) continue elif entry_point.name == u'hypothesispytest': note_deprecation( u'You have an obsolete version of the hypothesis-pytest ' u'plugin installed. Please update to a more recent version.', Settings.default) continue else: note_deprecation( u'The extra package mechanism is deprecated and will go away ' u"in Hypothesis 2.0. Just write a normal package and don't " u'have it in the Hypothesis namespace.', Settings.default) package = entry_point.load() # pragma: no cover if package not in loaded: loaded.add(package) __path__.extend(package.__path__) package.load()
def load_entry_points(name=None): for entry_point in pkg_resources.iter_entry_points( group=u'hypothesis.extra', name=name ): if entry_point.name in ( u'hypothesisdatetime', u'hypothesisdjango', u'hypothesisfakefactory', u'hypothesisnumpy' ): base_name = entry_point.name.replace(u'hypothesis', u'') note_deprecation( u'Ignoring obsolete extra package hypothesis-%s. This ' u'functionality is now included in hypothesis core. You ' u'should uninstall the extra package.' % (base_name,), Settings.default ) continue elif entry_point.name == u'hypothesispytest': note_deprecation( u'You have an obsolete version of the hypothesis-pytest ' u'plugin installed. Please update to a more recent version.', Settings.default ) continue else: note_deprecation( u'The extra package mechanism is deprecated and will go away ' u"in Hypothesis 2.0. Just write a normal package and don't " u'have it in the Hypothesis namespace.', Settings.default ) package = entry_point.load() # pragma: no cover if package not in loaded: loaded.add(package) __path__.extend(package.__path__) package.load()
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. """ # Keyword only arguments but actually supported in the full range of # pythons Hypothesis handles. pop so we don't later pick these up as # if they were keyword specifiers for data to pass to the test. provided_random = generator_kwargs.pop('random', None) settings = generator_kwargs.pop('settings', None) or Settings.default if (provided_random is not None) and settings.derandomize: raise InvalidArgument( 'Cannot both be derandomized and provide an explicit random') if not (generator_arguments or generator_kwargs): raise InvalidArgument( 'given must be called with at least one argument') if generator_arguments and generator_kwargs: note_deprecation( 'Mixing positional and keyword arguments in a call to given is ' 'deprecated. Use one or the other.', settings ) def run_test_with_generator(test): if settings.derandomize: assert provided_random is None random = Random( function_digest(test) ) else: random = provided_random or Random() original_argspec = inspect.getargspec(test) if original_argspec.varargs: raise InvalidArgument( 'varargs are not supported with @given' ) extra_kwargs = [ k for k in generator_kwargs if k not in original_argspec.args] if extra_kwargs and not original_argspec.keywords: raise InvalidArgument( '%s() got an unexpected keyword argument %r' % ( test.__name__, extra_kwargs[0] )) if ( len(generator_arguments) > len(original_argspec.args) ): raise InvalidArgument(( 'Too many positional arguments for %s() (got %d but' ' expected at most %d') % ( test.__name__, len(generator_arguments), len(original_argspec.args))) arguments = original_argspec.args + sorted(extra_kwargs) specifiers = list(generator_arguments) seen_kwarg = None for a in arguments: if a in generator_kwargs: seen_kwarg = seen_kwarg or a specifiers.append(generator_kwargs[a]) else: if seen_kwarg is not None: raise InvalidArgument(( 'Argument %s comes after keyword %s which has been ' 'specified, but does not itself have a ' 'specification') % ( a, seen_kwarg )) argspec = inspect.ArgSpec( args=arguments, keywords=original_argspec.keywords, varargs=original_argspec.varargs, defaults=tuple(map(HypothesisProvided, specifiers)) ) @copy_argspec( test.__name__, argspec ) def wrapped_test(*arguments, **kwargs): selfy = None # Because we converted all kwargs to given into real args and # error if we have neither args nor kwargs, this should always # be valid assert argspec.args selfy = kwargs.get(argspec.args[0]) if isinstance(selfy, HypothesisProvided): selfy = None test_runner = executor(selfy) for example in getattr( wrapped_test, 'hypothesis_explicit_examples', () ): if example.args: example_kwargs = dict(zip( argspec.args[-len(example.args):], example.args )) else: example_kwargs = dict(example.kwargs) for k, v in kwargs.items(): if not isinstance(v, HypothesisProvided): example_kwargs[k] = v test_runner( lambda: test(*arguments, **example_kwargs) ) if not any( isinstance(x, HypothesisProvided) for xs in (arguments, kwargs.values()) for x in xs ): # All arguments have been satisfied without needing to invoke # hypothesis test_runner(lambda: test(*arguments, **kwargs)) return def convert_to_specifier(v): if isinstance(v, HypothesisProvided): return strategy(v.value, settings) else: return sd.just(v) given_specifier = sd.tuples( sd.tuples(*map(convert_to_specifier, arguments)), sd.fixed_dictionaries({ k: convert_to_specifier(v) for k, v in kwargs.items()}) ) search_strategy = strategy(given_specifier, settings) if settings.database: storage = settings.database.storage_for( given_specifier, search_strategy) else: storage = None def is_template_example(xs): try: test_runner(reify_and_execute( search_strategy, xs, test, always_print=settings.max_shrinks <= 0 )) return False except UnsatisfiedAssumption as e: raise e except Exception as e: if settings.max_shrinks <= 0: raise e verbose_report(traceback.format_exc) return True is_template_example.__name__ = test.__name__ is_template_example.__qualname__ = getattr( test, '__qualname__', test.__name__) falsifying_template = None try: falsifying_template = best_satisfying_template( search_strategy, random, is_template_example, settings, storage ) except NoSuchExample: return test_runner(reify_and_execute( search_strategy, falsifying_template, test, print_example=True )) test_runner(reify_and_execute( search_strategy, falsifying_template, test_is_flaky(test), print_example=True )) wrapped_test.__name__ = test.__name__ wrapped_test.__doc__ = test.__doc__ wrapped_test.is_hypothesis_test = True wrapped_test.hypothesis_explicit_examples = getattr( test, 'hypothesis_explicit_examples', [] ) return wrapped_test return run_test_with_generator
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. """ # Keyword only arguments but actually supported in the full range of # pythons Hypothesis handles. pop so we don't later pick these up as # if they were keyword specifiers for data to pass to the test. provided_random = generator_kwargs.pop("random", None) settings = generator_kwargs.pop("settings", None) or Settings.default if (provided_random is not None) and settings.derandomize: raise InvalidArgument("Cannot both be derandomized and provide an explicit random") if not (generator_arguments or generator_kwargs): raise InvalidArgument("given must be called with at least one argument") if generator_arguments and generator_kwargs: note_deprecation( "Mixing positional and keyword arguments in a call to given is " "deprecated. Use one or the other.", settings, ) def run_test_with_generator(test): if settings.derandomize: assert provided_random is None random = Random(function_digest(test)) else: random = provided_random or Random() original_argspec = inspect.getargspec(test) if generator_arguments and original_argspec.varargs: raise InvalidArgument("varargs are not supported with positional arguments to @given") extra_kwargs = [k for k in generator_kwargs if k not in original_argspec.args] if extra_kwargs and not original_argspec.keywords: raise InvalidArgument("%s() got an unexpected keyword argument %r" % (test.__name__, extra_kwargs[0])) if len(generator_arguments) > len(original_argspec.args): raise InvalidArgument( ("Too many positional arguments for %s() (got %d but" " expected at most %d") % (test.__name__, len(generator_arguments), len(original_argspec.args)) ) arguments = original_argspec.args + sorted(extra_kwargs) specifiers = list(generator_arguments) seen_kwarg = None for a in arguments: if a in generator_kwargs: seen_kwarg = seen_kwarg or a specifiers.append(generator_kwargs[a]) else: if seen_kwarg is not None: raise InvalidArgument( ( "Argument %s comes after keyword %s which has been " "specified, but does not itself have a " "specification" ) % (a, seen_kwarg) ) argspec = inspect.ArgSpec( args=arguments, keywords=original_argspec.keywords, varargs=original_argspec.varargs, defaults=tuple(map(HypothesisProvided, specifiers)), ) @copy_argspec(test.__name__, argspec) def wrapped_test(*arguments, **kwargs): selfy = None arguments, kwargs = convert_positional_arguments(wrapped_test, arguments, kwargs) # Because we converted all kwargs to given into real args and # error if we have neither args nor kwargs, this should always # be valid assert argspec.args selfy = kwargs.get(argspec.args[0]) if isinstance(selfy, HypothesisProvided): selfy = None test_runner = executor(selfy) for example in getattr(wrapped_test, "hypothesis_explicit_examples", ()): if example.args: example_kwargs = dict(zip(argspec.args[-len(example.args) :], example.args)) else: example_kwargs = dict(example.kwargs) for k, v in kwargs.items(): if not isinstance(v, HypothesisProvided): example_kwargs[k] = v # Note: Test may mutate arguments and we can't rerun explicit # examples, so we have to calculate the failure message at this # point rather than than later. message_on_failure = "Falsifying example: %s(%s)" % ( test.__name__, arg_string(test, arguments, example_kwargs), ) try: test_runner(lambda: test(*arguments, **example_kwargs)) except BaseException: report(message_on_failure) raise if not any(isinstance(x, HypothesisProvided) for xs in (arguments, kwargs.values()) for x in xs): # All arguments have been satisfied without needing to invoke # hypothesis test_runner(lambda: test(*arguments, **kwargs)) return def convert_to_specifier(v): if isinstance(v, HypothesisProvided): return strategy(v.value, settings) else: return sd.just(v) given_specifier = sd.tuples( sd.tuples(*map(convert_to_specifier, arguments)), sd.fixed_dictionaries(dict((k, convert_to_specifier(v)) for (k, v) in kwargs.items())), ) search_strategy = strategy(given_specifier, settings) if settings.database: storage = settings.database.storage(fully_qualified_name(test)) else: storage = None last_exception = [None] repr_for_last_exception = [None] def is_template_example(xs): record_repr = [None] try: test_runner( reify_and_execute( search_strategy, xs, test, always_print=settings.max_shrinks <= 0, record_repr=record_repr ) ) return False except UnsatisfiedAssumption as e: raise e except Exception as e: if settings.max_shrinks <= 0: raise e last_exception[0] = traceback.format_exc() repr_for_last_exception[0] = record_repr[0] verbose_report(last_exception[0]) return True is_template_example.__name__ = test.__name__ is_template_example.__qualname__ = qualname(test) falsifying_template = None try: falsifying_template = best_satisfying_template( search_strategy, random, is_template_example, settings, storage ) except NoSuchExample: return assert last_exception[0] is not None with settings: test_runner(reify_and_execute(search_strategy, falsifying_template, test, print_example=True)) report("Failed to reproduce exception. Expected: \n" + last_exception[0]) test_runner( reify_and_execute( search_strategy, falsifying_template, test_is_flaky(test, repr_for_last_exception[0]), print_example=True, ) ) wrapped_test.__name__ = test.__name__ wrapped_test.__doc__ = test.__doc__ wrapped_test.is_hypothesis_test = True wrapped_test.hypothesis_explicit_examples = getattr(test, "hypothesis_explicit_examples", []) return wrapped_test return run_test_with_generator
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. """ # Keyword only arguments but actually supported in the full range of # pythons Hypothesis handles. pop so we don't later pick these up as # if they were keyword specifiers for data to pass to the test. provided_random = generator_kwargs.pop('random', None) settings = generator_kwargs.pop('settings', None) or Settings.default if (provided_random is not None) and settings.derandomize: raise InvalidArgument( 'Cannot both be derandomized and provide an explicit random') if not (generator_arguments or generator_kwargs): raise InvalidArgument( 'given must be called with at least one argument') if generator_arguments and generator_kwargs: note_deprecation( 'Mixing positional and keyword arguments in a call to given is ' 'deprecated. Use one or the other.', settings ) def run_test_with_generator(test): if settings.derandomize: assert provided_random is None random = Random( function_digest(test) ) else: random = provided_random or Random() original_argspec = inspect.getargspec(test) if generator_arguments and original_argspec.varargs: raise InvalidArgument( 'varargs are not supported with positional arguments to @given' ) extra_kwargs = [ k for k in generator_kwargs if k not in original_argspec.args] if extra_kwargs and not original_argspec.keywords: raise InvalidArgument( '%s() got an unexpected keyword argument %r' % ( test.__name__, extra_kwargs[0] )) if ( len(generator_arguments) > len(original_argspec.args) ): raise InvalidArgument(( 'Too many positional arguments for %s() (got %d but' ' expected at most %d') % ( test.__name__, len(generator_arguments), len(original_argspec.args))) arguments = original_argspec.args specifiers = list(generator_arguments) seen_kwarg = None for a in arguments: if a in generator_kwargs: seen_kwarg = seen_kwarg or a specifiers.append(generator_kwargs[a]) else: if seen_kwarg is not None: raise InvalidArgument(( 'Argument %s comes after keyword %s which has been ' 'specified, but does not itself have a ' 'specification') % ( a, seen_kwarg )) argspec = inspect.ArgSpec( args=arguments, keywords=original_argspec.keywords, varargs=original_argspec.varargs, defaults=tuple(map(HypothesisProvided, specifiers)) ) unused_kwargs = {} for k in extra_kwargs: unused_kwargs[k] = HypothesisProvided(generator_kwargs[k]) @copy_argspec( test.__name__, argspec ) def wrapped_test(*arguments, **kwargs): selfy = None arguments, kwargs = convert_positional_arguments( wrapped_test, arguments, kwargs) # Anything in unused_kwargs hasn't been injected through # argspec.defaults, so we need to add them. for k in unused_kwargs: if k not in kwargs: kwargs[k] = unused_kwargs[k] # If the test function is a method of some kind, the bound object # will be the first named argument if there are any, otherwise the # first vararg (if any). if argspec.args: selfy = kwargs.get(argspec.args[0]) elif arguments: selfy = arguments[0] if isinstance(selfy, HypothesisProvided): selfy = None test_runner = executor(selfy) for example in getattr( wrapped_test, 'hypothesis_explicit_examples', () ): if example.args: example_kwargs = dict(zip( argspec.args[-len(example.args):], example.args )) else: example_kwargs = dict(example.kwargs) for k, v in kwargs.items(): if not isinstance(v, HypothesisProvided): example_kwargs[k] = v # Note: Test may mutate arguments and we can't rerun explicit # examples, so we have to calculate the failure message at this # point rather than than later. message_on_failure = 'Falsifying example: %s(%s)' % ( test.__name__, arg_string(test, arguments, example_kwargs) ) try: test_runner( lambda: test(*arguments, **example_kwargs) ) except BaseException: report(message_on_failure) raise if not any( isinstance(x, HypothesisProvided) for xs in (arguments, kwargs.values()) for x in xs ): # All arguments have been satisfied without needing to invoke # hypothesis test_runner(lambda: test(*arguments, **kwargs)) return def convert_to_specifier(v): if isinstance(v, HypothesisProvided): return strategy(v.value, settings) else: return sd.just(v) given_specifier = sd.tuples( sd.tuples(*map(convert_to_specifier, arguments)), sd.fixed_dictionaries(dict( (k, convert_to_specifier(v)) for (k, v) in kwargs.items())) ) search_strategy = strategy(given_specifier, settings) if settings.database: storage = settings.database.storage( fully_qualified_name(test)) else: storage = None last_exception = [None] repr_for_last_exception = [None] def is_template_example(xs): record_repr = [None] try: test_runner(reify_and_execute( search_strategy, xs, test, always_print=settings.max_shrinks <= 0, record_repr=record_repr, )) return False except UnsatisfiedAssumption as e: raise e except Exception as e: if settings.max_shrinks <= 0: raise e last_exception[0] = traceback.format_exc() repr_for_last_exception[0] = record_repr[0] verbose_report(last_exception[0]) return True is_template_example.__name__ = test.__name__ is_template_example.__qualname__ = qualname(test) falsifying_template = None try: falsifying_template = best_satisfying_template( search_strategy, random, is_template_example, settings, storage ) except NoSuchExample: return assert last_exception[0] is not None with settings: test_runner(reify_and_execute( search_strategy, falsifying_template, test, print_example=True )) report( 'Failed to reproduce exception. Expected: \n' + last_exception[0], ) test_runner(reify_and_execute( search_strategy, falsifying_template, test_is_flaky(test, repr_for_last_exception[0]), print_example=True )) wrapped_test.__name__ = test.__name__ wrapped_test.__doc__ = test.__doc__ wrapped_test.is_hypothesis_test = True wrapped_test.hypothesis_explicit_examples = getattr( test, 'hypothesis_explicit_examples', [] ) return wrapped_test return run_test_with_generator
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. """ # Keyword only arguments but actually supported in the full range of # pythons Hypothesis handles. pop so we don't later pick these up as # if they were keyword specifiers for data to pass to the test. provided_random = generator_kwargs.pop(u'random', None) settings = generator_kwargs.pop(u'settings', None) or Settings.default if (provided_random is not None) and settings.derandomize: raise InvalidArgument( u'Cannot both be derandomized and provide an explicit random') if not (generator_arguments or generator_kwargs): raise InvalidArgument( u'given must be called with at least one argument') if generator_arguments and generator_kwargs: note_deprecation( u'Mixing positional and keyword arguments in a call to given is ' u'deprecated. Use one or the other.', settings ) def run_test_with_generator(test): if settings.derandomize: assert provided_random is None random = Random(function_digest(test)) else: random = provided_random or Random() original_argspec = getargspec(test) if generator_arguments and original_argspec.varargs: raise InvalidArgument( u'varargs are not supported with positional arguments to ' u'@given' ) extra_kwargs = [ k for k in generator_kwargs if k not in original_argspec.args] if extra_kwargs and not original_argspec.keywords: raise InvalidArgument( u'%s() got an unexpected keyword argument %r' % ( test.__name__, extra_kwargs[0] )) if ( len(generator_arguments) > len(original_argspec.args) ): raise InvalidArgument(( u'Too many positional arguments for %s() (got %d but' u' expected at most %d') % ( test.__name__, len(generator_arguments), len(original_argspec.args))) arguments = original_argspec.args specifiers = list(generator_arguments) seen_kwarg = None for a in arguments: if isinstance(a, list): # pragma: no cover raise InvalidArgument(( u'Cannot decorate function %s() because it has ' u'destructuring arguments') % ( test.__name__, )) if a in generator_kwargs: seen_kwarg = seen_kwarg or a specifiers.append(generator_kwargs[a]) else: if seen_kwarg is not None: raise InvalidArgument(( u'Argument %s comes after keyword %s which has been ' u'specified, but does not itself have a ' u'specification') % ( a, seen_kwarg )) argspec = inspect.ArgSpec( args=arguments, keywords=original_argspec.keywords, varargs=original_argspec.varargs, defaults=tuple(map(HypothesisProvided, specifiers)) ) unused_kwargs = {} for k in extra_kwargs: unused_kwargs[k] = HypothesisProvided(generator_kwargs[k]) @impersonate(test) @copy_argspec( test.__name__, argspec ) def wrapped_test(*arguments, **kwargs): selfy = None arguments, kwargs = convert_positional_arguments( wrapped_test, arguments, kwargs) # Anything in unused_kwargs hasn't been injected through # argspec.defaults, so we need to add them. for k in unused_kwargs: if k not in kwargs: kwargs[k] = unused_kwargs[k] # If the test function is a method of some kind, the bound object # will be the first named argument if there are any, otherwise the # first vararg (if any). if argspec.args: selfy = kwargs.get(argspec.args[0]) elif arguments: selfy = arguments[0] if isinstance(selfy, HypothesisProvided): selfy = None test_runner = executor(selfy) for example in getattr( wrapped_test, u'hypothesis_explicit_examples', () ): if example.args: example_kwargs = dict(zip( argspec.args[-len(example.args):], example.args )) else: example_kwargs = dict(example.kwargs) for k, v in kwargs.items(): if not isinstance(v, HypothesisProvided): example_kwargs[k] = v # Note: Test may mutate arguments and we can't rerun explicit # examples, so we have to calculate the failure message at this # point rather than than later. message_on_failure = u'Falsifying example: %s(%s)' % ( test.__name__, arg_string(test, arguments, example_kwargs) ) try: test_runner( lambda: test(*arguments, **example_kwargs) ) except BaseException: report(message_on_failure) raise if not any( isinstance(x, HypothesisProvided) for xs in (arguments, kwargs.values()) for x in xs ): # All arguments have been satisfied without needing to invoke # hypothesis test_runner(lambda: test(*arguments, **kwargs)) return def convert_to_specifier(v): if isinstance(v, HypothesisProvided): return strategy(v.value, settings) else: return sd.just(v) given_specifier = sd.tuples( sd.tuples(*map(convert_to_specifier, arguments)), sd.fixed_dictionaries(dict( (k, convert_to_specifier(v)) for (k, v) in kwargs.items())) ) search_strategy = strategy(given_specifier, settings) if settings.database: storage = settings.database.storage( fully_qualified_name(test)) else: storage = None last_exception = [None] repr_for_last_exception = [None] def is_template_example(xs): record_repr = [None] try: test_runner(reify_and_execute( search_strategy, xs, test, always_print=settings.max_shrinks <= 0, record_repr=record_repr, )) return False except UnsatisfiedAssumption as e: raise e except Exception as e: if settings.max_shrinks <= 0: raise e last_exception[0] = traceback.format_exc() repr_for_last_exception[0] = record_repr[0] verbose_report(last_exception[0]) return True is_template_example.__name__ = test.__name__ is_template_example.__qualname__ = qualname(test) falsifying_template = None try: falsifying_template = best_satisfying_template( search_strategy, random, is_template_example, settings, storage ) except NoSuchExample: return assert last_exception[0] is not None with settings: test_runner(reify_and_execute( search_strategy, falsifying_template, test, print_example=True )) report( u'Failed to reproduce exception. Expected: \n' + last_exception[0], ) test_runner(reify_and_execute( search_strategy, falsifying_template, test_is_flaky(test, repr_for_last_exception[0]), print_example=True )) for attr in dir(test): if attr[0] != '_' and not hasattr(wrapped_test, attr): setattr(wrapped_test, attr, getattr(test, attr)) wrapped_test.is_hypothesis_test = True return wrapped_test return run_test_with_generator