Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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']}")
4 changes: 4 additions & 0 deletions test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 22 additions & 0 deletions tests/test_seed.py
Original file line number Diff line number Diff line change
@@ -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)
Loading