def test_can_simplify_leap_years(): s = StrategyTable().strategy(datetime) d = datetime(year=2012, month=2, day=29) t = list( s.simplify_such_that( d, lambda x: (x.month == 2) and (x.day == 29) and (x.year > 2000)))[-1] assert t.year == 2004
def test_a_derandomized_verifier_produces_the_same_results_called_twice(): table = StrategyTable() settings = hs.Settings(derandomize=True) table.define_specification_for(float, lambda *_: BrokenFloatStrategy()) v1 = Verifier(strategy_table=table, settings=settings) foo = lambda x: False x1 = v1.falsify(foo, float) x2 = v1.falsify(foo, float) assert x1 == x2
def test_minor_variations_in_code_change_the_randomization(): table = StrategyTable() settings = hs.Settings(derandomize=True) settings.database = None table.define_specification_for(float, lambda *_: BrokenFloatStrategy()) v1 = Verifier(strategy_table=table, settings=settings) x1 = v1.falsify(lambda x: x == 42, float) x2 = v1.falsify(lambda x: x == 1, float) assert x1 != x2
def test_two_verifiers_produce_different_results_in_normal_mode(): settings = hs.Settings() settings.database = None table = StrategyTable() table.define_specification_for(float, lambda *_: BrokenFloatStrategy()) v1 = Verifier(strategy_table=table, settings=settings) v2 = Verifier(strategy_table=table, settings=settings) x1 = v1.falsify(lambda x: False, float) x2 = v2.falsify(lambda x: False, float) assert x1 != x2
def __init__( self, strategy_table=None, random=None, settings=None, ): if settings is None: settings = hs.default self.database = settings.database self.strategy_table = strategy_table or StrategyTable() if self.database is not None: self.strategy_table = self.strategy_table.augment_with_examples( self.examples_for ) self.min_satisfying_examples = settings.min_satisfying_examples self.max_skipped_examples = settings.max_skipped_examples self.max_examples = settings.max_examples self.timeout = settings.timeout if settings.derandomize and random: raise ValueError( 'A verifier cannot both be derandomized and have a random ' 'generator') if settings.derandomize: self.random = None else: self.random = random or Random() self.max_regenerations = 0
def test_example_source_needs_random(): with pytest.raises(ValueError): ExampleSource( random=None, strategy=StrategyTable.default().strategy(int), storage=None, )
def test_can_grow_the_set_of_available_parameters_if_doing_badly(): runs = 10 number_grown = 0 number_grown_large = 0 for _ in hrange(runs): source = ExampleSource( random=random.Random(), strategy=StrategyTable.default().strategy(int), storage=None, min_parameters=1, ) i = 0 for example in source: if example < 0: source.mark_bad() i += 1 if i >= 100: break if len(source.parameters) > 1: number_grown += 1 if len(source.parameters) > 10: number_grown_large += 1 assert number_grown >= 0.5 * runs assert number_grown_large <= 0.5 * runs
def test_errors_if_you_mark_bad_before_fetching(): storage = None if hs.default.database is not None: storage = hs.default.database.storage_for(int) source = ExampleSource( random=random.Random(), strategy=StrategyTable.default().strategy(int), storage=storage, ) with pytest.raises(ValueError): source.mark_bad()
def test_storage_does_not_return_things_not_matching_strategy(): table = StrategyTable() strategy = JustStrategy(PickyStrategyLazyFormat()) strategy.could_have_produced = lambda *args: False table.define_specification_for(PickyStrategyLazyFormat, lambda s, d: strategy) converters = ConverterTable(strategy_table=table) converters.define_specification_for( PickyStrategyLazyFormat, lambda s, d: JustConverter(PickyStrategyLazyFormat())) database = ExampleDatabase( converters=converters, backend=SQLiteBackend(), ) stor = database.storage_for(PickyStrategyLazyFormat) database.backend.save(stor.key, 'null') assert list(database.backend.fetch(stor.key)) != [] assert list(stor.fetch()) == [] assert list(database.backend.fetch(stor.key)) == []
def test_tries_each_parameter_at_least_min_index_times(): source = ExampleSource(random=random.Random(), strategy=StrategyTable.default().strategy(int), storage=None, min_tries=5) i = 0 for x in source: i += 1 if i > 500: break if i % 2: source.mark_bad() # The last index may not have been fully populated assert all(c >= 5 for c in source.counts[:-1])
def test_negative_is_not_too_far_off_mean(): source = ExampleSource( random=random.Random(), strategy=StrategyTable.default().strategy(int), storage=None, ) positive = 0 i = 0 for example in source: if example >= 0: positive += 1 i += 1 if i >= N_EXAMPLES: break assert 0.3 <= float(positive) / N_EXAMPLES <= 0.7
def test_marking_negative_avoids_similar_examples(): source = ExampleSource( random=random.Random(), strategy=StrategyTable.default().strategy(int), storage=None, ) positive = 0 i = 0 for example in source: if example >= 0: positive += 1 else: source.mark_bad() i += 1 if i >= N_EXAMPLES: break assert float(positive) / N_EXAMPLES >= 0.8
def test_can_falsify_types_without_default_productions(): strategies = StrategyTable() strategies.define_specification_for( Bar, lambda s, d: BarStrategy( s.strategy(descriptors.integers_in_range(0, 100)))) with pytest.raises(MissingSpecification): StrategyTable.default().strategy(Bar) verifier = Verifier(strategy_table=strategies) assert verifier.falsify( lambda x: False, Bar, )[0] == Bar() assert verifier.falsify(lambda x: x.size() < 3, Bar)[0] == Bar(Bar(Bar()))
def run_test(): if condition is None: _condition = lambda x: True condition_string = '' else: _condition = condition condition_string = strip_lambda( reflection.get_pretty_function_description(condition)) count = 0 successful_runs = 0 strategy = StrategyTable.default().strategy(descriptor) for _ in hrange(MAX_RUNS): pv = strategy.parameter.draw(random) x = strategy.produce(random, pv) if not _condition(x): continue successful_runs += 1 if predicate(x): count += 1 if successful_runs < MIN_RUNS: raise ConditionTooHard( ('Unable to find enough examples satisfying predicate %s ' 'only found %d but required at least %d for validity') % (condition_string, successful_runs, MIN_RUNS)) result = Result( count, successful_runs, q, predicate, condition_string, ) p = cumulative_binomial_probability(successful_runs, q, count) run_test.test_result = result # The test passes if we fail to reject the null hypothesis that # the probability is at least q if p < REQUIRED_P: result.failed = True raise HypothesisFalsified(result.description() + ' rejected')
def test_can_derandomize_on_evalled_functions(): table = StrategyTable() settings = hs.Settings(derandomize=True) v = Verifier(strategy_table=table, settings=settings) assert v.falsify(eval('lambda x: x > 0'), int) == (0, )
from hypothesis import Verifier import hypothesis.settings as hs from hypothesis.strategytable import StrategyTable import hypothesis.searchstrategy as strat settings = hs.Settings(max_examples=100, timeout=4) small_table = StrategyTable() small_table.define_specification_for_instances( list, lambda strategies, descriptor: strat.ListStrategy( list(map(strategies.strategy, descriptor)), average_length=2.0 ) ) small_verifier = Verifier( settings=settings, strategy_table=small_table, )
def test_minimizes_to_empty(desc): x = falsify(always_false, desc)[0] s = StrategyTable.default().strategy(desc) assert not list(s.simplify(x))
def __init__(self, strategy_table=None): super(ConverterTable, self).__init__() self.strategy_table = strategy_table or StrategyTable.default()
steps=steps, init_args=init_args, init_kwargs=init_kwargs ) return TestRun(self.descriptor, steps) def unpack(self, x): steps = x.steps if self.requires_init: return ((x.init_args, x.init_kwargs), steps) else: return steps def simplify(self, x): pruned = x.prune() if pruned: yield pruned for y in super(StatefulStrategy, self).simplify(x): yield y StrategyTable.default().define_specification_for_classes( define_stateful_strategy, subclasses_of=StatefulTest ) ConverterTable.default().define_specification_for_classes( StatefulConverter, subclasses_of=StatefulTest )
def load(): StrategyTable.default().define_specification_for( datetime, lambda s, d: DatetimeStrategy())
class Awkward(str): pass ConverterTable.default().mark_not_serializeable(Awkward) class AwkwardStrategy(SearchStrategy): descriptor = Awkward parameter = params.CompositeParameter() def produce(self, random, pv): return Awkward() StrategyTable.default().define_specification_for( Awkward, lambda s, d: AwkwardStrategy()) def test_can_verify_a_non_serializale_type(): verifier = Verifier(settings=hs.Settings(database=ExampleDatabase())) verifier.falsify(lambda x: len(x) > 0, Awkward) def test_verifier_deduplicates_on_coming_out_of_the_database(): db = ExampleDatabase() storage = db.storage_for((frozenset({int}), )) db.backend.save(storage.key, '[1, 2, 3]') db.backend.save(storage.key, '[3, 2, 1]') counter = Counter() calls = [] good = frozenset({1, 2, 3})