Best Python code snippet using hypothesis
test_fuzz_one_input.py
Source:test_fuzz_one_input.py
...23 "buffer_type",24 [bytes, bytearray, memoryview, io.BytesIO],25 ids=attrgetter("__name__"),26)27def test_fuzz_one_input(buffer_type):28 db = InMemoryExampleDatabase()29 seen = []30 seeds = []31 # This is a standard `@given` test, which we can also use as a fuzz target.32 # Note that we specify the DB so we can make more precise assertions,33 # and tighten the phases so we can be sure the failing examples come from fuzzing.34 @given(st.text())35 @settings(database=db, phases=[Phase.reuse, Phase.shrink])36 def test(s):37 seen.append(s)38 assert "\0" not in s, repr(s)39 # Before running fuzz_one_input, there's nothing in `db`, and so the test passes40 # (because example generation is disabled by the custom settings)41 test()42 assert len(seen) == 043 # If we run a lot of random bytestrings through fuzz_one_input, we'll eventually44 # find a failing example.45 with pytest.raises(AssertionError):46 for _ in range(1000):47 buf = bytes(random.getrandbits(8) for _ in range(1000))48 seeds.append(buf)49 test.hypothesis.fuzz_one_input(buffer_type(buf))50 # fuzz_one_input returns False for invalid bytestrings, due to e.g. assume(False)51 assert len(seen) <= len(seeds)52 # `db` contains exactly one failing example, which is either the most53 # recent seed that we tried or the pruned-and-canonicalised form of it.54 (saved_examples,) = db.data.values()55 assert len(saved_examples) == 156 assert sort_key(seeds[-1]) >= sort_key(list(saved_examples)[0])57 # Now that we have a failure in `db`, re-running our test is sufficient to58 # reproduce it, *and shrink to a minimal example*.59 with pytest.raises(AssertionError):60 test()61 assert seen[-1] == "\0"62def test_can_fuzz_with_database_eq_None():63 # This test exists to cover the can't-record-failure branch.64 @given(st.none())65 @settings(database=None)66 def test(s):67 assert False68 with pytest.raises(AssertionError):69 test.hypothesis.fuzz_one_input(b"\x00\x00")70def test_fuzzing_unsatisfiable_test_always_returns_None():71 # There are no examples of `st.none().filter(bool)`, but while the Hypothesis72 # engine would give up, fuzz_one_input will just return None each time.73 @given(st.none().filter(bool))74 @settings(database=None)75 def test(s):76 assert False77 for _ in range(100):78 buf = bytes(random.getrandbits(8) for _ in range(3))79 ret = test.hypothesis.fuzz_one_input(buf)80 assert ret is None81def test_autopruning_of_returned_buffer():82 @given(st.binary(min_size=4, max_size=4))83 @settings(database=None)84 def test(s):85 pass86 # Unused portions of the input buffer are discarded from output.87 # (and canonicalised, but that's a no-op for fixed-length `binary()`)88 assert test.hypothesis.fuzz_one_input(b"deadbeef") == b"dead"89STRAT = st.builds(object)90@given(x=STRAT)91def addx(x, y):92 pass93@given(STRAT)94def addy(x, y):95 pass96def test_can_access_strategy_for_wrapped_test():97 assert addx.hypothesis._given_kwargs == {"x": STRAT}98 assert addy.hypothesis._given_kwargs == {"y": STRAT}99@pytest.mark.parametrize(100 "buffers,db_size",101 [102 ([b"aa", b"bb", b"cc", b"dd"], 1), # ascending -> only saves first103 ([b"dd", b"cc", b"bb", b"aa"], 4), # descending -> saves all104 ([b"cc", b"dd", b"aa", b"bb"], 2), # sawtooth -> saves cc then aa105 ([b"aa", b"bb", b"cc", b"XX"], 2), # two distinct errors -> saves both106 ],107)108def test_fuzz_one_input_does_not_add_redundant_entries_to_database(buffers, db_size):109 db = InMemoryExampleDatabase()110 seen = []111 @given(st.binary(min_size=2, max_size=2))112 @settings(database=db)113 def test(s):114 seen.append(s)115 assert s != b"XX"116 raise AssertionError117 for buf in buffers:118 with pytest.raises(AssertionError):119 test.hypothesis.fuzz_one_input(buf)120 (saved_examples,) = db.data.values()121 assert seen == buffers...
fuzz.py
Source:fuzz.py
...25 writer = Writer()26 writer.document = doc27 writer.translate()28 return writer.output29def fuzz_one_input(data: bytes) -> None:30 fdp = atheris.FuzzedDataProvider(data)31 src = fdp.ConsumeUnicodeNoSurrogates(sys.maxsize)32 if "\x00" in src:33 return34 try:35 doc = parse_rst(src)36 except: # noqa: E72237 return38 wrote = write_rst(doc)39 assert wrote is not None40 wrote_publish = publish_string(wrote).decode("utf-8")41 wrote_tree_str = str(parse_rst(wrote))42 src_pubilsh = publish_string(src).decode("utf-8")43 src_tree_str = str(doc)...
ht_fuzz_test.py
Source:ht_fuzz_test.py
1# Fuzz (atheris) + Hypothesis strategies. Seems to combine both fine, but way slower or not adapting enough,2# as it did not find a bug vs using atheris only3from hypothesis import given, strategies as st4import atheris5with atheris.instrument_imports():6 import sys7 import functions8@given(st.text())9def test_not_kirby(s):10 functions.not_kirby(s)11if __name__ == "__main__":12 # see https://github.com/google/oss-fuzz/blob/master/projects/ujson/hypothesis_structured_fuzzer.py13 atheris.Setup(sys.argv, atheris.instrument_func(test_not_kirby.hypothesis.fuzz_one_input))...
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!!