Exemple #1
0
 def run():
     args, kwargs = search_strategy.reify(template)
     if print_example:
         current_reporter()(
             'Falsifying example: %s(%s)' % (
                 test.__name__,
                 arg_string(
                     test, args, kwargs
                 )
             )
         )
     return test(*args, **kwargs)
Exemple #2
0
    def execute_example(self, function):
        r, w = os.pipe()
        r = os.fdopen(r, 'rb')
        w = os.fdopen(w, 'wb')
        pid = os.fork()
        if not pid:  # pragma: no cover
            succeeded = False
            try:
                r.close()
                with with_reporter(report_to(w)):
                    function()
                    succeeded = True
            except BaseException as e:
                try:
                    pickle.dump(Error(e), w)
                    w.close()
                except:
                    traceback.print_exc()
            finally:
                if succeeded:
                    os._exit(0)
                else:
                    os._exit(1)
        w.close()
        error = None
        try:
            while True:
                message = pickle.load(r)
                if isinstance(message, Report):
                    current_reporter()(message.data)
                else:
                    assert isinstance(message, Error)
                    error = message.exception
                    break
        except EOFError:
            pass
        finally:
            r.close()

        if error is not None:
            raise error
        _, exitstatus = os.waitpid(pid, 0)
        if exitstatus:
            raise AbnormalExit()
Exemple #3
0
 def run():
     args, kwargs = search_strategy.reify(template)
     if print_example:
         current_reporter()('Falsifying example: %s(%s)' %
                            (test.__name__, arg_string(test, args, kwargs)))
     return test(*args, **kwargs)
Exemple #4
0
        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
            if selfy is not None:
                setup_example = getattr(selfy, 'setup_example', None)
                teardown_example = getattr(selfy, 'teardown_example', None)
            else:
                setup_example = None
                teardown_example = None

            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
                if setup_example is not None:
                    setup_example()
                try:
                    test(*arguments, **kwargs)
                finally:
                    if teardown_example is not None:
                        teardown_example((arguments, kwargs))
                return

            def convert_to_specifier(v):
                if isinstance(v, HypothesisProvided):
                    return v.value
                else:
                    return just(v)

            given_specifier = (
                tuple(map(convert_to_specifier, arguments)),
                {k: convert_to_specifier(v) for k, v in kwargs.items()}
            )

            def to_falsify(xs):
                testargs, testkwargs = xs
                try:
                    test(*testargs, **testkwargs)
                    return True
                except UnsatisfiedAssumption as e:
                    raise e
                except Exception:  # pylint: disable=broad-except
                    return False

            to_falsify.__name__ = test.__name__
            to_falsify.__qualname__ = getattr(
                test, '__qualname__', test.__name__)

            try:
                falsifying_example = verifier.falsify(
                    to_falsify, given_specifier,
                    setup_example=setup_example,
                    teardown_example=teardown_example,
                )[0]
            except Unfalsifiable:
                return

            strat = strategy(given_specifier)

            if setup_example is not None:
                setup_example()

            try:
                reified = strat.reify(falsifying_example)
                if _debugging_return_failing_example.value:
                    return reified
                false_args, false_kwargs = reified
                current_reporter()(
                    'Falsifying example: %s(%s)' % (
                        test.__name__,
                        arg_string(
                            test,
                            false_args,
                            false_kwargs,
                        )
                    )
                )
                # We run this one final time so we get good errors
                # Otherwise we would have swallowed all the reports of it
                # actually having gone wrong.
                test(*false_args, **false_kwargs)

            finally:
                if teardown_example is not None:
                    teardown_example(reified)

            # If we get here then something has gone wrong: We found a counter
            # example but it didn't fail when we invoked it again.
            raise Flaky(test, falsifying_example)
Exemple #5
0
        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
            if selfy is not None:
                setup_example = getattr(selfy, 'setup_example', None)
                teardown_example = getattr(selfy, 'teardown_example', None)
            else:
                setup_example = None
                teardown_example = None

            setup_example = setup_example or (lambda: None)
            teardown_example = teardown_example or (lambda ex: None)

            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
                setup_example()
                try:
                    test(*arguments, **kwargs)
                finally:
                    teardown_example((arguments, kwargs))
                return

            def convert_to_specifier(v):
                if isinstance(v, HypothesisProvided):
                    return v.value
                else:
                    return just(v)

            given_specifier = (tuple(map(convert_to_specifier, arguments)), {
                k: convert_to_specifier(v)
                for k, v in kwargs.items()
            })

            def to_falsify(xs):
                testargs, testkwargs = xs
                try:
                    test(*testargs, **testkwargs)
                    return True
                except UnsatisfiedAssumption as e:
                    raise e
                except Exception:  # pylint: disable=broad-except
                    return False

            to_falsify.__name__ = test.__name__
            to_falsify.__qualname__ = getattr(test, '__qualname__',
                                              test.__name__)

            if _debugging_return_failing_example.value:
                return verifier.falsify(
                    to_falsify,
                    given_specifier,
                    setup_example=setup_example,
                    teardown_example=teardown_example,
                )[0]

            try:
                falsifying_example = verifier.falsify(
                    to_falsify,
                    given_specifier,
                    setup_example=setup_example,
                    teardown_example=teardown_example,
                )[0]
            except Unfalsifiable:
                return

            try:
                false_args, false_kwargs = falsifying_example
                current_reporter()(
                    'Falsifying example: %s(%s)' %
                    (test.__name__, arg_string(
                        test,
                        false_args,
                        false_kwargs,
                    )))
                # We run this one final time so we get good errors
                # Otherwise we would have swallowed all the reports of it
                # actually having gone wrong.
                test(*false_args, **false_kwargs)

            finally:
                teardown_example(falsifying_example)

            # If we get here then something has gone wrong: We found a counter
            # example but it didn't fail when we invoked it again.
            raise Flaky(test, falsifying_example)