示例#1
0
def test_loading_profile_keeps_expected_behaviour():
    Settings.register_profile('ci', Settings(max_examples=10000))
    Settings.load_profile('ci')
    assert Settings().max_examples == 10000
    with Settings(max_examples=5):
        assert Settings().max_examples == 5
    assert Settings().max_examples == 10000
示例#2
0
def test_inherits_an_empty_database(db):
    assert Settings.default.database is not None
    s = Settings(database=db)
    assert s.database is db
    with s:
        t = Settings()
    assert t.database is db
示例#3
0
def test_can_use_values_in_the_database():
    example = 'Hello world'
    database = ExampleDatabase(backend=SQLiteBackend(':memory:'))
    storage = database.storage_for(text_type)
    storage.save(example)
    verifier = Verifier(settings=Settings(database=database))
    assert verifier.falsify(lambda x: x != example, text_type) == (example, )
示例#4
0
def test_prints_debug_on_no_simplification():
    with Settings(verbosity=Verbosity.debug):
        with capture_out() as o:
            find(just(u'hi'), bool)
    v = o.getvalue()
    print(v)
    assert u'No simplifiers' in v
示例#5
0
        def test_will_handle_a_really_weird_failure(self):
            db = ExampleDatabase()

            @given(
                specifier,
                settings=Settings(
                    database=db,
                    max_examples=max_examples,
                    min_satisfying_examples=2,
                    average_list_length=2.0,
                )
            )
            def nope(x):
                s = hashlib.sha1(show(x).encode('utf-8')).digest()
                if Random(s).randint(0, 1):
                    raise Rejected()
            try:
                try:
                    nope()
                except Rejected:
                    pass
                try:
                    nope()
                except Rejected:
                    pass
            finally:
                db.close()
示例#6
0
    def __call__(self, specifier, settings=None):
        from hypothesis.searchstrategy.strategies import SearchStrategy

        if isinstance(specifier, SearchStrategy):
            return specifier

        self.load_deprecated_api()

        if settings is None:
            settings = Settings()

        try:
            result = super(StrategyExtMethod, self).__call__(
                specifier, settings)
        except NotImplementedError:
            quiet_raise(NotImplementedError((
                'Expected a SearchStrategy but got %r of type %s. '
                'Note: This is a NotImplementedError for legacy reasons and '
                'will become an InvalidArgumentError in Hypothesis 2.0.'
            ) % (specifier, type(specifier).__name__)))
        note_deprecation((
            'Conversion of %r to strategy is deprecated '
            'and will be removed in Hypothesis 2.0. Use %r instead.') % (
                specifier, result
        ), settings)

        assert isinstance(result, SearchStrategy)
        return result
示例#7
0
文件: core.py 项目: trowt/hypothesis
def find(specifier, condition, settings=None, random=None, storage=None):
    settings = settings or Settings(
        max_examples=2000,
        min_satisfying_examples=0,
        max_shrinks=2000,
    )

    search = strategy(specifier, settings)

    if storage is None and settings.database is not None:
        storage = settings.database.storage(
            u'find(%s)' %
            (binascii.hexlify(function_digest(condition)).decode(u'ascii'), ))

    random = random or Random()
    successful_examples = [0]

    def template_condition(template):
        with BuildContext():
            result = search.reify(template)
            success = condition(result)

        if success:
            successful_examples[0] += 1

        if not successful_examples[0]:
            verbose_report(lambda: u'Trying example %s' % (repr(result), ))
        elif success:
            if successful_examples[0] == 1:
                verbose_report(lambda: u'Found satisfying example %s' %
                               (repr(result), ))
            else:
                verbose_report(lambda: u'Shrunk example to %s' %
                               (repr(result), ))
        return success

    template_condition.__name__ = condition.__name__
    tracker = Tracker()

    try:
        template = best_satisfying_template(
            search,
            random,
            template_condition,
            settings,
            tracker=tracker,
            max_parameter_tries=2,
            storage=storage,
        )
        with BuildContext():
            return search.reify(template)
    except Timeout:
        raise
    except NoSuchExample:
        if search.template_upper_bound <= len(tracker):
            raise DefinitelyNoSuchExample(
                get_pretty_function_description(condition),
                search.template_upper_bound,
            )
        raise NoSuchExample(get_pretty_function_description(condition))
