mirror of
https://github.com/python-telegram-bot/python-telegram-bot.git
synced 2025-02-16 18:31:45 +01:00
Don't fail tests if they've exceeded the timeout
This commit is contained in:
parent
a54770947f
commit
1d0f982e95
3 changed files with 43 additions and 1 deletions
|
@ -13,5 +13,4 @@ pytest==6.2.2
|
||||||
|
|
||||||
flaky
|
flaky
|
||||||
beautifulsoup4
|
beautifulsoup4
|
||||||
pytest-timeout
|
|
||||||
wheel
|
wheel
|
||||||
|
|
|
@ -23,6 +23,8 @@ disable = C0330,R0801,R0913,R0904,R0903,R0902,W0511,C0116,C0115,W0703,R0914,R091
|
||||||
|
|
||||||
[tool:pytest]
|
[tool:pytest]
|
||||||
testpaths = tests
|
testpaths = tests
|
||||||
|
markers =
|
||||||
|
timeout: xfail a test if exceeds the timeout specified.
|
||||||
addopts = --no-success-flaky-report -rsxX
|
addopts = --no-success-flaky-report -rsxX
|
||||||
filterwarnings =
|
filterwarnings =
|
||||||
error
|
error
|
||||||
|
|
|
@ -21,6 +21,7 @@ import functools
|
||||||
import inspect
|
import inspect
|
||||||
import os
|
import os
|
||||||
import re
|
import re
|
||||||
|
import threading
|
||||||
from collections import defaultdict
|
from collections import defaultdict
|
||||||
from queue import Queue
|
from queue import Queue
|
||||||
from threading import Thread, Event
|
from threading import Thread, Event
|
||||||
|
@ -339,6 +340,46 @@ def timezone(tzinfo):
|
||||||
return tzinfo
|
return tzinfo
|
||||||
|
|
||||||
|
|
||||||
|
timed_out = False
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.hookimpl(hookwrapper=True)
|
||||||
|
def pytest_runtest_call(item):
|
||||||
|
marker = item.get_closest_marker("timeout") # Check if function has timeout marker
|
||||||
|
max_time = 10 # Default timeout if timeout arg not specified
|
||||||
|
if marker:
|
||||||
|
max_time = marker.args[0] if marker.args else 10 # Get timeout arg
|
||||||
|
timeout_setup(item, max_time) # Set and start timer
|
||||||
|
yield # This executes the test function and later the code after this is run.
|
||||||
|
if marker:
|
||||||
|
timeout_teardown(item) # Cancel timer if timeout didn't exceed.
|
||||||
|
if timed_out: # xfail test if exceeded!
|
||||||
|
pytest.xfail(reason=f"Timeout of {max_time}s exceeded.")
|
||||||
|
|
||||||
|
|
||||||
|
def timeout_setup(item, max_time):
|
||||||
|
global timed_out
|
||||||
|
|
||||||
|
def xfail_test():
|
||||||
|
global timed_out
|
||||||
|
timed_out = True
|
||||||
|
|
||||||
|
timer = threading.Timer(max_time, xfail_test)
|
||||||
|
|
||||||
|
def cancel_timer():
|
||||||
|
timer.cancel()
|
||||||
|
timer.join()
|
||||||
|
|
||||||
|
item.cancel_timeout = cancel_timer
|
||||||
|
timer.start()
|
||||||
|
|
||||||
|
|
||||||
|
def timeout_teardown(item):
|
||||||
|
cancel = getattr(item, "cancel_timeout", None)
|
||||||
|
if cancel:
|
||||||
|
cancel()
|
||||||
|
|
||||||
|
|
||||||
def expect_bad_request(func, message, reason):
|
def expect_bad_request(func, message, reason):
|
||||||
"""
|
"""
|
||||||
Wrapper for testing bot functions expected to result in an :class:`telegram.error.BadRequest`.
|
Wrapper for testing bot functions expected to result in an :class:`telegram.error.BadRequest`.
|
||||||
|
|
Loading…
Add table
Reference in a new issue