Is there a way to assert that a Pytest test case has failed due to a pytest timeout? I want to run a longevity test that I expect to run without issue, until a pytest timeout is encountered. I annotate the test with @pytest.mark.timeout(6000)to override the default pytest timeout, and when the 6000 second timeout is encountered, the test fails with E Failed: Timeout >6000.0s.
I've tried adding with pytest.raises(pytest.TimeoutExpired) to my test to catch the eventual timeout, but that doesn't seem to do the trick. Is there a way to properly catch the timeout raised by pytest?
Unfortunately, the pytest-timeout plugin, which provides the @pytest.mark.timeout marker, does not offer the means to catch the timeout (source for reference).
You may find more luck using a library that offers timeout functionality as a context manager, such as the one from Thomas Ahle's answer
import signal
class Timeout:
def __init__(self, seconds=1, error_message='Timeout'):
self.seconds = seconds
self.error_message = error_message
def handle_timeout(self, signum, frame):
raise TimeoutError(self.error_message)
def __enter__(self):
signal.signal(signal.SIGALRM, self.handle_timeout)
signal.alarm(self.seconds)
def __exit__(self, type, value, traceback):
signal.alarm(0)
def test_it_doesnt_succeed():
try:
with Timeout(seconds=6):
do_the_thing()
except TimeoutError:
pass
else:
raise AssertionError('Expected the thing to timeout!')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With