示例#8
0
        def test_will_handle_a_really_weird_failure(self, rnd):
            db = ExampleDatabase()

            @given(specifier,
                   settings=Settings(
                       settings,
                       database=db,
                       max_examples=max_examples,
                       min_satisfying_examples=2,
                       average_list_length=2.0,
                   ),
                   random=rnd)
            def nope(x):
                s = hashlib.sha1(repr(x).encode(u'utf-8')).digest()
                assert Random(s).randint(0, 1) == Random(s).randint(0, 1)
                if Random(s).randint(0, 1):
                    raise Rejected(u'%r with digest %r' % (x, s))

            try:
                try:
                    nope()
                except Rejected:
                    pass
                try:
                    nope()
                except Rejected:
                    pass
            finally:
                db.close()
示例#9
0
def test_puts_elements_of_set_in_database():
    database = ExampleDatabase(backend=SQLiteBackend(':memory:'))
    verifier = Verifier(settings=Settings(database=database))
    verifier.falsify(lambda x: not x, {int})
    assert list(database.storage_for([int]).fetch()) == []
    assert list(database.storage_for({int}).fetch()) == [{0}]
    assert list(database.storage_for(int).fetch()) == [0]
示例#10
0
def find(specifier, condition, settings=None, random=None):
    settings = settings or Settings(
        max_examples=2000,
        min_satisfying_examples=0,
    )

    search = strategy(specifier, settings)
    random = random or Random()

    def template_condition(template):
        return assume(condition(search.reify(template)))

    template_condition.__name__ = condition.__name__
    tracker = Tracker()

    try:
        return search.reify(
            best_satisfying_template(
                search,
                random,
                template_condition,
                settings,
                None,
                tracker=tracker,
            ))
    except NoSuchExample:
        if search.size_upper_bound <= len(tracker):
            raise DefinitelyNoSuchExample(
                get_pretty_function_description(condition),
                search.size_upper_bound,
            )
        raise NoSuchExample(get_pretty_function_description(condition))
示例#11
0
def test_reports_differently_for_single_shrink():
    with capture_verbosity(Verbosity.verbose) as o:
        @fails
        @given(basic(SillyStrategy), settings=Settings(database=None))
        def test_foo(x):
            assert False
        test_foo()
    assert u'shrunk example once' in o.getvalue()
示例#12
0
def test_includes_progress_in_verbose_mode():
    with capture_verbosity(Verbosity.verbose) as o:
        with Settings(verbosity=Verbosity.verbose):
            find(lists(integers()), lambda x: sum(x) >= 1000000)

    out = o.getvalue()
    assert out
    assert u'Shrunk example' in out
    assert u'Found satisfying example' in out
示例#13
0
def test_puts_keyword_arguments_in_the_database_from_given():
    database = ExampleDatabase(backend=SQLiteBackend(':memory:'))
    verifier = Verifier(settings=Settings(database=database))

    @given(x=int, verifier=verifier)
    def a_test(x):
        assert False

    with pytest.raises(AssertionError):
        a_test()
    assert list(database.storage_for(int).fetch()) == [0]
示例#14
0
        def test_will_find_a_failure_from_the_database(self):
            db = ExampleDatabase()

            @given(specifier, settings=Settings(max_examples=10, database=db))
            def nope(x):
                raise Rejected()
            try:
                for i in hrange(3):
                    self.assertRaises(Rejected, nope)  # pragma: no cover
            finally:
                db.close()
示例#15
0
    def __init__(self, base, extend, max_leaves):
        self.max_leaves = max_leaves
        self.base = TemplateLimitedStrategy(base)
        self.extend = extend

        with Settings(average_list_length=2):
            strategies = [self.base, self.extend(self.base)]
            while 2**len(strategies) <= max_leaves:
                strategies.append(extend(OneOfStrategy(tuple(strategies))))
            super(RecursiveStrategy,
                  self).__init__(OneOfStrategy(tuple(strategies)))
示例#16
0
 def __init__(self, strategy, constraint=None, execute=None, settings=None):
     self.strategy = strategy
     self.constraint = constraint
     self.settings = settings or Settings(
         max_examples=10000,
         max_iterations=10000,
         max_shrinks=5000,
     )
     self.constraint = constraint or (lambda x: True)
     self.execute = execute or (lambda f: f())
     self._set_template()
     ALL_FIXTURES.append(weakref.ref(self))
