Best Python code snippet using hypothesis
dfas.py
Source:dfas.py
...98 assert v == prefix + v_core + suffix, (list(u), list(v))99 better = runner.cached_test_function(u)100 worse = runner.cached_test_function(v)101 allow_discards = worse.has_discards or better.has_discards102 def is_valid_core(s):103 if not (len(u_core) <= len(s) <= len(v_core)):104 return False105 buf = prefix + s + suffix106 result = runner.cached_test_function(buf)107 return (108 predicate(result)109 # Because we're often using this to learn strategies110 # rather than entire complex test functions, it's111 # important that our replacements are precise and112 # don't leave the rest of the test case in a weird113 # state.114 and result.buffer == buf115 # Because the shrinker is good at removing discarded116 # data, unless we need discards to allow one or both117 # of u and v to result in valid shrinks, we don't118 # count attempts that have them as valid. This will119 # cause us to match fewer strings, which will make120 # the resulting shrink pass more efficient when run121 # on test functions it wasn't really intended for.122 and (allow_discards or not result.has_discards)123 )124 assert sort_key(u_core) < sort_key(v_core)125 assert is_valid_core(u_core)126 assert is_valid_core(v_core)127 learner = LStar(is_valid_core)128 prev = -1129 while learner.generation != prev:130 prev = learner.generation131 learner.learn(u_core)132 learner.learn(v_core)133 # L* has a tendency to learn DFAs which wrap around to134 # the beginning. We don't want to it to do that unless135 # it's accurate, so we use these as examples to show136 # check going around the DFA twice.137 learner.learn(u_core * 2)138 learner.learn(v_core * 2)139 if learner.dfa.max_length(learner.dfa.start) > len(v_core):140 # The language we learn is finite and bounded above141 # by the length of v_core. This is important in order142 # to keep our shrink passes reasonably efficient -143 # otherwise they can match far too much. So whenever144 # we learn a DFA that could match a string longer145 # than len(v_core) we fix it by finding the first146 # string longer than v_core and learning that as147 # a correction.148 x = next(learner.dfa.all_matching_strings(min_length=len(v_core) + 1))149 assert not is_valid_core(x)150 learner.learn(x)151 assert not learner.dfa.matches(x)152 assert learner.generation != prev153 else:154 # We mostly care about getting the right answer on the155 # minimal test case, but because we're doing this offline156 # anyway we might as well spend a little more time trying157 # small examples to make sure the learner gets them right.158 for x in islice(learner.dfa.all_matching_strings(), 100):159 if not is_valid_core(x):160 learner.learn(x)161 assert learner.generation != prev162 break163 # We've now successfully learned a DFA that works for shrinking164 # our failed normalisation further. Canonicalise it into a concrete165 # DFA so we can save it for later.166 new_dfa = learner.dfa.canonicalise()167 assert math.isfinite(new_dfa.max_length(new_dfa.start))168 shrinker = runner.new_shrinker(runner.cached_test_function(v), predicate)169 assert (len(prefix), len(v) - len(suffix)) in shrinker.matching_regions(new_dfa)170 name = "tmp-dfa-" + repr(new_dfa)171 shrinker.extra_dfas[name] = new_dfa172 shrinker.fixate_shrink_passes([dfa_replacement(name)])173 assert sort_key(shrinker.buffer) < sort_key(v)...
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!!