How to use consider_new_test_data method in hypothesis

Best Python code snippet using hypothesis

optimiser.py

Source:optimiser.py Github

copy

Full Screen

...45 return data.target_observations.get(self.target, NO_SCORE)46 @property47 def current_score(self):48 return self.score_function(self.current_data)49 def consider_new_test_data(self, data):50 """Consider a new data object as a candidate target. If it is better51 than the current one, return True."""52 if data.status < Status.VALID:53 return False54 score = self.score_function(data)55 if score < self.current_score:56 return False57 if score > self.current_score:58 self.improvements += 159 self.current_data = data60 return True61 assert score == self.current_score62 # We allow transitions that leave the score unchanged as long as they63 # don't increase the buffer size. This gives us a certain amount of64 # freedom for lateral moves that will take us out of local maxima.65 if len(data.buffer) <= len(self.current_data.buffer):66 self.current_data = data67 return True68 return False69 def hill_climb(self):70 """The main hill climbing loop where we actually do the work: Take71 data, and attempt to improve its score for target. select_example takes72 a data object and returns an index to an example where we should focus73 our efforts."""74 blocks_examined = set()75 prev = None76 i = len(self.current_data.blocks) - 177 while i >= 0 and self.improvements <= self.max_improvements:78 if prev is not self.current_data:79 i = len(self.current_data.blocks) - 180 prev = self.current_data81 if i in blocks_examined:82 i -= 183 continue84 blocks_examined.add(i)85 data = self.current_data86 block = data.blocks[i]87 prefix = data.buffer[: block.start]88 existing = data.buffer[block.start : block.end]89 existing_as_int = int_from_bytes(existing)90 max_int_value = (256 ** len(existing)) - 191 if existing_as_int == max_int_value:92 continue93 def attempt_replace(v):94 """Try replacing the current block in the current best test case95 with an integer of value i. Note that we use the *current*96 best and not the one we started with. This helps ensure that97 if we luck into a good draw when making random choices we get98 to keep the good bits."""99 if v < 0 or v > max_int_value:100 return False101 v_as_bytes = int_to_bytes(v, len(existing))102 # We make a couple attempts at replacement. This only matters103 # if we end up growing the buffer - otherwise we exit the loop104 # early - but in the event that there *is* some randomized105 # component we want to give it a couple of tries to succeed.106 for _ in range(3):107 attempt = self.engine.cached_test_function(108 prefix109 + v_as_bytes110 + self.current_data.buffer[block.end :]111 + bytes(BUFFER_SIZE),112 )113 if self.consider_new_test_data(attempt):114 return True115 if attempt.status < Status.INVALID or len(attempt.buffer) == len(116 self.current_data.buffer117 ):118 return False119 for i, ex in enumerate(self.current_data.examples):120 if ex.start >= block.end:121 break122 if ex.end <= block.start:123 continue124 ex_attempt = attempt.examples[i]125 if ex.length == ex_attempt.length:126 continue127 replacement = attempt.buffer[ex_attempt.start : ex_attempt.end]128 if self.consider_new_test_data(129 self.engine.cached_test_function(130 prefix131 + replacement132 + self.current_data.buffer[ex.end :]133 )134 ):135 return True136 return False137 # We unconditionally scan both upwards and downwards. The reason138 # for this is that we allow "lateral" moves that don't increase the139 # score but instead leave it constant. All else being equal we'd140 # like to leave the test case closer to shrunk, so afterwards we141 # try lowering the value towards zero even if we've just raised it.142 if not attempt_replace(max_int_value):...

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run hypothesis automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful