Skip to content

Commit 6d976f2

Browse files
committed
add --fast option to run hypothesis tests faster
1 parent de8665d commit 6d976f2

File tree

6 files changed

+48
-9
lines changed

6 files changed

+48
-9
lines changed

conftest.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import pytest
2+
3+
def pytest_addoption(parser):
4+
parser.addoption(
5+
"--fast", action="store_true", default=False, help="run tests fast"
6+
)

cosmic-ray.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module-path = "src"
33
python-version = ""
44
timeout = 30.0
55
exclude-modules = ['src/ecdsa/_version.py', 'src/ecdsa/test*']
6-
test-command = "pytest -x src/"
6+
test-command = "pytest -x --fast src/"
77

88
[cosmic-ray.execution-engine]
99
name = "local"

src/ecdsa/test_der.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@
77
import unittest2 as unittest
88
except ImportError:
99
import unittest
10+
import sys
1011
from six import b
1112
import hypothesis.strategies as st
12-
from hypothesis import given, example
13+
from hypothesis import given, example, settings
1314
import pytest
1415
from ._compat import str_idx_as_int
1516
from .curves import NIST256p, NIST224p
@@ -389,6 +390,14 @@ def st_oid(draw, max_value=2 ** 512, max_size=50):
389390
return (first, second) + tuple(rest)
390391

391392

393+
HYP_SETTINGS = {}
394+
395+
396+
if "--fast" in sys.argv:
397+
HYP_SETTINGS["max_examples"] = 2
398+
399+
400+
@settings(**HYP_SETTINGS)
392401
@given(st_oid())
393402
def test_oids(ids):
394403
encoded_oid = encode_oid(*ids)

src/ecdsa/test_malformed_sigs.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -145,12 +145,17 @@ def st_fuzzed_sig(draw, keys_and_sigs):
145145
HealthCheck.filter_too_much,
146146
HealthCheck.too_slow,
147147
]
148+
if "--fast" in sys.argv:
149+
params["max_examples"] = 20
148150

149151
slow_params = dict(params)
150-
slow_params["max_examples"] = 10
152+
if "--fast" in sys.argv:
153+
slow_params["max_examples"] = 2
154+
else:
155+
slow_params["max_examples"] = 10
151156

152157

153-
@settings(**params)
158+
@settings(**slow_params)
154159
@given(st_fuzzed_sig(keys_and_sigs))
155160
def test_fuzzed_der_signatures(args):
156161
verifying_key, sig = args
@@ -296,7 +301,7 @@ def st_der():
296301
)
297302

298303

299-
@settings(**params)
304+
@settings(**slow_params)
300305
@given(st.sampled_from(keys_and_sigs), st_der())
301306
def test_random_der_as_signature(params, der):
302307
"""Check if random DER structures are rejected as signature"""
@@ -341,7 +346,7 @@ def test_random_bytes_as_signature(params, der):
341346
"""
342347

343348

344-
@settings(**params)
349+
@settings(**slow_params)
345350
@given(st_fuzzed_sig(keys_and_string_sigs))
346351
def test_fuzzed_string_signatures(params):
347352
verifying_key, sig = params

src/ecdsa/test_numbertheory.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from six import print_
33
from functools import reduce
44
import operator
5+
import sys
56

67
try:
78
import unittest2 as unittest
@@ -202,9 +203,15 @@ def st_comp_no_com_fac(draw):
202203
# the factorization() sometimes takes a long time to finish
203204
HYP_SETTINGS["deadline"] = 5000
204205

206+
if "--fast" in sys.argv:
207+
HYP_SETTINGS["max_examples"] = 20
208+
205209

206210
HYP_SLOW_SETTINGS = dict(HYP_SETTINGS)
207-
HYP_SLOW_SETTINGS["max_examples"] = 10
211+
if "--fast" in sys.argv:
212+
HYP_SLOW_SETTINGS["max_examples"] = 2
213+
else:
214+
HYP_SLOW_SETTINGS["max_examples"] = 20
208215

209216

210217
class TestNumbertheory(unittest.TestCase):
@@ -239,6 +246,7 @@ def test_gcd_with_uncom_factor(self, numbers):
239246
n = gcd(numbers)
240247
assert n == 1
241248

249+
@settings(**HYP_SLOW_SETTINGS)
242250
@given(
243251
st.lists(
244252
st.integers(min_value=1, max_value=2 ** 8192),
@@ -257,6 +265,7 @@ def test_lcm(self):
257265
assert lcm([3, 5 * 3, 7 * 3]) == 3 * 5 * 7
258266
assert lcm(3) == 3
259267

268+
@settings(**HYP_SLOW_SETTINGS)
260269
@given(
261270
st.lists(
262271
st.integers(min_value=1, max_value=2 ** 8192),
@@ -275,7 +284,7 @@ def test_lcm_with_random_numbers(self, numbers):
275284
"meet requirements (like `is_prime()`), the test "
276285
"case times-out on it",
277286
)
278-
@settings(**HYP_SETTINGS)
287+
@settings(**HYP_SLOW_SETTINGS)
279288
@given(st_num_square_prime())
280289
def test_square_root_mod_prime(self, vals):
281290
square, prime = vals
@@ -313,6 +322,7 @@ def test_jacobi(self, mod):
313322
c *= jacobi(a, i[0]) ** i[1]
314323
assert c == jacobi(a, mod)
315324

325+
@settings(**HYP_SETTINGS)
316326
@given(st_two_nums_rel_prime())
317327
def test_inverse_mod(self, nums):
318328
num, mod = nums

src/ecdsa/test_pyecdsa.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,13 @@
99
import shutil
1010
import subprocess
1111
import pytest
12+
import sys
1213
from binascii import hexlify, unhexlify
1314
from hashlib import sha1, sha256, sha384, sha512
1415
import hashlib
1516
from functools import partial
1617

17-
from hypothesis import given
18+
from hypothesis import given, settings
1819
import hypothesis.strategies as st
1920

2021
from six import b, print_, binary_type
@@ -57,6 +58,13 @@ class SubprocessError(Exception):
5758
pass
5859

5960

61+
HYP_SETTINGS = {}
62+
63+
64+
if "--fast" in sys.argv:
65+
HYP_SETTINGS["max_examples"] = 20
66+
67+
6068
def run_openssl(cmd):
6169
OPENSSL = "openssl"
6270
p = subprocess.Popen(
@@ -1335,6 +1343,7 @@ def test_trytryagain(self):
13351343
b("6fa59d73bf0446ae8743cf748fc5ac11d5585a90356417e97155c3bc"),
13361344
)
13371345

1346+
@settings(**HYP_SETTINGS)
13381347
@given(st.integers(min_value=0, max_value=10 ** 200))
13391348
def test_randrange(self, i):
13401349
# util.randrange does not provide long-term stability: we might

0 commit comments

Comments
 (0)