Skip to content

Commit ce4e8d0

Browse files
authored
Fixed #1164 Improve Reproducibility Message For Failed Tests (#1167)
* Initial commit * Addressed comments
1 parent ecd44c1 commit ce4e8d0

2 files changed

Lines changed: 60 additions & 36 deletions

File tree

conftest.py

Lines changed: 58 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,33 +5,75 @@
55
# running tests from inside VS code.
66
# See https://stackoverflow.com/a/34520971
77

8-
import json
8+
import os
9+
from importlib.metadata import PackageNotFoundError, version
910

1011
import pytest
1112

1213
from stumpy import rng
1314

1415

16+
def get_specs():
17+
"""
18+
Find and return all package versions
19+
"""
20+
pkgs = [
21+
# Alphabetical Order
22+
"black",
23+
"coverage",
24+
"dask",
25+
"distributed",
26+
"flake8",
27+
"isort",
28+
"numba",
29+
"numpy",
30+
"pandas",
31+
"polars",
32+
"pytest",
33+
"python",
34+
"ray",
35+
"scipy",
36+
]
37+
38+
specs = []
39+
for pkg in pkgs:
40+
try: # pragma: no cover
41+
pkg_version = version(pkg)
42+
specs.append(f"--spec {pkg}={pkg_version}")
43+
except PackageNotFoundError:
44+
pass
45+
46+
return " ".join(specs)
47+
48+
49+
def get_env_vars():
50+
"""
51+
Find and return all environment variables
52+
"""
53+
keys = [
54+
"NUMBA_DISABLE_JIT",
55+
"NUMBA_ENABLE_CUDASIM",
56+
]
57+
58+
env_vars = []
59+
for key in keys:
60+
value = os.getenv(key)
61+
if value is not None:
62+
env_vars.append(f"{key}={value}")
63+
64+
return " ".join(env_vars)
65+
66+
1567
def pytest_configure(config):
1668
"""
1769
Called after command line options have been parsed
1870
and all plugins and initial conftest files been loaded.
1971
"""
20-
state = rng.STATE
21-
state_str = json.dumps(
22-
(
23-
state[0],
24-
state[1].tolist(),
25-
state[2],
26-
state[3],
27-
state[4],
28-
)
29-
)
30-
31-
# Store details of starting random state in case of failure
32-
pytest.STUMPY_MSG = (
33-
f"\n\nSTUMPY_STATE='{state_str}' pixi run tests custom {config.args[0]}"
34-
)
72+
# Store details of starting random seed in case of failure
73+
env_vars = get_env_vars()
74+
specs = get_specs()
75+
pytest.STUMPY_MSG = f"\n\nSTUMPY_SEED={rng.SEED} {env_vars} "
76+
pytest.STUMPY_MSG += f"pixi exec {specs} ./test.sh custom 1 {config.args[0]}"
3577

3678

3779
def pytest_sessionfinish(session, exitstatus):

stumpy/rng.py

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
import json
21
import os
3-
import warnings
42
from contextlib import contextmanager
53

64
import numpy as np
@@ -9,28 +7,12 @@
97
# in order to account for unit testing
108
if os.getenv("STUMPY_SEED") is not None: # pragma: no cover
119
SEED = int(os.getenv("STUMPY_SEED"))
10+
if SEED == 0:
11+
raise ValueError("STUMPY_SEED must be greater than zero!")
1212
else:
1313
SEED = np.random.randint(1, 4_294_967_296, dtype=np.uint32)
1414
RNG = np.random.RandomState(seed=SEED)
1515

16-
if os.getenv("STUMPY_STATE") is not None: # pragma: no cover
17-
if os.getenv("STUMPY_SEED") is not None: # pragma: no cover
18-
warnings.warn("STUMPY_SEED was ignored in lieu of STUMPY_STATE")
19-
state_str = os.getenv("STUMPY_STATE")
20-
state = json.loads(state_str)
21-
STATE = (
22-
state[0],
23-
np.array(state[1], dtype=np.uint32),
24-
state[2],
25-
state[3],
26-
state[4],
27-
)
28-
RNG.set_state(STATE)
29-
else:
30-
STATE = RNG.get_state()
31-
32-
# seed = RNG.get_state()[1][0]
33-
3416

3517
@contextmanager
3618
def fix_seed(seed):

0 commit comments

Comments
 (0)