Skip to content

Commit c1c6691

Browse files
okkenflub
authored andcommitted
switch to seconds
1 parent 94f948a commit c1c6691

File tree

2 files changed

+21
-20
lines changed

2 files changed

+21
-20
lines changed

pytest_timeout.py

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

2020

2121
__all__ = ("is_debugging", "Settings")
22+
suite_timeout_key = pytest.StashKey[float]()
2223

2324

2425
HAVE_SIGALRM = hasattr(signal, "SIGALRM")
@@ -45,9 +46,9 @@
4546
will be interrupted by the timeout.
4647
""".strip()
4748
SUITE_TIMEOUT_DESC = """
48-
Timeout in minutes for entire suite. Default is None which
49+
Timeout in seconds for entire suite. Default is None which
4950
means no timeout. Timeout is checked between tests, and will not interrupt a test
50-
in progress. Can be specified as a float for partial minutes.
51+
in progress.
5152
""".strip()
5253

5354
# bdb covers pdb, ipdb, and possibly others
@@ -91,7 +92,7 @@ def pytest_addoption(parser):
9192
dest="suite_timeout",
9293
default=None,
9394
type=float,
94-
metavar="minutes",
95+
metavar="SECONDS",
9596
help=SUITE_TIMEOUT_DESC,
9697
)
9798
parser.addini("timeout", TIMEOUT_DESC)
@@ -162,10 +163,12 @@ def pytest_configure(config):
162163
config._env_timeout_func_only = settings.func_only
163164
config._env_timeout_disable_debugger_detection = settings.disable_debugger_detection
164165

165-
_suite_timeout_minutes = config.getoption("--suite-timeout")
166-
if _suite_timeout_minutes:
167-
_suite_expire_time = time.time() + (_suite_timeout_minutes * 60)
168-
166+
timeout = config.getoption("--suite-timeout")
167+
if timeout is not None:
168+
expire_time = time.time() + timeout
169+
else:
170+
expire_time = 0
171+
config.stash[suite_timeout_key] = expire_time
169172

170173

171174
@pytest.hookimpl(hookwrapper=True)
@@ -533,7 +536,10 @@ def dump_stacks(terminal):
533536
terminal.write("".join(traceback.format_stack(frame)))
534537

535538

536-
def pytest_runtest_logfinish(nodeid, location):
537-
if _suite_expire_time and _suite_expire_time < time.time():
538-
pytest.exit(f"suite-timeout: {_suite_timeout_minutes} minutes exceeded",
539-
returncode=0)
539+
def pytest_runtest_makereport(item, call):
540+
session = item.session
541+
config = session.config
542+
expire_time = config.stash[suite_timeout_key]
543+
if expire_time and (expire_time < time.time()):
544+
timeout = config.getoption("--suite-timeout")
545+
session.shouldfail = f"suite-timeout: {timeout} sec exceeded"

test_pytest_timeout.py

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -611,14 +611,9 @@ def test_suite_timeout(pytester):
611611
612612
@pytest.mark.parametrize('i', range(10))
613613
def test_foo(i):
614-
time.sleep(0.1)
614+
time.sleep(1)
615615
"""
616616
)
617-
# each parametrization runs for 0.1 sec
618-
# or about 0.00166 seconds each
619-
# so 0.005 min should be about 3 iterations
620-
result = pytester.runpytest_subprocess("--suite-timeout", "0.005")
621-
result.stdout.fnmatch_lines([
622-
"*= 3 passed * =*",
623-
"*!! * suite-timeout: 0.005 minutes exceeded !!!*"
624-
])
617+
result = pytester.runpytest_subprocess("--suite-timeout", "0.5")
618+
result.stdout.fnmatch_lines(["*!! suite-timeout: 0.5 sec exceeded !!!*"])
619+
result.assert_outcomes(passed=1)

0 commit comments

Comments
 (0)