Exemple #1
0
def test_a_verifier_retrieves_previous_failing_examples_from_the_database():
    database = ExampleDatabase()
    verifier = Verifier(settings=hs.Settings(database=database))
    verifier.falsify(lambda x: x < 11, int)
    called = []

    def save_calls(t):
        called.append(t)
        return False

    verifier2 = Verifier(settings=hs.Settings(database=database))
    verifier2.falsify(save_calls, int)
    assert called[0] == 11
    assert all(0 <= x <= 11 for x in called)
Exemple #2
0
def test_does_not_falsify_if_all_args_given():
    verifier = Verifier()
    verifier.falsify = None

    @given(int, int, verifier=verifier)
    def foo(x, y):
        pass

    foo(1, 2)
Exemple #3
0
from hypothesis.internal.verifier import Verifier

hs.Settings.default.max_examples = 1000


TestStandardDescriptorFeatures1 = strategy_test_suite(datetime)
TestStandardDescriptorFeatures2 = strategy_test_suite(
    timezone_aware_datetime)
TestStandardDescriptorFeatures3 = strategy_test_suite(naive_datetime)
TestStandardDescriptorFeatures4 = strategy_test_suite(one_of((
    naive_datetime,
    timezone_aware_datetime,
)))


falsify = Verifier().falsify


def test_can_find_after_the_year_2000():
    falsify(lambda x: x.year > 2000, datetime)


def test_can_find_before_the_year_2000():
    falsify(lambda x: x.year < 2000, datetime)


def test_can_find_each_month():
    for i in hrange(1, 12):
        falsify(lambda x: x.month != i, datetime)

Exemple #4
0
def test_french_names_may_have_an_accent():
    Verifier().falsify(lambda x: 'é' not in x,
                       FakeFactory('name', locale='fr_FR'))
Exemple #5
0
def test_can_falsify_in_derandomize_mode():
    settings = Settings(derandomize=True)
    v = Verifier(settings=settings)
    v.falsify(lambda x: x > 0, int)
Exemple #6
0
def test_verifier_explodes_when_you_mix_random_and_derandom():
    settings = Settings(derandomize=True)
    with pytest.raises(ValueError):
        Verifier(settings=settings, random=Random())
Exemple #7
0
# coding=utf-8

# Copyright (C) 2013-2015 David R. MacIver ([email protected])

# This file is part of Hypothesis (https://github.com/DRMacIver/hypothesis)

# 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 hypothesis.internal.verifier import Verifier
import hypothesis.settings as hs

settings = hs.Settings(max_examples=100, timeout=4)

small_verifier = Verifier(settings=settings, )
Exemple #8
0
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(
            settings=generator_kwargs.pop('settings', None),
            random=generator_kwargs.pop('random', None),
        )

    if not (generator_arguments or generator_kwargs):
        raise InvalidArgument(
            'given must be called with at least one argument')

    def run_test_with_generator(test):
        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)
        for a in arguments:
            if a in generator_kwargs:
                specifiers.append(generator_kwargs[a])

        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
            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)

        wrapped_test.__name__ = test.__name__
        wrapped_test.__doc__ = test.__doc__
        wrapped_test.verifier = verifier
        wrapped_test.is_hypothesis_test = True
        return wrapped_test

    return run_test_with_generator
def test_slow_failing_test_1(x):
    time.sleep(0.05)
    assert not calls[0]
    calls[0] = 1


@fails
@given(int, settings=timeout_settings)
def test_slow_failing_test_2(x):
    time.sleep(0.05)
    assert not calls[1]
    calls[1] = 1


@fails
@given(int, verifier=Verifier(settings=timeout_settings))
def test_slow_failing_test_3(x):
    time.sleep(0.05)
    assert not calls[2]
    calls[2] = 1


@fails
@given(int, verifier=Verifier(settings=timeout_settings))
def test_slow_failing_test_4(x):
    time.sleep(0.05)
    assert not calls[3]
    calls[3] = 1


@fails
Exemple #10
0
def strategy_test_suite(specifier, max_examples=100, random=None):
    settings = Settings(
        database=None,
        max_examples=max_examples,
        average_list_length=2.0,
    )
    random = random or Random()
    verifier = Verifier(settings=settings, random=random)
    strat = strategy(specifier, settings)
    specifier_test = given(TemplatesFor(specifier), settings=settings)

    class ValidationSuite(TestCase):
        def __repr__(self):
            return 'strategy_test_suite(%s)' % (show(specifier), )

        @given(specifier, settings=settings)
        def test_does_not_error(self, value):
            pass

        def test_can_give_example(self):
            strat.example()

        @specifier_test
        def test_is_basic(self, value):
            def is_basic(v):
                return isinstance(
                    v, integer_types + (list, type(None), text_type)) and (
                        not isinstance(v, list) or all(is_basic(w) for w in v))

            supposedly_basic = strat.to_basic(value)
            self.assertTrue(is_basic(supposedly_basic), repr(supposedly_basic))

        @specifier_test
        def test_can_round_trip_through_the_database(self, template):
            empty_db = ExampleDatabase(backend=SQLiteBackend(':memory:'), )
            try:
                storage = empty_db.storage_for(specifier)
                storage.save(template)
                values = list(storage.fetch())
                assert len(values) == 1
                assert strat.to_basic(template) == strat.to_basic(values[0])
            finally:
                empty_db.close()

        @specifier_test
        def test_template_is_hashable(self, template):
            hash(template)

        @specifier_test
        def test_can_minimize_to_empty(self, template):
            simplest = list(strat.simplify_such_that(template,
                                                     lambda x: True))[-1]
            assert list(strat.simplify(simplest)) == []

        @given(Random, verifier=verifier)
        def test_can_perform_all_basic_operations(self, random):
            parameter = strat.draw_parameter(random)
            template = strat.draw_template(BuildContext(random), parameter)
            minimal_template = list(
                strat.simplify_such_that(template, lambda x: True))[-1]
            strat.reify(minimal_template)
            assert (strat.to_basic(minimal_template) == strat.to_basic(
                strat.from_basic(strat.to_basic(minimal_template))))

    return ValidationSuite
Exemple #11
0
def smallest_tree(predicate):
    d = NAryTree(int, int, int)
    strat = strategy(d)
    v = Verifier()
    return strat.reify(v.falsify(lambda t: not predicate(t), d)[0])
Exemple #12
0
def test_a_verifier_can_still_do_its_thing_if_a_saved_example_fails():
    database = ExampleDatabase()
    verifier = Verifier(settings=hs.Settings(database=database))
    verifier.falsify(lambda x: x < 11, int)
    verifier2 = Verifier(settings=hs.Settings(database=database))
    verifier2.falsify(lambda x: x < 100, int)
Exemple #13
0
def test_a_verifier_saves_any_failing_examples_in_its_database():
    database = ExampleDatabase()
    verifier = Verifier(settings=hs.Settings(database=database))
    counterexample = verifier.falsify(lambda x: x > 0, int)
    saved = list(database.storage_for((int, )).fetch())
    assert saved == [counterexample]