示例#17
0
def test_prints_initial_attempts_on_find():

    with capture_verbosity(Verbosity.verbose) as o:
        with Settings(verbosity=Verbosity.verbose):
            seen = []

            def not_first(x):
                if not seen:
                    seen.append(x)
                    return False
                return x not in seen
            find(integers(), not_first)

    assert u'Trying example' in o.getvalue()
示例#18
0
    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
示例#19
0
文件: core.py 项目: kbara/hypothesis
def find(specifier, condition, settings=None, random=None):
    settings = settings or Settings(
        max_examples=2000,
        min_satisfying_examples=0,
    )

    search = strategy(specifier, settings)
    random = random or Random()
    successful_examples = [0]

    def template_condition(template):
        result = search.reify(template)
        success = condition(result)

        if success:
            successful_examples[0] += 1

        if not successful_examples[0]:
            verbose_report(lambda: 'Trying example %s' % (show(result), ))
        elif success:
            if successful_examples[0] == 1:
                verbose_report(lambda: 'Found satisfying example %s' %
                               (show(result), ))
            else:
                verbose_report(lambda: 'Shrunk example to %s' %
                               (show(result), ))
        return assume(success)

    template_condition.__name__ = condition.__name__
    tracker = Tracker()

    try:
        return search.reify(
            best_satisfying_template(
                search,
                random,
                template_condition,
                settings,
                None,
                tracker=tracker,
            ))
    except NoSuchExample:
        if search.size_upper_bound <= len(tracker):
            raise DefinitelyNoSuchExample(
                get_pretty_function_description(condition),
                search.size_upper_bound,
            )
        raise NoSuchExample(get_pretty_function_description(condition))
示例#20
0
def test_load_profile():
    Settings.load_profile('default')
    assert Settings.default.max_examples == 200
    assert Settings.default.max_shrinks == 500
    assert Settings.default.min_satisfying_examples == 5

    Settings.register_profile('test', Settings(max_examples=10, max_shrinks=5))

    Settings.load_profile('test')

    assert Settings.default.max_examples == 10
    assert Settings.default.max_shrinks == 5
    assert Settings.default.min_satisfying_examples == 5

    Settings.load_profile('default')

    assert Settings.default.max_examples == 200
    assert Settings.default.max_shrinks == 500
    assert Settings.default.min_satisfying_examples == 5
示例#21
0
    def __call__(self, specifier, settings=None):
        from hypothesis.searchstrategy.strategies import SearchStrategy

        if isinstance(specifier, SearchStrategy):
            return specifier

        self.load_deprecated_api()

        if settings is None:
            settings = Settings()

        result = super(StrategyExtMethod, self).__call__(specifier, settings)
        note_deprecation(
            ('Conversion of %r to strategy is deprecated '
             'and will be removed in Hypothesis 2.0. Use %r instead.') %
            (specifier, result), settings)

        assert isinstance(result, SearchStrategy)
        return result
示例#22
0
        class StateMachineTestCase(TestCase):
            settings = Settings()

            def runTest(self):
                run_state_machine_as_test(state_machine_class)
