本文整理汇总了Python中hypothesis.internal.conjecture.engine.ConjectureRunner类的典型用法代码示例。如果您正苦于以下问题:Python ConjectureRunner类的具体用法?Python ConjectureRunner怎么用?Python ConjectureRunner使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了ConjectureRunner类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_will_shrink_covering_examples
def test_will_shrink_covering_examples():
best = [None]
replaced = []
def tagged(data):
b = hbytes(data.draw_bytes(4))
if any(b):
data.add_tag('nonzero')
if best[0] is None:
best[0] = b
elif b < best[0]:
replaced.append(best[0])
best[0] = b
db = InMemoryExampleDatabase()
runner = ConjectureRunner(tagged, settings=settings(
max_examples=100, max_iterations=10000, max_shrinks=0,
buffer_size=1024,
database=db,
), database_key=b'stuff')
runner.run()
saved = set(all_values(db))
assert best[0] in saved
for r in replaced:
assert r not in saved
示例2: run_benchmark_for_sizes
def run_benchmark_for_sizes(benchmark, n_runs):
click.echo('Calculating data for %s' % (benchmark.name,))
total_sizes = []
with click.progressbar(range(n_runs)) as runs:
for _ in runs:
sizes = []
valid_seed = random.getrandbits(64).to_bytes(8, 'big')
interesting_seed = random.getrandbits(64).to_bytes(8, 'big')
def test_function(data):
try:
try:
value = data.draw(benchmark.strategy)
except UnsatisfiedAssumption:
data.mark_invalid()
if not data.frozen:
if not benchmark.valid(valid_seed, data, value):
data.mark_invalid()
if benchmark.interesting(
interesting_seed, data, value
):
data.mark_interesting()
finally:
sizes.append(len(data.buffer))
engine = ConjectureRunner(
test_function, settings=BENCHMARK_SETTINGS, random=random
)
engine.run()
assert len(sizes) > 0
total_sizes.append(sum(sizes))
return total_sizes
示例3: test_saves_data_while_shrinking
def test_saves_data_while_shrinking():
key = b'hi there'
n = 5
db = ExampleDatabase(':memory:')
assert list(db.fetch(key)) == []
seen = set()
def f(data):
x = data.draw_bytes(512)
if sum(x) >= 5000 and len(seen) < n:
seen.add(hbytes(x))
if hbytes(x) in seen:
data.mark_interesting()
runner = ConjectureRunner(
f, settings=settings(database=db), database_key=key)
runner.run()
assert runner.last_data.status == Status.INTERESTING
assert len(seen) == n
in_db = set(
v
for vs in db.data.values()
for v in vs
)
assert in_db.issubset(seen)
assert in_db == seen
示例4: test_run_nothing
def test_run_nothing():
def f(data):
assert False
runner = ConjectureRunner(f, settings=settings(phases=()))
runner.run()
assert runner.call_count == 0
示例5: test_clears_out_its_database_on_shrinking
def test_clears_out_its_database_on_shrinking(
initial_attempt, skip_target, monkeypatch
):
def generate_new_examples(self):
self.test_function(
ConjectureData.for_buffer(hbytes([initial_attempt])))
monkeypatch.setattr(
ConjectureRunner, 'generate_new_examples', generate_new_examples)
key = b'key'
db = InMemoryExampleDatabase()
def f(data):
if data.draw_bits(8) >= 127:
data.mark_interesting()
runner = ConjectureRunner(
f, settings=settings(database=db, max_examples=256), database_key=key,
random=Random(0),
)
for n in hrange(256):
if n != 127 or not skip_target:
db.save(runner.secondary_key, hbytes([n]))
runner.run()
assert len(runner.interesting_examples) == 1
for b in db.fetch(runner.secondary_key):
assert b[0] >= 127
assert len(list(db.fetch(runner.database_key))) == 1
示例6: run_to_data
def run_to_data(f):
with deterministic_PRNG():
runner = ConjectureRunner(f, settings=TEST_SETTINGS)
runner.run()
assert runner.interesting_examples
last_data, = runner.interesting_examples.values()
return last_data
示例7: test_can_delete_intervals
def test_can_delete_intervals(monkeypatch):
def generate_new_examples(self):
self.test_function(
ConjectureData.for_buffer(hbytes([255] * 10 + [1, 3])))
monkeypatch.setattr(
ConjectureRunner, 'generate_new_examples', generate_new_examples)
monkeypatch.setattr(
Shrinker, 'shrink', fixate(Shrinker.adaptive_example_deletion)
)
def f(data):
while True:
n = data.draw_bits(8)
if n == 255:
continue
elif n == 1:
break
else:
data.mark_invalid()
if data.draw_bits(8) == 3:
data.mark_interesting()
runner = ConjectureRunner(f, settings=settings(database=None))
runner.run()
x, = runner.interesting_examples.values()
assert x.buffer == hbytes([1, 3])
示例8: test_database_uses_values_from_secondary_key
def test_database_uses_values_from_secondary_key():
key = b'key'
database = InMemoryExampleDatabase()
def f(data):
if data.draw_bits(8) >= 5:
data.mark_interesting()
else:
data.mark_invalid()
runner = ConjectureRunner(f, settings=settings(
max_examples=1, buffer_size=1024,
database=database, suppress_health_check=HealthCheck.all(),
), database_key=key)
for i in range(10):
database.save(runner.secondary_key, hbytes([i]))
runner.test_function(ConjectureData.for_buffer(hbytes([10])))
assert runner.interesting_examples
assert len(set(database.fetch(key))) == 1
assert len(set(database.fetch(runner.secondary_key))) == 10
runner.clear_secondary_key()
assert len(set(database.fetch(key))) == 1
assert set(
map(int_from_bytes, database.fetch(runner.secondary_key))
) == set(range(6, 11))
v, = runner.interesting_examples.values()
assert list(v.buffer) == [5]
示例9: test_debug_data
def test_debug_data(capsys):
buf = [0, 1, 2]
def f(data):
for x in hbytes(buf):
if data.draw_bits(8) != x:
data.mark_invalid()
data.start_example(1)
data.stop_example()
data.mark_interesting()
runner = ConjectureRunner(
f,
settings=settings(
max_examples=5000,
buffer_size=1024,
database=None,
suppress_health_check=HealthCheck.all(),
verbosity=Verbosity.debug,
),
)
runner.cached_test_function(buf)
runner.run()
out, _ = capsys.readouterr()
assert re.match(u"\\d+ bytes \\[.*\\] -> ", out)
assert "INTERESTING" in out
示例10: test_terminates_shrinks
def test_terminates_shrinks(n, monkeypatch):
from hypothesis.internal.conjecture import engine
db = InMemoryExampleDatabase()
def generate_new_examples(self):
def draw_bytes(data, n):
return hbytes([255] * n)
self.test_function(self.new_conjecture_data(draw_bytes))
monkeypatch.setattr(
ConjectureRunner, "generate_new_examples", generate_new_examples
)
monkeypatch.setattr(engine, "MAX_SHRINKS", n)
runner = ConjectureRunner(
slow_shrinker(),
settings=settings(max_examples=5000, database=db),
random=Random(0),
database_key=b"key",
)
runner.run()
last_data, = runner.interesting_examples.values()
assert last_data.status == Status.INTERESTING
assert runner.shrinks == n
in_db = set(db.data[runner.secondary_key])
assert len(in_db) == n
示例11: test_database_clears_secondary_key
def test_database_clears_secondary_key():
key = b"key"
database = InMemoryExampleDatabase()
def f(data):
if data.draw_bits(8) == 10:
data.mark_interesting()
else:
data.mark_invalid()
runner = ConjectureRunner(
f,
settings=settings(
max_examples=1,
buffer_size=1024,
database=database,
suppress_health_check=HealthCheck.all(),
),
database_key=key,
)
for i in range(10):
database.save(runner.secondary_key, hbytes([i]))
runner.cached_test_function([10])
assert runner.interesting_examples
assert len(set(database.fetch(key))) == 1
assert len(set(database.fetch(runner.secondary_key))) == 10
runner.clear_secondary_key()
assert len(set(database.fetch(key))) == 1
assert len(set(database.fetch(runner.secondary_key))) == 0
示例12: test_can_cover_without_a_database_key
def test_can_cover_without_a_database_key():
def tagged(data):
data.add_tag(0)
runner = ConjectureRunner(tagged, settings=settings(), database_key=None)
runner.run()
assert len(runner.covering_examples) == 1
示例13: test_saves_data_while_shrinking
def test_saves_data_while_shrinking(monkeypatch):
key = b'hi there'
n = 5
db = InMemoryExampleDatabase()
assert list(db.fetch(key)) == []
seen = set()
monkeypatch.setattr(
ConjectureRunner, 'generate_new_examples',
lambda runner: runner.test_function(
ConjectureData.for_buffer([255] * 10)))
def f(data):
x = data.draw_bytes(10)
if sum(x) >= 2000 and len(seen) < n:
seen.add(hbytes(x))
if hbytes(x) in seen:
data.mark_interesting()
runner = ConjectureRunner(
f, settings=settings(database=db), database_key=key)
runner.run()
assert runner.interesting_examples
assert len(seen) == n
in_db = non_covering_examples(db)
assert in_db.issubset(seen)
assert in_db == seen
示例14: test_exhaust_space
def test_exhaust_space():
with deterministic_PRNG():
runner = ConjectureRunner(
lambda data: data.draw_bits(1), settings=TEST_SETTINGS
)
runner.run()
assert runner.tree.is_exhausted
assert runner.valid_examples == 2
示例15: test_note_events
def test_note_events(event):
def f(data):
data.note_event(event)
data.draw_bytes(1)
runner = ConjectureRunner(f)
runner.run()
assert runner.event_call_counts[str(event)] == runner.call_count > 0