Skip to content

Commit 0d1d78d

Browse files
committed
Add STUMPY_SEED test reproducibility (#707)
1 parent bccb485 commit 0d1d78d

3 files changed

Lines changed: 45 additions & 0 deletions

File tree

conftest.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,22 @@
44
# to fix eventual module import errors that can arise, for example when
55
# running tests from inside VS code.
66
# See https://stackoverflow.com/a/34520971
7+
8+
import os
9+
10+
import numpy as np
11+
12+
# Set STUMPY_SEED for reproducible test failures.
13+
# If not already set (e.g., by test.sh), generate a random seed.
14+
# The seed is printed so it can be noted and reused to reproduce failures:
15+
# STUMPY_SEED=12345 pytest tests/test_stump.py
16+
if "STUMPY_SEED" not in os.environ: # pragma: no cover
17+
os.environ["STUMPY_SEED"] = str(np.random.default_rng().integers(2**31))
18+
19+
# Seed the legacy np.random globally for all tests:
20+
np.random.seed(int(os.environ["STUMPY_SEED"]))
21+
22+
23+
def pytest_configure(config):
24+
"""Print STUMPY_SEED so failed tests can be reproduced."""
25+
print(f"\nSTUMPY_SEED={os.environ['STUMPY_SEED']}")

test.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,10 @@ convert_notebooks()
347347
# Main #
348348
###########
349349

350+
# Set STUMPY_SEED for reproducible test failures
351+
export STUMPY_SEED=${STUMPY_SEED:-$RANDOM}
352+
echo "STUMPY_SEED=$STUMPY_SEED"
353+
350354
if [[ $test_mode == "show" ]]; then
351355
echo "Show development/test environment"
352356
show

tests/test_seed.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import os
2+
3+
import numpy as np
4+
import numpy.testing as npt
5+
6+
7+
def test_stumpy_seed_env_var_is_set():
8+
"""conftest.py should always set STUMPY_SEED in the environment."""
9+
assert "STUMPY_SEED" in os.environ
10+
seed_val = os.environ["STUMPY_SEED"]
11+
assert seed_val.isdigit()
12+
assert 0 <= int(seed_val) < 2**31
13+
14+
15+
def test_stumpy_seed_produces_deterministic_rng():
16+
"""Same STUMPY_SEED should produce identical random sequences."""
17+
seed = int(os.environ["STUMPY_SEED"])
18+
np.random.seed(seed)
19+
arr1 = np.random.uniform(-1000, 1000, [64])
20+
np.random.seed(seed)
21+
arr2 = np.random.uniform(-1000, 1000, [64])
22+
npt.assert_array_equal(arr1, arr2)

0 commit comments

Comments
 (0)