##// END OF EJS Templates
testing: add hypothesis fuzz testing...
David R. MacIver -
r26842:0f76c64f default
parent child Browse files
Show More
@@ -0,0 +1,62 b''
1 # Helper module to use the Hypothesis tool in tests
2 #
3 # Copyright 2015 David R. MacIver
4 #
5 # For details see http://hypothesis.readthedocs.org
6
7 import os
8 import sys
9 import traceback
10
11 from hypothesis.settings import set_hypothesis_home_dir
12 import hypothesis.strategies as st
13 from hypothesis import given, Settings
14
15 # hypothesis store data regarding generate example and code
16 set_hypothesis_home_dir(os.path.join(
17 os.getenv('TESTTMP'), ".hypothesis"
18 ))
19
20 def check(*args, **kwargs):
21 """decorator to make a function a hypothesis test
22
23 Decorated function are run immediately (to be used doctest style)"""
24 def accept(f):
25 # Workaround for https://github.com/DRMacIver/hypothesis/issues/206
26 # Fixed in version 1.13 (released 2015 october 29th)
27 f.__module__ = '__anon__'
28 try:
29 given(*args, settings=Settings(max_examples=2000), **kwargs)(f)()
30 except Exception:
31 traceback.print_exc(file=sys.stdout)
32 sys.exit(1)
33 return accept
34
35
36 def roundtrips(data, decode, encode):
37 """helper to tests function that must do proper encode/decode roundtripping
38 """
39 @given(data)
40 def testroundtrips(value):
41 encoded = encode(value)
42 decoded = decode(encoded)
43 if decoded != value:
44 raise ValueError(
45 "Round trip failed: %s(%r) -> %s(%r) -> %r" % (
46 encode.__name__, value, decode.__name__, encoded,
47 decoded
48 ))
49 try:
50 testroundtrips()
51 except Exception:
52 # heredoc swallow traceback, we work around it
53 traceback.print_exc(file=sys.stdout)
54 raise
55 print("Round trip OK")
56
57
58 # strategy for generating bytestring that might be an issue for Mercurial
59 bytestrings = (
60 st.builds(lambda s, e: s.encode(e), st.text(), st.sampled_from([
61 'utf-8', 'utf-16',
62 ]))) | st.binary()
@@ -463,3 +463,12 b' def has_pure():'
463 @check("slow", "allow slow tests")
463 @check("slow", "allow slow tests")
464 def has_slow():
464 def has_slow():
465 return os.environ.get('HGTEST_SLOW') == 'slow'
465 return os.environ.get('HGTEST_SLOW') == 'slow'
466
467 @check("hypothesis", "is Hypothesis installed")
468 def has_hypothesis():
469 try:
470 import hypothesis
471 hypothesis.given
472 return True
473 except ImportError:
474 return False
General Comments 0
You need to be logged in to leave comments. Login now