Best Python code snippet using hypothesis
engine.py
Source:engine.py  
...158                    self.first_bug_found_at = self.call_count159            else:160                if sort_key(data.buffer) < sort_key(existing.buffer):161                    self.shrinks += 1162                    self.downgrade_buffer(existing.buffer)163                    self.__data_cache.unpin(existing.buffer)164                    changed = True165            if changed:166                self.save_buffer(data.buffer)167                self.interesting_examples[key] = data.as_result()168                self.__data_cache.pin(data.buffer)169                self.shrunk_examples.discard(key)170            if self.shrinks >= MAX_SHRINKS:171                self.exit_with(ExitReason.max_shrinks)172        if not self.interesting_examples:173            # Note that this logic is reproduced to end the generation phase when174            # we have interesting examples.  Update that too if you change this!175            # (The doubled implementation is because here we exit the engine entirely,176            #  while in the other case below we just want to move on to shrinking.)177            if self.valid_examples >= self.settings.max_examples:178                self.exit_with(ExitReason.max_examples)179            if self.call_count >= max(180                self.settings.max_examples * 10,181                # We have a high-ish default max iterations, so that tests182                # don't become flaky when max_examples is too low.183                1000,184            ):185                self.exit_with(ExitReason.max_iterations)186        if self.__tree_is_exhausted():187            self.exit_with(ExitReason.finished)188        self.record_for_health_check(data)189    def generate_novel_prefix(self):190        """Uses the tree to proactively generate a starting sequence of bytes191        that we haven't explored yet for this test.192        When this method is called, we assume that there must be at193        least one novel prefix left to find. If there were not, then the194        test run should have already stopped due to tree exhaustion.195        """196        return self.tree.generate_novel_prefix(self.random)197    @property198    def cap(self):199        return BUFFER_SIZE // 2200    def record_for_health_check(self, data):201        # Once we've actually found a bug, there's no point in trying to run202        # health checks - they'll just mask the actually important information.203        if data.status == Status.INTERESTING:204            self.health_check_state = None205        state = self.health_check_state206        if state is None:207            return208        state.draw_times.extend(data.draw_times)209        if data.status == Status.VALID:210            state.valid_examples += 1211        elif data.status == Status.INVALID:212            state.invalid_examples += 1213        else:214            assert data.status == Status.OVERRUN215            state.overrun_examples += 1216        max_valid_draws = 10217        max_invalid_draws = 50218        max_overrun_draws = 20219        assert state.valid_examples <= max_valid_draws220        if state.valid_examples == max_valid_draws:221            self.health_check_state = None222            return223        if state.overrun_examples == max_overrun_draws:224            fail_health_check(225                self.settings,226                (227                    "Examples routinely exceeded the max allowable size. "228                    "(%d examples overran while generating %d valid ones)"229                    ". Generating examples this large will usually lead to"230                    " bad results. You could try setting max_size parameters "231                    "on your collections and turning "232                    "max_leaves down on recursive() calls."233                )234                % (state.overrun_examples, state.valid_examples),235                HealthCheck.data_too_large,236            )237        if state.invalid_examples == max_invalid_draws:238            fail_health_check(239                self.settings,240                (241                    "It looks like your strategy is filtering out a lot "242                    "of data. Health check found %d filtered examples but "243                    "only %d good ones. This will make your tests much "244                    "slower, and also will probably distort the data "245                    "generation quite a lot. You should adapt your "246                    "strategy to filter less. This can also be caused by "247                    "a low max_leaves parameter in recursive() calls"248                )249                % (state.invalid_examples, state.valid_examples),250                HealthCheck.filter_too_much,251            )252        draw_time = sum(state.draw_times)253        if draw_time > 1.0:254            fail_health_check(255                self.settings,256                (257                    "Data generation is extremely slow: Only produced "258                    "%d valid examples in %.2f seconds (%d invalid ones "259                    "and %d exceeded maximum size). Try decreasing "260                    "size of the data you're generating (with e.g."261                    "max_size or max_leaves parameters)."262                )263                % (264                    state.valid_examples,265                    draw_time,266                    state.invalid_examples,267                    state.overrun_examples,268                ),269                HealthCheck.too_slow,270            )271    def save_buffer(self, buffer):272        if self.settings.database is not None:273            key = self.database_key274            if key is None:275                return276            self.settings.database.save(key, hbytes(buffer))277    def downgrade_buffer(self, buffer):278        if self.settings.database is not None and self.database_key is not None:279            self.settings.database.move(self.database_key, self.secondary_key, buffer)280    @property281    def secondary_key(self):282        return b".".join((self.database_key, b"secondary"))283    @property284    def covering_key(self):285        return b".".join((self.database_key, b"coverage"))286    def note_details(self, data):287        self.__data_cache[data.buffer] = data.as_result()288        runtime = max(data.finish_time - data.start_time, 0.0)289        self.all_runtimes.append(runtime)290        self.all_drawtimes.extend(data.draw_times)291        self.status_runtimes.setdefault(data.status, []).append(runtime)...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