示例#23
0
    class ValidationSuite(TestCase):
        def __repr__(self):
            return u'strategy_test_suite(%s)' % (repr(specifier), )

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

        def test_can_give_example(self):
            strat.example()

        def test_can_give_list_of_examples(self):
            strategy(lists(strat)).example()

        def test_will_give_unsatisfiable_if_all_rejected(self):
            @given(specifier, settings=settings)
            def nope(x):
                assume(False)

            self.assertRaises(Unsatisfiable, nope)

        def test_will_find_a_constant_failure(self):
            @given(specifier, settings=settings)
            def nope(x):
                raise Rejected()

            self.assertRaises(Rejected, nope)

        def test_will_find_a_failure_from_the_database(self):
            db = ExampleDatabase()

            @given(specifier,
                   settings=Settings(settings, max_examples=10, database=db))
            def nope(x):
                raise Rejected()

            try:
                for i in hrange(3):
                    self.assertRaises(Rejected, nope)  # pragma: no cover
            finally:
                db.close()

        @given(templates_for(specifier),
               templates_for(specifier),
               settings=settings)
        def test_simplicity_is_asymmetric(self, x, y):
            assert not (strat.strictly_simpler(x, y)
                        and strat.strictly_simpler(y, x))

        @given(integers(), settings=settings)
        def test_templates_generated_from_same_random_are_equal(self, i):
            try:
                t1 = strat.draw_and_produce(Random(i))
                t2 = strat.draw_and_produce(Random(i))
            except BadTemplateDraw:
                assume(False)

            if t1 is not t2:
                assert t1 == t2
                assert hash(t1) == hash(t2)

        @given(integers(), settings=settings)
        def test_templates_generated_from_same_random_are_equal_after_reify(
                self, i):
            try:
                t1 = strat.draw_and_produce(Random(i))
                t2 = strat.draw_and_produce(Random(i))
            except BadTemplateDraw:
                assume(False)
            if t1 is not t2:
                with BuildContext():
                    strat.reify(t1)
                with BuildContext():
                    strat.reify(t2)
                assert t1 == t2
                assert hash(t1) == hash(t2)

        @given(randoms(), settings=settings)
        def test_will_handle_a_really_weird_failure(self, rnd):
            db = ExampleDatabase()

            @given(specifier,
                   settings=Settings(
                       settings,
                       database=db,
                       max_examples=max_examples,
                       min_satisfying_examples=2,
                       average_list_length=2.0,
                   ),
                   random=rnd)
            def nope(x):
                s = hashlib.sha1(repr(x).encode(u'utf-8')).digest()
                assert Random(s).randint(0, 1) == Random(s).randint(0, 1)
                if Random(s).randint(0, 1):
                    raise Rejected(u'%r with digest %r' % (x, s))

            try:
                try:
                    nope()
                except Rejected:
                    pass
                try:
                    nope()
                except Rejected:
                    pass
            finally:
                db.close()

        @specifier_test
        def test_is_basic(self, value, rnd):
            def is_basic(v):
                if v is None or isinstance(v, text_type):
                    return True
                if isinstance(v, integer_types):
                    return not (abs(v) >> 64)
                if isinstance(v, list):
                    return all(is_basic(w) for w in v)
                return False

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

        @specifier_test
        def test_only_raises_bad_data_in_from_basic(self, value, rnd):
            basic = strat.to_basic(value)

            messed_basic = mutate_basic(basic, rnd)
            try:
                strat.from_basic(messed_basic)
            except BadData:
                pass

        @specifier_test
        def test_can_round_trip_through_the_database(self, template, rnd):
            empty_db = ExampleDatabase(backend=SQLiteBackend(u':memory:'), )
            try:
                storage = empty_db.storage(u'round trip')
                storage.save(template, strat)
                values = list(storage.fetch(strat))
                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, rnd):
            hash(template)
            # It can be easy to forget to convert a list...
            hash(strat.from_basic(strat.to_basic(template)))

        @specifier_test
        def test_can_minimize_to_empty(self, template, rnd):
            simplest = template
            tracker = Tracker()
            while True:
                for t in strat.full_simplify(rnd, simplest):
                    if tracker.track(t) == 1:
                        simplest = t
                        break
                else:
                    break
            assert list(strat.full_simplify(rnd, simplest)) == []

        @specifier_test
        def test_full_simplify_completes(self, template, rnd):
            # Cut off at 100 for the occasional case where we get
            # really very large templates which have too many simplifies.
            for x in islice(strat.full_simplify(rnd, template), 100):
                pass

        @specifier_test
        def test_does_not_increase_complexity(self, template, rnd):
            for s in islice(strat.full_simplify(rnd, template), 100):
                assert not strat.strictly_simpler(template, s)

        @given(randoms(), settings=Settings(settings, max_examples=100))
        def test_can_create_templates(self, random):
            parameter = strat.draw_parameter(random)
            try:
                strat.draw_template(random, parameter)
            except BadTemplateDraw:
                assume(False)
示例#24
0
def test_verifier_explodes_when_you_mix_random_and_derandom():
    settings = Settings(derandomize=True)
    with pytest.raises(ValueError):
        Verifier(settings=settings, random=Random())
