Skip to content

Commit bbc2229

Browse files
committed
Warn for non-repeatable random tests in a testing environment
1 parent 636e3ff commit bbc2229

2 files changed

Lines changed: 53 additions & 1 deletion

File tree

discretize/tests.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@
2525
assert_isadjoint
2626
""" # NOQA D205
2727

28+
import os
29+
import warnings
30+
2831
import numpy as np
2932
import scipy.sparse as sp
3033

@@ -81,6 +84,24 @@
8184
_happiness_rng = np.random.default_rng()
8285

8386

87+
def _warn_random_test():
88+
in_pytest = "PYTEST_CURRENT_TEST" in os.environ
89+
in_nosetest = any(
90+
x[0].f_globals["__name__"].startswith("nose.") for x in inspect.stack()
91+
)
92+
93+
if in_pytest or in_nosetest:
94+
test = "pytest" if in_pytest else "nosetest"
95+
warnings.warn(
96+
f"You are running a {test} without setting a random seed, the results might not be"
97+
"repeatable. For repeatable tests please pass an argument to `random seed` that is"
98+
"not `None`.",
99+
UserWarning,
100+
stacklevel=3,
101+
)
102+
return in_pytest or in_nosetest
103+
104+
84105
def setup_mesh(mesh_type, nC, nDim, random_seed=None):
85106
"""Generate arbitrary mesh for testing.
86107
@@ -110,6 +131,8 @@ def setup_mesh(mesh_type, nC, nDim, random_seed=None):
110131
A discretize mesh of class specified by the input argument *mesh_type*
111132
"""
112133
if "random" in mesh_type:
134+
if random_seed is None:
135+
_warn_random_test()
113136
rng = np.random.default_rng(random_seed)
114137
if "TensorMesh" in mesh_type:
115138
if "uniform" in mesh_type:
@@ -649,6 +672,8 @@ def check_derivative(
649672
x0 = mkvc(x0)
650673

651674
if dx is None:
675+
if random_seed is None:
676+
_warn_random_test()
652677
rng = np.random.default_rng(random_seed)
653678
dx = rng.standard_normal(len(x0))
654679

@@ -867,6 +892,8 @@ def assert_isadjoint(
867892
"""
868893
__tracebackhide__ = True
869894

895+
if random_seed is None:
896+
_warn_random_test()
870897
rng = np.random.default_rng(random_seed)
871898

872899
def random(size, iscomplex):

tests/base/test_tests.py

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,13 @@
44
import subprocess
55
import numpy as np
66
import scipy.sparse as sp
7-
from discretize.tests import assert_isadjoint, check_derivative, assert_expected_order
7+
from discretize.tests import (
8+
assert_isadjoint,
9+
check_derivative,
10+
assert_expected_order,
11+
_warn_random_test,
12+
setup_mesh,
13+
)
814

915

1016
class TestAssertIsAdjoint:
@@ -166,3 +172,22 @@ def test_import_time():
166172

167173
# Currently we check t < 1.0s.
168174
assert float(out.stderr.decode("utf-8")[:-1]) < 1.0
175+
176+
177+
def test_random_test_warning():
178+
179+
match = r"You are running a pytest without setting a random seed.*"
180+
with pytest.warns(UserWarning, match=match):
181+
_warn_random_test()
182+
183+
def simple_deriv(x):
184+
return np.sin(x), lambda y: np.cos(x) * y
185+
186+
with pytest.warns(UserWarning, match=match):
187+
check_derivative(simple_deriv, np.zeros(10), plotIt=False)
188+
189+
with pytest.warns(UserWarning, match=match):
190+
setup_mesh("randomTensorMesh", 10, 1)
191+
192+
with pytest.warns(UserWarning, match=match):
193+
assert_isadjoint(lambda x: x, lambda x: x, 5, 5)

0 commit comments

Comments
 (0)