Best Python code snippet using hypothesis
tstl_hypothesis.py
Source:tstl_hypothesis.py
...46 while True:47 data.start_example(CHOICE)48 i = data.draw_bits(n_bits)49 if i >= n:50 data.stop_example(discard=True)51 continue52 data.stop_example()53 return i54FILTERED_ACTIONS = calc_label_from_name("FILTERED ACTIONS")55@click.group()56@click.option('--sut-file')57@click.option('--log-file')58@click.option('--seed')59@click.option('--n-steps')60@click.pass_context61def tstl(ctx, sut_file, log_file, seed, n_steps):62 eng.BUFFER_SIZE = 10 ** 663 eng.MAX_SHRINKS = 10 ** 664 with open(sut_file) as i:65 source = i.read()66 exec_globals = {}67 exec(source, exec_globals)68 sut_class = exec_globals["sut"]69 seed = int(seed)70 n_steps = int(n_steps)71 def run_tstl(data):72 data.extra_information.tstl_steps = getattr(data, 'tstl_steps', [])73 run_checks = False74 count = 075 sut = sut_class()76 sut.restart()77 random.seed(0)78 actions = list(sut.actions())79 actions.sort(key=lambda t: t[0])80 while True:81 # We draw a value which in normal random mode will almost never be82 # zero, but when we are shrinking can be easily turned into a zero.83 data.start_example(STEP_LABEL)84 if count >= n_steps:85 should_continue = data.draw_bits(64, forced=0)86 else:87 should_continue = data.draw_bits(64)88 if not should_continue:89 data.stop_example()90 break91 count_discarded = data.draw_bits(64) == 092 count += 193 if count_discarded:94 data.start_example(CHOICE)95 i = choice(data, len(actions))96 _, guard, _ = actions[i]97 if not guard():98 data.stop_example(discard=False)99 continue100 else:101 data.stop_example(discard=False)102 else:103 for _ in range(3):104 data.start_example(CHOICE)105 i = choice(data, len(actions))106 _, guard, _ = actions[i]107 succeeded = guard()108 data.stop_example(discard=not succeeded)109 if succeeded:110 break111 else:112 data.start_example(FILTERED_ACTIONS)113 valid_actions = [i for i in range(len(actions)) if actions[i][1]()]114 if not valid_actions:115 data.mark_invalid()116 j = choice(data, len(valid_actions))117 i = valid_actions[j]118 data.stop_example(discard=True)119 data.draw_bits(len(actions).bit_length(), forced=i)120 name, guard, action = actions[i]121 assert guard()122 data.stop_example()123 data.extra_information.tstl_steps.append(name)124 failure = None125 try:126 with timeout(5):127 action()128 if run_checks and not sut.check():129 failure = sut.failure()130 except TimeoutExpired:131 data.mark_invalid()132 # FIXME: Sympy specific hack133 except RecursionError:134 data.mark_invalid()135 except StopTest:136 raise...
collections.py
Source:collections.py
...84 while len(result) < self.max_size:85 data.start_example()86 more = cu.biased_coin(data, stopping_value)87 if not more:88 data.stop_example()89 if len(result) < self.min_size:90 continue91 else:92 break93 value = data.draw(self.element_strategy)94 data.stop_example()95 result.append(value)96 else:97 cu.write(data, b'\0')98 return result99 def __repr__(self):100 return (101 'ListStrategy(%r, min_size=%r, average_size=%r, max_size=%r)'102 ) % (103 self.element_strategy, self.min_size, self.average_length,104 self.max_size105 )106class UniqueListStrategy(SearchStrategy):107 def __init__(108 self,109 elements, min_size, max_size, average_size,110 key111 ):112 super(UniqueListStrategy, self).__init__()113 assert min_size <= average_size <= max_size114 self.min_size = min_size115 self.max_size = max_size116 self.average_size = average_size117 self.element_strategy = elements118 self.key = key119 def validate(self):120 self.element_strategy.validate()121 Parameter = namedtuple(122 'Parameter', ('parameter_seed', 'parameter')123 )124 def do_draw(self, data):125 seen = set()126 result = []127 if self.max_size == self.min_size:128 while len(result) < self.max_size:129 v = data.draw(self.element_strategy)130 k = self.key(v)131 if k not in seen:132 result.append(v)133 seen.add(k)134 return result135 stopping_value = 1 - 1.0 / (1 + self.average_size)136 duplicates = 0137 while len(result) < self.max_size:138 data.start_example()139 if len(result) >= self.min_size:140 more = cu.biased_coin(data, stopping_value)141 else:142 more = True143 if not more:144 data.stop_example()145 break146 value = data.draw(self.element_strategy)147 data.stop_example()148 k = self.key(value)149 if k in seen:150 duplicates += 1151 assume(duplicates <= len(result))152 continue153 seen.add(k)154 result.append(value)155 assume(len(result) >= self.min_size)156 return result157class FixedKeysDictStrategy(MappedSearchStrategy):158 """A strategy which produces dicts with a fixed set of keys, given a159 strategy for each of their equivalent values.160 e.g. {'foo' : some_int_strategy} would161 generate dicts with the single key 'foo' mapping to some integer....
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!!