Skip to content
Open
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
2 changes: 2 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,8 @@ Changelog
X.Y.Z
-----

- Keep thread timeouts effective and report diagnostics to stderr when the
terminal plugin is disabled.
- Preserve capture-fixture output and disable fixture capturing when reporting
thread timeouts. Thanks Arham Begani.
- Add support Python3.15. Thanks Vladimir Roshchin.
Expand Down
10 changes: 8 additions & 2 deletions pytest_timeout.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from contextlib import ExitStack

import pytest
from _pytest._io import TerminalWriter

__all__ = ("Settings", "is_debugging")
SESSION_TIMEOUT_KEY = pytest.StashKey[float]()
Expand Down Expand Up @@ -508,7 +509,7 @@ def timeout_timer(item, settings):
"""
if not settings.disable_debugger_detection and is_debugging():
return
terminal = item.config.get_terminal_writer()
terminal = None
with ExitStack() as stack:
try:
capman = item.config.pluginmanager.getplugin("capturemanager")
Expand All @@ -520,6 +521,10 @@ def timeout_timer(item, settings):
stdout, stderr = capman.read_global_capture()
else:
stdout, stderr = None, None
if item.config.pluginmanager.hasplugin("terminalreporter"):
terminal = item.config.get_terminal_writer()
else:
terminal = TerminalWriter(file=sys.stderr)
terminal.sep("+", title="Timeout")
caplog = item.config.pluginmanager.getplugin("_capturelog")
if caplog and hasattr(item, "capturelog_handler"):
Expand All @@ -538,7 +543,8 @@ def timeout_timer(item, settings):
except Exception:
traceback.print_exc()
finally:
terminal.flush()
if terminal is not None:
terminal.flush()
sys.stdout.flush()
sys.stderr.flush()
os._exit(1)
Expand Down
43 changes: 43 additions & 0 deletions test_pytest_timeout.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,49 @@ def test_foo():
result.stdout.fnmatch_lines(["*++ Timeout ++*", "*~~ Stack of MainThread* ~~*"])


@pytest.mark.parametrize(
"capture_args",
[
["--capture=fd"],
["--capture=sys"],
["--capture=tee-sys"],
["--capture=no"],
["-p", "no:capture"],
],
ids=["fd", "sys", "tee-sys", "no", "no-capture-plugin"],
)
def test_thread_without_terminal_plugin(pytester, capture_args):
pytester.makepyfile(
"""
import sys
import time

def test_foo():
print("BEFORE TIMEOUT STDOUT", flush=True)
print("BEFORE TIMEOUT STDERR", file=sys.stderr, flush=True)
time.sleep(2)
print("TEST COMPLETED", flush=True)
"""
)

result = pytester.runpytest_subprocess(
"--timeout=1",
"--timeout-method=thread",
"-p",
"no:terminal",
*capture_args,
timeout=20,
)

assert result.ret == 1
output = result.stdout.lines + result.stderr.lines
assert any(line.endswith("BEFORE TIMEOUT STDOUT") for line in output)
assert any(line.endswith("BEFORE TIMEOUT STDERR") for line in output)
assert "TEST COMPLETED" not in output
result.stderr.fnmatch_lines(["*++ Timeout ++*", "*~~ Stack of MainThread* ~~*"])
assert "++ Timeout ++" in result.stderr.lines[-1]


def test_cov(pytester):
# This test requires pytest-cov
pytest.importorskip("pytest_cov")
Expand Down
Loading