From 0d1d78dc09277d5ac2c3149449e3665e2f68cb8b Mon Sep 17 00:00:00 2001 From: Vansh Sharma Date: Sun, 19 Jul 2026 20:48:47 +1200 Subject: [PATCH] Add STUMPY_SEED test reproducibility (#707) --- conftest.py | 19 +++++++++++++++++++ test.sh | 4 ++++ tests/test_seed.py | 22 ++++++++++++++++++++++ 3 files changed, 45 insertions(+) create mode 100644 tests/test_seed.py diff --git a/conftest.py b/conftest.py index fe4951163..a2b97f50f 100644 --- a/conftest.py +++ b/conftest.py @@ -4,3 +4,22 @@ # to fix eventual module import errors that can arise, for example when # running tests from inside VS code. # See https://stackoverflow.com/a/34520971 + +import os + +import numpy as np + +# Set STUMPY_SEED for reproducible test failures. +# If not already set (e.g., by test.sh), generate a random seed. +# The seed is printed so it can be noted and reused to reproduce failures: +# STUMPY_SEED=12345 pytest tests/test_stump.py +if "STUMPY_SEED" not in os.environ: # pragma: no cover + os.environ["STUMPY_SEED"] = str(np.random.default_rng().integers(2**31)) + +# Seed the legacy np.random globally for all tests: +np.random.seed(int(os.environ["STUMPY_SEED"])) + + +def pytest_configure(config): + """Print STUMPY_SEED so failed tests can be reproduced.""" + print(f"\nSTUMPY_SEED={os.environ['STUMPY_SEED']}") diff --git a/test.sh b/test.sh index 194288574..0eab86e2d 100755 --- a/test.sh +++ b/test.sh @@ -347,6 +347,10 @@ convert_notebooks() # Main # ########### +# Set STUMPY_SEED for reproducible test failures +export STUMPY_SEED=${STUMPY_SEED:-$RANDOM} +echo "STUMPY_SEED=$STUMPY_SEED" + if [[ $test_mode == "show" ]]; then echo "Show development/test environment" show diff --git a/tests/test_seed.py b/tests/test_seed.py new file mode 100644 index 000000000..42d10beae --- /dev/null +++ b/tests/test_seed.py @@ -0,0 +1,22 @@ +import os + +import numpy as np +import numpy.testing as npt + + +def test_stumpy_seed_env_var_is_set(): + """conftest.py should always set STUMPY_SEED in the environment.""" + assert "STUMPY_SEED" in os.environ + seed_val = os.environ["STUMPY_SEED"] + assert seed_val.isdigit() + assert 0 <= int(seed_val) < 2**31 + + +def test_stumpy_seed_produces_deterministic_rng(): + """Same STUMPY_SEED should produce identical random sequences.""" + seed = int(os.environ["STUMPY_SEED"]) + np.random.seed(seed) + arr1 = np.random.uniform(-1000, 1000, [64]) + np.random.seed(seed) + arr2 = np.random.uniform(-1000, 1000, [64]) + npt.assert_array_equal(arr1, arr2)