示例#25
0
 def __call__(self, specifier, settings=None):
     if settings is None:
         settings = Settings()
     result = super(StrategyExtMethod, self).__call__(specifier, settings)
     assert isinstance(result, SearchStrategy)
     return result
示例#26
0
import pytest

from hypothesis.settings import Settings
from hypothesis.internal.debug import timeout
from hypothesis.strategytests import templates_for
from tests.common.basic import Bitfields
from hypothesis.strategies import integers, floats, just, one_of, \
    sampled_from, streaming, basic, lists, booleans, dictionaries, tuples, \
    frozensets, complex_numbers, sets, text, binary, decimals, fractions, \
    none, randoms, builds, fixed_dictionaries, recursive
from hypothesis.internal.compat import hrange
from hypothesis.searchstrategy.narytree import n_ary_tree
from hypothesis.utils.show import show


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

__all__ = [u'small_verifier', u'timeout', u'standard_types', u'OrderedPair']


OrderedPair = namedtuple(u'OrderedPair', (u'left', u'right'))


ordered_pair = integers().flatmap(
    lambda right: integers(min_value=0).map(
        lambda length: OrderedPair(right - length, right)))


def constant_list(strat):
    return strat.flatmap(
        lambda v: lists(just(v)),
示例#27
0
def test_does_print_verbose_in_debug():
    with Settings(verbosity=Verbosity.debug):
        with capture_out() as o:
            verbose_report(u'Hi')
    assert u'Hi' in o.getvalue()
示例#28
0
def test_does_not_print_debug_in_verbose():
    with Settings(verbosity=Verbosity.verbose):
        with capture_out() as o:
            debug_report(u'Hi')
    assert not o.getvalue()
示例#29
0
    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()

        def test_can_give_list_of_examples(self):
            strategy([strat]).example()

        def test_will_give_unsatisfiable_if_all_rejected(self):
            @given(specifier, settings=settings)
            def nope(x):
                assume(False)

            with self.assertRaises(Unsatisfiable):
                nope()

        def test_will_find_a_constant_failure(self):
            @given(specifier, settings=settings)
            def nope(x):
                raise Rejected()

            with self.assertRaises(Rejected):
                nope()

        def test_will_find_a_failure_from_the_database(self):
            db = ExampleDatabase()

            @given(specifier, settings=Settings(max_examples=10, database=db))
            def nope(x):
                raise Rejected()

            try:
                for _ in hrange(3):
                    with self.assertRaises(Rejected):
                        nope()  # pragma: no branch
            finally:
                db.close()

        def test_will_handle_a_really_weird_failure(self):
            @given(specifier, settings=settings)
            def nope(x):
                if hash(show(specifier)) % 2:
                    raise Rejected()

            try:
                nope()
            except Rejected:
                pass

        @specifier_test
        def test_is_basic(self, value, rnd):
            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_only_raises_bad_data_in_from_basic(self, value, rnd):
            basic = strat.to_basic(value)

            messed_basic = mutate_basic(basic, rnd)
            try:
                strat.from_basic(messed_basic)
            except BadData:
                pass

        @specifier_test
        def test_can_round_trip_through_the_database(self, template, rnd):
            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, rnd):
            hash(template)
            # It can be easy to forget to convert a list...
            hash(strat.from_basic(strat.to_basic(template)))

        @given(TemplatesFor(specifier), Random, [[int]], settings=settings)
        def test_apply_all_simplifiers(self, template, rnd, path):
            path = list(filter(None, path))
            assume(path)
            current_template = template
            for local_route in path:
                simplifiers = list(strat.simplifiers(current_template))
                if not simplifiers:
                    break
                for i in local_route:
                    simplify = simplifiers[abs(i) % len(simplifiers)]
                    simpler = list(simplify(rnd, current_template))
                    if simpler:
                        current_template = random.choice(simpler)

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

        @specifier_test
        def test_can_complete_falsify_loop(self, template, rnd):
            for _ in strat.full_simplify(rnd, template):
                pass

        @given(Random, settings=Settings(max_examples=1000))
        def test_can_create_templates(self, random):
            parameter = strat.draw_parameter(random)
            strat.draw_template(BuildContext(random), parameter)
示例#30
0
def capture_verbosity(level):
    with capture_out() as o:
        with with_reporter(default_reporter):
            with Settings(verbosity=level):
                yield o