Best Python code snippet using hypothesis
dfas.py
Source:dfas.py  
...56    allow the shrinker to normalise them better. ``u`` and ``v``57    should not currently shrink to the same test case when calling58    this function."""59    from hypothesis.internal.conjecture.shrinker import dfa_replacement, sort_key60    assert predicate(runner.cached_test_function(u))61    assert predicate(runner.cached_test_function(v))62    u_shrunk = fully_shrink(runner, u, predicate)63    v_shrunk = fully_shrink(runner, v, predicate)64    u, v = sorted((u_shrunk.buffer, v_shrunk.buffer), key=sort_key)65    assert u != v66    assert not v.startswith(u)67    # We would like to avoid using LStar on large strings as its68    # behaviour can be quadratic or worse. In order to help achieve69    # this we peel off a common prefix and suffix of the two final70    # results and just learn the internal bit where they differ.71    #72    # This potentially reduces the length quite far if there's73    # just one tricky bit of control flow we're struggling to74    # reduce inside a strategy somewhere and the rest of the75    # test function reduces fine.76    if v.endswith(u):77        prefix = b""78        suffix = u79        u_core = b""80        assert len(u) > 081        v_core = v[: -len(u)]82    else:83        i = 084        while u[i] == v[i]:85            i += 186        prefix = u[:i]87        assert u.startswith(prefix)88        assert v.startswith(prefix)89        i = 190        while u[-i] == v[-i]:91            i += 192        suffix = u[max(len(prefix), len(u) + 1 - i) :]93        assert u.endswith(suffix)94        assert v.endswith(suffix)95        u_core = u[len(prefix) : len(u) - len(suffix)]96        v_core = v[len(prefix) : len(v) - len(suffix)]97    assert u == prefix + u_core + suffix, (list(u), list(v))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)174    return new_dfa175def fully_shrink(runner, test_case, predicate):176    if not isinstance(test_case, ConjectureResult):177        test_case = runner.cached_test_function(test_case)178    while True:179        shrunk = runner.shrink(test_case, predicate)180        if shrunk.buffer == test_case.buffer:181            break182        test_case = shrunk183    return test_case184def normalize(185    base_name,186    test_function,187    *,188    required_successes=100,189    allowed_to_update=False,190    max_dfas=10,191):192    """Attempt to ensure that this test function successfully normalizes - i.e.193    whenever it declares a test case to be interesting, we are able194    to shrink that to the same interesting test case (which logically should195    be the shortlex minimal interesting test case, though we may not be able196    to detect if it is).197    Will run until we have seen ``required_successes`` many interesting test198    cases in a row normalize to the same value.199    If ``allowed_to_update`` is True, whenever we fail to normalize we will200    learn a new DFA-based shrink pass that allows us to make progress. Any201    learned DFAs will be written back into the learned DFA file at the end202    of this function. If ``allowed_to_update`` is False, this will raise an203    error as soon as it encounters a failure to normalize.204    Additionally, if more than ``max_dfas` DFAs are required to normalize205    this test function, this function will raise an error - it's essentially206    designed for small patches that other shrink passes don't cover, and207    if it's learning too many patches then you need a better shrink pass208    than this can provide.209    """210    # Need import inside the function to avoid circular imports211    from hypothesis.internal.conjecture.engine import BUFFER_SIZE, ConjectureRunner212    runner = ConjectureRunner(213        test_function,214        settings=settings(database=None, suppress_health_check=HealthCheck.all()),215        ignore_limits=True,216    )217    seen = set()218    dfas_added = 0219    found_interesting = False220    consecutive_successes = 0221    failures_to_find_interesting = 0222    while consecutive_successes < required_successes:223        attempt = runner.cached_test_function(b"", extend=BUFFER_SIZE)224        if attempt.status < Status.INTERESTING:225            failures_to_find_interesting += 1226            assert (227                found_interesting or failures_to_find_interesting <= 1000228            ), "Test function seems to have no interesting test cases"229            continue230        found_interesting = True231        target = attempt.interesting_origin232        def shrinking_predicate(d):233            return d.status == Status.INTERESTING and d.interesting_origin == target234        if target not in seen:235            seen.add(target)236            runner.shrink(attempt, shrinking_predicate)237            continue...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!!
