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 @@ -351,6 +351,8 @@ set of tests running in one session. A session all of the tests
that will be run with one invocation of pytest.

A session timeout is set with ``--session-timeout`` and is in seconds.
Elapsed session time uses a monotonic clock, so changes to the system clock
do not affect the timeout.

The following example shows a session timeout of 10 minutes (600 seconds)::

Expand Down
4 changes: 2 additions & 2 deletions pytest_timeout.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ def pytest_configure(config):
if ini:
timeout = _validate_timeout(config.getini("session_timeout"), "config file")
if timeout is not None:
expire_time = time.time() + timeout
expire_time = time.monotonic() + timeout
else:
expire_time = 0
timeout = 0
Expand All @@ -192,7 +192,7 @@ def pytest_runtest_protocol(item):

# check session timeout
expire_time = item.session.config.stash[SESSION_EXPIRE_KEY]
if expire_time and (expire_time < time.time()):
if expire_time and (expire_time < time.monotonic()):
timeout = item.session.config.stash[SESSION_TIMEOUT_KEY]
item.session.shouldfail = f"session-timeout: {timeout} sec exceeded"

Expand Down
40 changes: 40 additions & 0 deletions test_pytest_timeout.py
Original file line number Diff line number Diff line change
Expand Up @@ -708,6 +708,46 @@ def test_two(slow_setup_and_teardown):
result.assert_outcomes(passed=1)


@pytest.mark.parametrize(
("wall_shift", "elapsed", "passed"), [(1000, 1, 2), (-1000, 11, 1)]
)
def test_session_timeout_ignores_wall_clock_changes(
pytester, wall_shift, elapsed, passed
):
pytester.makeconftest(
"""
from types import SimpleNamespace
import pytest
import pytest_timeout

clock = SimpleNamespace(wall=100, elapsed=0)

@pytest.hookimpl(tryfirst=True)
def pytest_configure(config):
pytest_timeout.time = SimpleNamespace(
time=lambda: clock.wall,
monotonic=lambda: clock.elapsed,
)
"""
)
pytester.makepyfile(
f"""
from conftest import clock

def test_one():
clock.wall += {wall_shift}
clock.elapsed += {elapsed}

def test_two():
pass
"""
)
result = pytester.runpytest_subprocess("--session-timeout=10")
result.assert_outcomes(passed=passed)
if passed == 1:
result.stdout.fnmatch_lines(["*session-timeout: 10.0 sec exceeded*"])


def test_ini_session_timeout(pytester):
pytester.makepyfile(
"""
Expand Down
Loading