def test_digest_is_stable_across_process_runs(): # Hard coded as the only sensible way to check this doesn't change between # process runs. There's nothing special about these values. If you update # the code just update them to match. digest = function_digest(test_digests_are_reasonably_unique) print(repr(digest)) assert digest == b'\x8d\x07\xdb\xe1\xbeC\x92\xec-\xb4PWj\x0c%\x87'
def falsify(self, hypothesis, *argument_types): # pylint: disable=too-many-locals,too-many-branches """ Attempt to construct an example tuple x matching argument_types such that hypothesis(*x) returns a falsey value or throws an AssertionError """ random = self.random if random is None: random = Random(function_digest(hypothesis)) search_strategy = ( self.strategy_table.specification_for(argument_types)) storage = None if self.database is not None: try: storage = self.database.storage_for(argument_types) except NotSerializeable: pass def falsifies(args): # pylint: disable=missing-docstring try: return not hypothesis(*search_strategy.copy(args)) except AssertionError: return True except UnsatisfiedAssumption: return False track_seen = Tracker() falsifying_examples = [] examples_found = 0 satisfying_examples = 0 timed_out = False if argument_types: max_examples = self.max_examples min_satisfying_examples = self.min_satisfying_examples else: max_examples = 1 min_satisfying_examples = 1 example_source = ExampleSource(random=random, strategy=search_strategy, storage=storage, min_parameters=max( 2, int(float(max_examples) / 10))) start_time = time.time() def time_to_call_it_a_day(): """Have we exceeded our timeout?""" return time.time() >= start_time + self.timeout skipped_examples = 0 examples_seen = 0 # At present this loop will never exit normally . This needs proper # testing when "database only" mode becomes available but right now # it's not. for args in example_source: # pragma: no branch assert search_strategy.could_have_produced(args) if falsifying_examples: break if examples_seen >= max_examples: break if time_to_call_it_a_day(): break examples_seen += 1 if track_seen.track(args) > 1: example_source.mark_bad() skipped_examples += 1 if skipped_examples >= self.max_skipped_examples: raise Exhausted(hypothesis, examples_found) else: # This really is covered. I suspect a bug in coverage that # I have not yet narrowed down. It is impossible to execute # the other branch without first executing this one and # there is a test that cannot pass without executing the # other branch. continue # pragma: no cover else: skipped_examples = 0 examples_found += 1 try: is_falsifying_example = not hypothesis( *search_strategy.copy(args)) except AssertionError: is_falsifying_example = True except UnsatisfiedAssumption: example_source.mark_bad() continue satisfying_examples += 1 if is_falsifying_example: falsifying_examples.append(args) run_time = time.time() - start_time timed_out = run_time >= self.timeout if not falsifying_examples: if satisfying_examples < min_satisfying_examples: raise Unsatisfiable(hypothesis, satisfying_examples, run_time) elif timed_out: raise Timeout(hypothesis, satisfying_examples, run_time) else: raise Unfalsifiable(hypothesis) for example in falsifying_examples: if not falsifies(example): raise Flaky(hypothesis, example) best_example = falsifying_examples[0] for simpler in search_strategy.simplify_such_that( best_example, falsifies): best_example = simpler if time_to_call_it_a_day(): break if storage is not None: storage.save(best_example) return best_example
def test_digest_returns_the_same_value_for_two_calls(): assert (function_digest(test_simple_conversion) == function_digest( test_simple_conversion))
def test_digests_are_reasonably_unique(): assert ( function_digest(test_simple_conversion) != function_digest(test_does_not_error_on_dynamically_defined_functions))