Skip to content

Commit a3bb20c

Browse files
author
Nils Bars
committed
Add suppress_run_tests() to prevent double test execution
When submission_tests scripts are loaded as modules by task.py, any rf.run_tests() call at module level would execute tests during import. Adding a suppression mechanism allows task.py to prevent this, with a deprecation warning nudging authors to remove the manual rf.run_tests() call.
1 parent 410c367 commit a3bb20c

3 files changed

Lines changed: 40 additions & 0 deletions

File tree

ref_utils/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
"add_environment_test",
2626
"add_submission_test",
2727
"run_tests",
28+
"suppress_run_tests",
2829
# Process
2930
"drop_privileges",
3031
"run",
@@ -57,6 +58,7 @@
5758
environment_test,
5859
run_tests,
5960
submission_test,
61+
suppress_run_tests,
6062
)
6163
from .process import (
6264
drop_privileges,

ref_utils/decorator.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
DEFAULT_TASK_NAME = "default"
1313
__registered_tasks: ty.Dict[str, "_Task"] = {}
14+
_suppress_run_tests: bool = False
1415

1516

1617
@dataclass
@@ -124,6 +125,12 @@ def wrapper(*args: str, **kwargs: Any) -> Any:
124125
return _extended_submission_test
125126

126127

128+
def suppress_run_tests(suppress: bool = True) -> None:
129+
"""Suppress run_tests() calls (used by task.py to prevent double execution)."""
130+
global _suppress_run_tests
131+
_suppress_run_tests = suppress
132+
133+
127134
def run_tests(
128135
*,
129136
result_will_be_submitted: bool = False,
@@ -139,6 +146,16 @@ def run_tests(
139146
Returns:
140147
A list of TaskTestResult objects containing the results of each task.
141148
"""
149+
if _suppress_run_tests:
150+
warnings.warn(
151+
"Calling rf.run_tests() at the end of submission_tests is deprecated "
152+
"and will be removed in a future version. "
153+
"Remove the rf.run_tests() call; tests are executed automatically.",
154+
DeprecationWarning,
155+
stacklevel=2,
156+
)
157+
return []
158+
142159
# Set env var so test_result_will_be_submitted() works within tests
143160
if result_will_be_submitted:
144161
os.environ["RESULT_WILL_BE_SUBMITTED"] = "1"

tests/test_decorator.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
extended_submission_test,
1818
run_tests,
1919
submission_test,
20+
suppress_run_tests,
2021
)
2122
from ref_utils.error import RefUtilsError
2223

@@ -394,3 +395,23 @@ def sub_test() -> bool:
394395
assert isinstance(results, list)
395396
assert len(results) == 1
396397
assert isinstance(results[0], TaskTestResult)
398+
399+
def test_suppress_run_tests_returns_empty_with_warning(self) -> None:
400+
"""Test that run_tests() emits a deprecation warning when suppressed."""
401+
402+
@submission_test()
403+
def sub_test() -> bool:
404+
return True
405+
406+
suppress_run_tests(True)
407+
try:
408+
with warnings.catch_warnings(record=True) as w:
409+
warnings.simplefilter("always")
410+
results = run_tests()
411+
412+
assert results == []
413+
assert len(w) == 1
414+
assert issubclass(w[0].category, DeprecationWarning)
415+
assert "deprecated" in str(w[0].message).lower()
416+
finally:
417+
suppress_run_tests(False)

0 commit comments

Comments
 (0)