-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpytest_timeouts.py
215 lines (188 loc) · 6.83 KB
/
pytest_timeouts.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
from __future__ import absolute_import
import functools
import signal
import pytest
SETUP_TIMEOUT_HELP = 'test case setup timeout in seconds'
EXECUTION_TIMEOUT_HELP = 'test case execution timeout in seconds'
TEARDOWN_TIMEOUT_HELP = 'test case teardown timeout in seconds'
TIMEOUT_ORDER_HELP = """override order: i - ini, m - mark, o - opt
example: "omi", "imo", "i" - ini only
"""
@staticmethod
def get_markers_old_way(item, name):
return item.get_marker(name=name)
@staticmethod
def get_markers_new_way(item, name):
return item.iter_markers(name=name)
@pytest.hookimpl
def pytest_addoption(parser):
group = parser.getgroup('timeouts')
group.addoption(
'--setup-timeout',
type=float,
help=SETUP_TIMEOUT_HELP,
)
group.addoption(
'--execution-timeout',
type=float,
help=EXECUTION_TIMEOUT_HELP,
)
group.addoption(
'--teardown-timeout',
type=float,
help=TEARDOWN_TIMEOUT_HELP,
)
group.addoption(
'--timeouts-order',
type=str,
help=TIMEOUT_ORDER_HELP,
default='omi'
)
parser.addini('setup_timeout', SETUP_TIMEOUT_HELP)
parser.addini('execution_timeout', SETUP_TIMEOUT_HELP)
parser.addini('teardown_timeout', SETUP_TIMEOUT_HELP)
@pytest.hookimpl
def pytest_configure(config):
assert hasattr(signal, 'SIGALRM')
TimeoutsPlugin.configure()
config.pluginmanager.register(TimeoutsPlugin(config))
class TimeoutsPlugin(object):
def __init__(self, config):
config.addinivalue_line(
'markers',
'execution_timeout(seconds): '
'time out test case after specified time\n'
)
config.addinivalue_line(
'markers',
'setup_timeout(seconds): '
'time out fixture setup after specific time\n'
)
config.addinivalue_line(
'markers',
'teardown_timeout(seconds):'
'time out fixture teardown after specific time\n'
)
self.order = self.fetch_timeout_order(config)
self.timeout = {
'setup_timeout': self.fetch_timeout_from_config(
'setup_timeout', config),
'execution_timeout': self.fetch_timeout_from_config(
'execution_timeout', config),
'teardown_timeout': self.fetch_timeout_from_config(
'teardown_timeout', config),
}
@staticmethod
def parse_timeout(timeout):
timeout = (
0.0 if (timeout is None) or (timeout == '')
else float(timeout)
)
timeout = 0.0 if timeout < 0.0 else timeout
return timeout
@staticmethod
def configure():
ver = [int(v) for v in pytest.__version__.split('.')]
if (ver[0] > 3) or ((ver[0] == 3) and (ver[1] >= 6)):
TimeoutsPlugin.get_markers = get_markers_new_way
else:
TimeoutsPlugin.get_markers = get_markers_old_way
@staticmethod
def fetch_timeout_from_config(timeout_name, config):
timeout_option = config.getvalue(timeout_name)
timeout_ini = config.getini(timeout_name)
return timeout_option, timeout_ini
@staticmethod
def fetch_timeout_order(config):
order = list(config.getvalue('timeouts_order'))
order_set = set(['i', 'm', 'o'])
if len(order) == 0 or len(order) > 3:
raise pytest.UsageError(
'Order should have at least 1 and less then or '
'equal 3 elements'
)
if not set(order).issubset(order_set):
raise pytest.UsageError(
'Incorrect item \'{}\' in timeout order list'.format(
list(set(order).difference(order_set)))
)
return order
def fetch_timeout(self, timeout_name, item):
marker_timeout = (
self.fetch_marker_timeout(item, timeout_name) if item is not None
else None
)
timeout = None
for order_item in self.order:
if order_item == 'o' and self.timeout[timeout_name][0] is not None:
timeout = self.timeout[timeout_name][0]
break
elif order_item == 'm' and marker_timeout is not None:
timeout = marker_timeout
break
elif (order_item == 'i' and
self.timeout[timeout_name][1] != ''):
timeout = self.timeout[timeout_name][1]
break
return self.parse_timeout(timeout)
@pytest.hookimpl(tryfirst=True)
def pytest_report_header(self, config):
timeout_prints = [
'setup timeout: {}s'.format(
self.fetch_timeout('setup_timeout', None)),
'execution timeout: {}s'.format(
self.fetch_timeout('execution_timeout', None)),
'teardown timeout: {}s'.format(
self.fetch_timeout('teardown_timeout', None)),
]
return [', '.join(timeout_prints)]
@pytest.hookimpl
def pytest_enter_pdb(self):
self.cancel_timer()
@pytest.hookimpl(hookwrapper=True)
def pytest_runtest_setup(self, item):
self.setup_timer(self.fetch_timeout('setup_timeout', item))
yield
self.cancel_timer()
@pytest.hookimpl(hookwrapper=True)
def pytest_runtest_call(self, item):
self.setup_timer(self.fetch_timeout('execution_timeout', item))
yield
self.cancel_timer()
@staticmethod
def fetch_marker_timeout(item, name):
def get_fixture_scope(item):
return item._fixtureinfo.name2fixturedefs[
item._fixtureinfo.names_closure[0]][0].scope
markers = TimeoutsPlugin.get_markers(item, name)
if markers:
for marker in markers:
if marker.args:
if len(marker.args) == 2:
if marker.args[1] == get_fixture_scope(item):
return marker.args[0]
else:
continue
else:
return marker.args[0]
else:
raise TypeError('Timeout value is missing')
return None
@pytest.hookimpl(hookwrapper=True)
def pytest_runtest_teardown(self, item):
self.setup_timer(self.fetch_timeout('teardown_timeout', item))
yield
self.cancel_timer()
@staticmethod
def setup_timer(timeout):
handler = functools.partial(TimeoutsPlugin.timeout_handler, timeout)
signal.signal(signal.SIGALRM, handler)
signal.setitimer(signal.ITIMER_REAL, timeout)
@staticmethod
def cancel_timer():
signal.setitimer(signal.ITIMER_REAL, 0)
signal.signal(signal.SIGALRM, signal.SIG_DFL)
@staticmethod
def timeout_handler(timeout, signum, frame):
__tracebackhide__ = True
pytest.fail('Timeout >%ss' % timeout)