/
core.py
1415 lines (1225 loc) · 56.5 KB
/
core.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# This file is part of Hypothesis, which may be found at
# https://github.com/HypothesisWorks/hypothesis/
#
# Copyright the Hypothesis Authors.
# Individual contributors are listed in AUTHORS.rst and the git log.
#
# This Source Code Form is subject to the terms of the Mozilla Public License,
# v. 2.0. If a copy of the MPL was not distributed with this file, You can
# obtain one at https://mozilla.org/MPL/2.0/.
"""This module provides the core primitives of Hypothesis, such as given."""
import base64
import contextlib
import datetime
import inspect
import io
import sys
import time
import types
import unittest
import warnings
import zlib
from collections import defaultdict
from io import StringIO
from itertools import chain
from random import Random
from typing import (
TYPE_CHECKING,
Any,
BinaryIO,
Callable,
Coroutine,
Hashable,
List,
Optional,
TypeVar,
Union,
overload,
)
from unittest import TestCase
import attr
from hypothesis import strategies as st
from hypothesis._settings import (
HealthCheck,
Phase,
Verbosity,
local_settings,
settings as Settings,
)
from hypothesis.control import BuildContext
from hypothesis.errors import (
DeadlineExceeded,
DidNotReproduce,
FailedHealthCheck,
Flaky,
Found,
HypothesisDeprecationWarning,
HypothesisWarning,
InvalidArgument,
MultipleFailures,
NoSuchExample,
StopTest,
Unsatisfiable,
UnsatisfiedAssumption,
)
from hypothesis.executors import default_new_style_executor, new_style_executor
from hypothesis.internal.compat import (
PYPY,
bad_django_TestCase,
get_type_hints,
int_from_bytes,
)
from hypothesis.internal.conjecture.data import ConjectureData, Status
from hypothesis.internal.conjecture.engine import ConjectureRunner
from hypothesis.internal.conjecture.shrinker import sort_key
from hypothesis.internal.entropy import deterministic_PRNG
from hypothesis.internal.escalation import (
escalate_hypothesis_internal_error,
format_exception,
get_interesting_origin,
get_trimmed_traceback,
)
from hypothesis.internal.healthcheck import fail_health_check
from hypothesis.internal.reflection import (
convert_positional_arguments,
define_function_signature,
function_digest,
get_pretty_function_description,
getfullargspec_except_self as getfullargspec,
impersonate,
is_mock,
proxies,
repr_call,
)
from hypothesis.internal.scrutineer import Tracer, explanatory_lines
from hypothesis.reporting import (
current_verbosity,
report,
verbose_report,
with_reporter,
)
from hypothesis.statistics import describe_targets, note_statistics
from hypothesis.strategies._internal.collections import TupleStrategy
from hypothesis.strategies._internal.misc import NOTHING
from hypothesis.strategies._internal.strategies import (
Ex,
MappedSearchStrategy,
SearchStrategy,
)
from hypothesis.utils.conventions import infer
from hypothesis.vendor.pretty import RepresentationPrinter
from hypothesis.version import __version__
if sys.version_info >= (3, 10): # pragma: no cover
from types import EllipsisType as InferType
elif TYPE_CHECKING:
from builtins import ellipsis as InferType
else:
InferType = type(Ellipsis)
TestFunc = TypeVar("TestFunc", bound=Callable)
running_under_pytest = False
global_force_seed = None
_hypothesis_global_random = None
@attr.s()
class Example:
args = attr.ib()
kwargs = attr.ib()
def example(*args: Any, **kwargs: Any) -> Callable[[TestFunc], TestFunc]:
"""A decorator which ensures a specific example is always tested."""
if args and kwargs:
raise InvalidArgument(
"Cannot mix positional and keyword arguments for examples"
)
if not (args or kwargs):
raise InvalidArgument("An example must provide at least one argument")
hypothesis_explicit_examples: List[Example] = []
def accept(test):
if not hasattr(test, "hypothesis_explicit_examples"):
test.hypothesis_explicit_examples = hypothesis_explicit_examples
test.hypothesis_explicit_examples.append(Example(tuple(args), kwargs))
return test
accept.hypothesis_explicit_examples = hypothesis_explicit_examples # type: ignore
return accept
def seed(seed: Hashable) -> Callable[[TestFunc], TestFunc]:
"""seed: Start the test execution from a specific seed.
May be any hashable object. No exact meaning for seed is provided
other than that for a fixed seed value Hypothesis will try the same
actions (insofar as it can given external sources of non-
determinism. e.g. timing and hash randomization).
Overrides the derandomize setting, which is designed to enable
deterministic builds rather than reproducing observed failures.
"""
def accept(test):
test._hypothesis_internal_use_seed = seed
current_settings = getattr(test, "_hypothesis_internal_use_settings", None)
test._hypothesis_internal_use_settings = Settings(
current_settings, database=None
)
return test
return accept
def reproduce_failure(version: str, blob: bytes) -> Callable[[TestFunc], TestFunc]:
"""Run the example that corresponds to this data blob in order to reproduce
a failure.
A test with this decorator *always* runs only one example and always fails.
If the provided example does not cause a failure, or is in some way invalid
for this test, then this will fail with a DidNotReproduce error.
This decorator is not intended to be a permanent addition to your test
suite. It's simply some code you can add to ease reproduction of a problem
in the event that you don't have access to the test database. Because of
this, *no* compatibility guarantees are made between different versions of
Hypothesis - its API may change arbitrarily from version to version.
"""
def accept(test):
test._hypothesis_internal_use_reproduce_failure = (version, blob)
return test
return accept
def encode_failure(buffer):
buffer = bytes(buffer)
compressed = zlib.compress(buffer)
if len(compressed) < len(buffer):
buffer = b"\1" + compressed
else:
buffer = b"\0" + buffer
return base64.b64encode(buffer)
def decode_failure(blob):
try:
buffer = base64.b64decode(blob)
except Exception:
raise InvalidArgument(f"Invalid base64 encoded string: {blob!r}") from None
prefix = buffer[:1]
if prefix == b"\0":
return buffer[1:]
elif prefix == b"\1":
try:
return zlib.decompress(buffer[1:])
except zlib.error as err:
raise InvalidArgument(
f"Invalid zlib compression for blob {blob!r}"
) from err
else:
raise InvalidArgument(
f"Could not decode blob {blob!r}: Invalid start byte {prefix!r}"
)
class WithRunner(MappedSearchStrategy):
def __init__(self, base, runner):
assert runner is not None
super().__init__(base)
self.runner = runner
def do_draw(self, data):
data.hypothesis_runner = self.runner
return self.mapped_strategy.do_draw(data)
def __repr__(self):
return f"WithRunner({self.mapped_strategy!r}, runner={self.runner!r})"
def is_invalid_test(test, original_argspec, given_arguments, given_kwargs):
"""Check the arguments to ``@given`` for basic usage constraints.
Most errors are not raised immediately; instead we return a dummy test
function that will raise the appropriate error if it is actually called.
When the user runs a subset of tests (e.g via ``pytest -k``), errors will
only be reported for tests that actually ran.
"""
def invalid(message, *, exc=InvalidArgument):
def wrapped_test(*arguments, **kwargs):
raise exc(message)
wrapped_test.is_hypothesis_test = True
wrapped_test.hypothesis = HypothesisHandle(
inner_test=test,
get_fuzz_target=wrapped_test,
given_kwargs=given_kwargs,
)
return wrapped_test
if not (given_arguments or given_kwargs):
return invalid("given must be called with at least one argument")
if given_arguments and any(
[original_argspec.varargs, original_argspec.varkw, original_argspec.kwonlyargs]
):
return invalid(
"positional arguments to @given are not supported with varargs, "
"varkeywords, or keyword-only arguments"
)
if len(given_arguments) > len(original_argspec.args):
args = tuple(given_arguments)
return invalid(
f"Too many positional arguments for {test.__name__}() were passed to "
f"@given - expected at most {int(len(original_argspec.args))} "
f"arguments, but got {int(len(args))} {args!r}"
)
if infer in given_arguments:
return invalid(
"... was passed as a positional argument to @given, "
"but may only be passed as a keyword argument or as "
"the sole argument of @given"
)
if given_arguments and given_kwargs:
return invalid("cannot mix positional and keyword arguments to @given")
extra_kwargs = [
k
for k in given_kwargs
if k not in original_argspec.args + original_argspec.kwonlyargs
]
if extra_kwargs and not original_argspec.varkw:
arg = extra_kwargs[0]
return invalid(
f"{test.__name__}() got an unexpected keyword argument {arg!r}, "
f"from `{arg}={given_kwargs[arg]!r}` in @given"
)
if original_argspec.defaults or original_argspec.kwonlydefaults:
return invalid("Cannot apply @given to a function with defaults.")
missing = [repr(kw) for kw in original_argspec.kwonlyargs if kw not in given_kwargs]
if missing:
return invalid(
"Missing required kwarg{}: {}".format(
"s" if len(missing) > 1 else "", ", ".join(missing)
)
)
# This case would raise Unsatisfiable *anyway*, but by detecting it here we can
# provide a much more helpful error message for people e.g. using the Ghostwriter.
empty = [
f"{s!r} (arg {idx})" for idx, s in enumerate(given_arguments) if s is NOTHING
] + [f"{name}={s!r}" for name, s in given_kwargs.items() if s is NOTHING]
if empty:
strats = "strategies" if len(empty) > 1 else "strategy"
return invalid(
f"Cannot generate examples from empty {strats}: " + ", ".join(empty),
exc=Unsatisfiable,
)
class ArtificialDataForExample(ConjectureData):
"""Dummy object that pretends to be a ConjectureData object for the purposes of
drawing arguments for @example. Provides just enough of the ConjectureData API
to allow the test to run. Does not support any sort of interactive drawing,
but that's fine because you can't access that when all of your arguments are
provided by @example.
"""
def __init__(self, kwargs):
self.__draws = 0
self.__kwargs = kwargs
super().__init__(max_length=0, prefix=b"", random=None)
def draw_bits(self, n):
raise NotImplementedError("Dummy object should never be asked for bits.")
def draw(self, strategy):
assert self.__draws == 0
self.__draws += 1
# The main strategy for given is always a tuples strategy that returns
# first positional arguments then keyword arguments. When building this
# object already converted all positional arguments to keyword arguments,
# so this is the correct format to return.
return (), self.__kwargs
def execute_explicit_examples(state, wrapped_test, arguments, kwargs):
original_argspec = getfullargspec(state.test)
for example in reversed(getattr(wrapped_test, "hypothesis_explicit_examples", ())):
example_kwargs = dict(original_argspec.kwonlydefaults or {})
if example.args:
if len(example.args) > len(original_argspec.args):
raise InvalidArgument(
"example has too many arguments for test. Expected at most "
f"{len(original_argspec.args)} but got {len(example.args)}"
)
example_kwargs.update(
dict(zip(original_argspec.args[-len(example.args) :], example.args))
)
else:
example_kwargs.update(example.kwargs)
if Phase.explicit not in state.settings.phases:
continue
example_kwargs.update(kwargs)
with local_settings(state.settings):
fragments_reported = []
try:
with with_reporter(fragments_reported.append):
state.execute_once(
ArtificialDataForExample(example_kwargs),
is_final=True,
print_example=True,
)
except UnsatisfiedAssumption:
# Odd though it seems, we deliberately support explicit examples that
# are then rejected by a call to `assume()`. As well as iterative
# development, this is rather useful to replay Hypothesis' part of
# a saved failure when other arguments are supplied by e.g. pytest.
# See https://github.com/HypothesisWorks/hypothesis/issues/2125
pass
except BaseException as err:
# In order to support reporting of multiple failing examples, we yield
# each of the (report text, error) pairs we find back to the top-level
# runner. This also ensures that user-facing stack traces have as few
# frames of Hypothesis internals as possible.
err = err.with_traceback(get_trimmed_traceback())
# One user error - whether misunderstanding or typo - we've seen a few
# times is to pass strategies to @example() where values are expected.
# Checking is easy, and false-positives not much of a problem, so:
if any(
isinstance(arg, SearchStrategy)
for arg in example.args + tuple(example.kwargs.values())
):
new = HypothesisWarning(
"The @example() decorator expects to be passed values, but "
"you passed strategies instead. See https://hypothesis."
"readthedocs.io/en/latest/reproducing.html for details."
)
new.__cause__ = err
err = new
yield (fragments_reported, err)
if state.settings.report_multiple_bugs:
continue
break
finally:
if fragments_reported:
assert fragments_reported[0].startswith("Falsifying example")
fragments_reported[0] = fragments_reported[0].replace(
"Falsifying example", "Falsifying explicit example", 1
)
if fragments_reported:
verbose_report(fragments_reported[0].replace("Falsifying", "Trying", 1))
for f in fragments_reported[1:]:
verbose_report(f)
def get_random_for_wrapped_test(test, wrapped_test):
settings = wrapped_test._hypothesis_internal_use_settings
wrapped_test._hypothesis_internal_use_generated_seed = None
if wrapped_test._hypothesis_internal_use_seed is not None:
return Random(wrapped_test._hypothesis_internal_use_seed)
elif settings.derandomize:
return Random(int_from_bytes(function_digest(test)))
elif global_force_seed is not None:
return Random(global_force_seed)
else:
global _hypothesis_global_random
if _hypothesis_global_random is None:
_hypothesis_global_random = Random()
seed = _hypothesis_global_random.getrandbits(128)
wrapped_test._hypothesis_internal_use_generated_seed = seed
return Random(seed)
def process_arguments_to_given(wrapped_test, arguments, kwargs, given_kwargs, argspec):
selfy = None
arguments, kwargs = convert_positional_arguments(wrapped_test, arguments, kwargs)
# If the test function is a method of some kind, the bound object
# will be the first named argument if there are any, otherwise the
# first vararg (if any).
if argspec.args:
selfy = kwargs.get(argspec.args[0])
elif arguments:
selfy = arguments[0]
# Ensure that we don't mistake mocks for self here.
# This can cause the mock to be used as the test runner.
if is_mock(selfy):
selfy = None
test_runner = new_style_executor(selfy)
arguments = tuple(arguments)
# We use TupleStrategy over tuples() here to avoid polluting
# st.STRATEGY_CACHE with references (see #493), and because this is
# trivial anyway if the fixed_dictionaries strategy is cacheable.
search_strategy = TupleStrategy(
(
st.just(arguments),
st.fixed_dictionaries(given_kwargs).map(lambda args: dict(args, **kwargs)),
)
)
if selfy is not None:
search_strategy = WithRunner(search_strategy, selfy)
search_strategy.validate()
return arguments, kwargs, test_runner, search_strategy
def skip_exceptions_to_reraise():
"""Return a tuple of exceptions meaning 'skip this test', to re-raise.
This is intended to cover most common test runners; if you would
like another to be added please open an issue or pull request adding
it to this function and to tests/cover/test_lazy_import.py
"""
# This is a set because nose may simply re-export unittest.SkipTest
exceptions = set()
# We use this sys.modules trick to avoid importing libraries -
# you can't be an instance of a type from an unimported module!
# This is fast enough that we don't need to cache the result,
# and more importantly it avoids possible side-effects :-)
if "unittest" in sys.modules:
exceptions.add(sys.modules["unittest"].SkipTest)
if "unittest2" in sys.modules:
exceptions.add(sys.modules["unittest2"].SkipTest)
if "nose" in sys.modules:
exceptions.add(sys.modules["nose"].SkipTest)
if "_pytest" in sys.modules:
exceptions.add(sys.modules["_pytest"].outcomes.Skipped)
return tuple(sorted(exceptions, key=str))
def failure_exceptions_to_catch():
"""Return a tuple of exceptions meaning 'this test has failed', to catch.
This is intended to cover most common test runners; if you would
like another to be added please open an issue or pull request.
"""
# While SystemExit and GeneratorExit are instances of BaseException, we also
# expect them to be deterministic - unlike KeyboardInterrupt - and so we treat
# them as standard exceptions, check for flakiness, etc.
# See https://github.com/HypothesisWorks/hypothesis/issues/2223 for details.
exceptions = [Exception, SystemExit, GeneratorExit]
if "_pytest" in sys.modules:
exceptions.append(sys.modules["_pytest"].outcomes.Failed)
return tuple(exceptions)
def new_given_argspec(original_argspec, given_kwargs):
"""Make an updated argspec for the wrapped test."""
new_args = [a for a in original_argspec.args if a not in given_kwargs]
new_kwonlyargs = [a for a in original_argspec.kwonlyargs if a not in given_kwargs]
annots = {
k: v
for k, v in original_argspec.annotations.items()
if k in new_args + new_kwonlyargs
}
annots["return"] = None
return original_argspec._replace(
args=new_args, kwonlyargs=new_kwonlyargs, annotations=annots
)
class StateForActualGivenExecution:
def __init__(
self, test_runner, search_strategy, test, settings, random, wrapped_test
):
self.test_runner = test_runner
self.search_strategy = search_strategy
self.settings = settings
self.last_exception = None
self.falsifying_examples = ()
self.__was_flaky = False
self.random = random
self.__test_runtime = None
self.ever_executed = False
self.is_find = getattr(wrapped_test, "_hypothesis_internal_is_find", False)
self.wrapped_test = wrapped_test
self.test = test
self.print_given_args = getattr(
wrapped_test, "_hypothesis_internal_print_given_args", True
)
self.files_to_propagate = set()
self.failed_normally = False
self.failed_due_to_deadline = False
self.explain_traces = defaultdict(set)
def execute_once(
self, data, print_example=False, is_final=False, expected_failure=None
):
"""Run the test function once, using ``data`` as input.
If the test raises an exception, it will propagate through to the
caller of this method. Depending on its type, this could represent
an ordinary test failure, or a fatal error, or a control exception.
If this method returns normally, the test might have passed, or
it might have placed ``data`` in an unsuccessful state and then
swallowed the corresponding control exception.
"""
self.ever_executed = True
data.is_find = self.is_find
text_repr = None
if self.settings.deadline is None:
test = self.test
else:
@proxies(self.test)
def test(*args, **kwargs):
self.__test_runtime = None
initial_draws = len(data.draw_times)
start = time.perf_counter()
result = self.test(*args, **kwargs)
finish = time.perf_counter()
internal_draw_time = sum(data.draw_times[initial_draws:])
runtime = datetime.timedelta(
seconds=finish - start - internal_draw_time
)
self.__test_runtime = runtime
current_deadline = self.settings.deadline
if not is_final:
current_deadline = (current_deadline // 4) * 5
if runtime >= current_deadline:
raise DeadlineExceeded(runtime, self.settings.deadline)
return result
def run(data):
# Set up dynamic context needed by a single test run.
with local_settings(self.settings):
with deterministic_PRNG():
with BuildContext(data, is_final=is_final):
# Generate all arguments to the test function.
args, kwargs = data.draw(self.search_strategy)
if expected_failure is not None:
nonlocal text_repr
text_repr = repr_call(test, args, kwargs)
if print_example or current_verbosity() >= Verbosity.verbose:
output = StringIO()
printer = RepresentationPrinter(output)
if print_example:
printer.text("Falsifying example:")
else:
printer.text("Trying example:")
if self.print_given_args:
printer.text(" ")
printer.text(test.__name__)
with printer.group(indent=4, open="(", close=""):
printer.break_()
for v in args:
printer.pretty(v)
# We add a comma unconditionally because
# generated arguments will always be kwargs,
# so there will always be more to come.
printer.text(",")
printer.breakable()
for i, (k, v) in enumerate(kwargs.items()):
printer.text(k)
printer.text("=")
printer.pretty(v)
printer.text(",")
if i + 1 < len(kwargs):
printer.breakable()
printer.break_()
printer.text(")")
printer.flush()
report(output.getvalue())
return test(*args, **kwargs)
# Run the test function once, via the executor hook.
# In most cases this will delegate straight to `run(data)`.
result = self.test_runner(data, run)
# If a failure was expected, it should have been raised already, so
# instead raise an appropriate diagnostic error.
if expected_failure is not None:
exception, traceback = expected_failure
if (
isinstance(exception, DeadlineExceeded)
and self.__test_runtime is not None
):
report(
"Unreliable test timings! On an initial run, this "
"test took %.2fms, which exceeded the deadline of "
"%.2fms, but on a subsequent run it took %.2f ms, "
"which did not. If you expect this sort of "
"variability in your test timings, consider turning "
"deadlines off for this test by setting deadline=None."
% (
exception.runtime.total_seconds() * 1000,
self.settings.deadline.total_seconds() * 1000,
self.__test_runtime.total_seconds() * 1000,
)
)
else:
report("Failed to reproduce exception. Expected: \n" + traceback)
self.__flaky(
f"Hypothesis {text_repr} produces unreliable results: Falsified"
" on the first call but did not on a subsequent one",
cause=exception,
)
return result
def _execute_once_for_engine(self, data):
"""Wrapper around ``execute_once`` that intercepts test failure
exceptions and single-test control exceptions, and turns them into
appropriate method calls to `data` instead.
This allows the engine to assume that any exception other than
``StopTest`` must be a fatal error, and should stop the entire engine.
"""
try:
trace = frozenset()
if (
self.failed_normally
and not self.failed_due_to_deadline
and Phase.shrink in self.settings.phases
and Phase.explain in self.settings.phases
and sys.gettrace() is None
and not PYPY
): # pragma: no cover
# This is in fact covered by our *non-coverage* tests, but due to the
# settrace() contention *not* by our coverage tests. Ah well.
tracer = Tracer()
try:
sys.settrace(tracer.trace)
result = self.execute_once(data)
if data.status == Status.VALID:
self.explain_traces[None].add(frozenset(tracer.branches))
finally:
sys.settrace(None)
trace = frozenset(tracer.branches)
else:
result = self.execute_once(data)
if result is not None:
fail_health_check(
self.settings,
"Tests run under @given should return None, but "
f"{self.test.__name__} returned {result!r} instead.",
HealthCheck.return_value,
)
except UnsatisfiedAssumption:
# An "assume" check failed, so instead we inform the engine that
# this test run was invalid.
data.mark_invalid()
except StopTest:
# The engine knows how to handle this control exception, so it's
# OK to re-raise it.
raise
except (
HypothesisDeprecationWarning,
FailedHealthCheck,
) + skip_exceptions_to_reraise():
# These are fatal errors or control exceptions that should stop the
# engine, so we re-raise them.
raise
except failure_exceptions_to_catch() as e:
# If the error was raised by Hypothesis-internal code, re-raise it
# as a fatal error instead of treating it as a test failure.
escalate_hypothesis_internal_error()
if data.frozen:
# This can happen if an error occurred in a finally
# block somewhere, suppressing our original StopTest.
# We raise a new one here to resume normal operation.
raise StopTest(data.testcounter) from e
else:
# The test failed by raising an exception, so we inform the
# engine that this test run was interesting. This is the normal
# path for test runs that fail.
tb = get_trimmed_traceback()
info = data.extra_information
info.__expected_traceback = format_exception(e, tb)
info.__expected_exception = e
verbose_report(info.__expected_traceback)
self.failed_normally = True
interesting_origin = get_interesting_origin(e)
if trace: # pragma: no cover
# Trace collection is explicitly disabled under coverage.
self.explain_traces[interesting_origin].add(trace)
if interesting_origin[0] == DeadlineExceeded:
self.failed_due_to_deadline = True
self.explain_traces.clear()
data.mark_interesting(interesting_origin)
def run_engine(self):
"""Run the test function many times, on database input and generated
input, using the Conjecture engine.
"""
# Tell pytest to omit the body of this function from tracebacks
__tracebackhide__ = True
try:
database_key = self.wrapped_test._hypothesis_internal_database_key
except AttributeError:
if global_force_seed is None:
database_key = function_digest(self.test)
else:
database_key = None
runner = ConjectureRunner(
self._execute_once_for_engine,
settings=self.settings,
random=self.random,
database_key=database_key,
)
# Use the Conjecture engine to run the test function many times
# on different inputs.
runner.run()
note_statistics(runner.statistics)
if runner.call_count == 0:
return
if runner.interesting_examples:
self.falsifying_examples = sorted(
runner.interesting_examples.values(),
key=lambda d: sort_key(d.buffer),
reverse=True,
)
else:
if runner.valid_examples == 0:
rep = get_pretty_function_description(self.test)
raise Unsatisfiable(f"Unable to satisfy assumptions of {rep}")
if not self.falsifying_examples:
return
elif not self.settings.report_multiple_bugs:
# Pretend that we only found one failure, by discarding the others.
del self.falsifying_examples[:-1]
# The engine found one or more failures, so we need to reproduce and
# report them.
flaky = 0
if runner.best_observed_targets:
for line in describe_targets(runner.best_observed_targets):
report(line)
report("")
explanations = explanatory_lines(self.explain_traces, self.settings)
for falsifying_example in self.falsifying_examples:
info = falsifying_example.extra_information
ran_example = ConjectureData.for_buffer(falsifying_example.buffer)
self.__was_flaky = False
assert info.__expected_exception is not None
try:
self.execute_once(
ran_example,
print_example=not self.is_find,
is_final=True,
expected_failure=(
info.__expected_exception,
info.__expected_traceback,
),
)
except (UnsatisfiedAssumption, StopTest) as e:
report(format_exception(e, e.__traceback__))
self.__flaky(
"Unreliable assumption: An example which satisfied "
"assumptions on the first run now fails it.",
cause=e,
)
except BaseException as e:
# If we have anything for explain-mode, this is the time to report.
for line in explanations[falsifying_example.interesting_origin]:
report(line)
if len(self.falsifying_examples) <= 1:
# There is only one failure, so we can report it by raising
# it directly.
raise
# We are reporting multiple failures, so we need to manually
# print each exception's stack trace and information.
tb = get_trimmed_traceback()
report(format_exception(e, tb))
finally:
# Whether or not replay actually raised the exception again, we want
# to print the reproduce_failure decorator for the failing example.
if self.settings.print_blob:
report(
"\nYou can reproduce this example by temporarily adding "
"@reproduce_failure(%r, %r) as a decorator on your test case"
% (__version__, encode_failure(falsifying_example.buffer))
)
# Mostly useful for ``find`` and ensuring that objects that
# hold on to a reference to ``data`` know that it's now been
# finished and they can't draw more data from it.
ran_example.freeze()
if self.__was_flaky:
flaky += 1
# If we only have one example then we should have raised an error or
# flaky prior to this point.
assert len(self.falsifying_examples) > 1
if flaky > 0:
raise Flaky(
f"Hypothesis found {len(self.falsifying_examples)} distinct failures, "
f"but {flaky} of them exhibited some sort of flaky behaviour."
)
else:
raise MultipleFailures(
f"Hypothesis found {len(self.falsifying_examples)} distinct failures."
)
def __flaky(self, message, *, cause):
if len(self.falsifying_examples) <= 1:
raise Flaky(message) from cause
else:
self.__was_flaky = True
report("Flaky example! " + message)
@contextlib.contextmanager
def fake_subTest(self, msg=None, **__):
"""Monkeypatch for `unittest.TestCase.subTest` during `@given`.
If we don't patch this out, each failing example is reported as a
separate failing test by the unittest test runner, which is
obviously incorrect. We therefore replace it for the duration with
this version.
"""
warnings.warn(
"subTest per-example reporting interacts badly with Hypothesis "
"trying hundreds of examples, so we disable it for the duration of "
"any test that uses `@given`.",
HypothesisWarning,
stacklevel=2,
)
yield
@attr.s()
class HypothesisHandle:
"""This object is provided as the .hypothesis attribute on @given tests.
Downstream users can reassign its attributes to insert custom logic into
the execution of each case, for example by converting an async into a
sync function.
This must be an attribute of an attribute, because reassignment of a
first-level attribute would not be visible to Hypothesis if the function
had been decorated before the assignment.
See https://github.com/HypothesisWorks/hypothesis/issues/1257 for more
information.
"""
inner_test = attr.ib()
_get_fuzz_target = attr.ib()
_given_kwargs = attr.ib()
@property
def fuzz_one_input(
self,
) -> Callable[[Union[bytes, bytearray, memoryview, BinaryIO]], Optional[bytes]]:
"""Run the test as a fuzz target, driven with the `buffer` of bytes.
Returns None if buffer invalid for the strategy, canonical pruned
bytes if the buffer was valid, and leaves raised exceptions alone.
"""
# Note: most users, if they care about fuzzer performance, will access the
# property and assign it to a local variable to move the attribute lookup
# outside their fuzzing loop / before the fork point. We cache it anyway,
# so that naive or unusual use-cases get the best possible performance too.
try:
return self.__cached_target # type: ignore
except AttributeError:
self.__cached_target = self._get_fuzz_target()
return self.__cached_target
def fullargspec_to_signature(
argspec: inspect.FullArgSpec, *, return_annotation=inspect.Parameter.empty
):
# Construct a new signature based on this argspec. We'll later convert everything
# over to explicit use of signature everywhere, but this is a nice stopgap.
def as_param(name, kind, defaults):
return P(
name,
kind=kind,
default=defaults.get(name, P.empty),
annotation=argspec.annotations.get(name, P.empty),
)
params = []
P = inspect.Parameter
for arg in argspec.args:
defaults = dict(zip(argspec.args[::-1], argspec.defaults[::-1]))
params.append(as_param(arg, P.POSITIONAL_OR_KEYWORD, defaults))
if argspec.varargs:
params.append(as_param(argspec.varargs, P.VAR_POSITIONAL, {}))
for arg in argspec.kwonlyargs:
params.append(as_param(arg, P.KEYWORD_ONLY, argspec.kwonlydefaults))
if argspec.varkw:
params.append(as_param(argspec.varkw, P.VAR_POSITIONAL, {}))
return inspect.Signature(params, return_annotation=return_annotation)