@@ -213,26 +213,6 @@ def stopForInteraction(self):
213
213
stop = stopForInteraction
214
214
215
215
216
- @contextmanager
217
- def _capture_exceptions (self ):
218
- """
219
- Context manager that captures exceptions that happen insides it context,
220
- and returns them as a list of (type, value, traceback) after the
221
- context ends.
222
- """
223
- result = []
224
-
225
- def hook (type_ , value , tback ):
226
- result .append ((type_ , value , tback ))
227
- sys .__excepthook__ (type_ , value , tback )
228
-
229
- sys .excepthook = hook
230
- try :
231
- yield result
232
- finally :
233
- sys .excepthook = sys .__excepthook__
234
-
235
-
236
216
def pytest_configure (config ):
237
217
"""
238
218
PyTest plugin API. Called before the start of each test session, used
@@ -252,6 +232,40 @@ def exit_qapp():
252
232
config ._cleanup .append (exit_qapp )
253
233
254
234
235
+ @contextmanager
236
+ def capture_exceptions ():
237
+ """
238
+ Context manager that captures exceptions that happen insides its context,
239
+ and returns them as a list of (type, value, traceback) after the
240
+ context ends.
241
+ """
242
+ result = []
243
+
244
+ def hook (type_ , value , tback ):
245
+ result .append ((type_ , value , tback ))
246
+ sys .__excepthook__ (type_ , value , tback )
247
+
248
+ sys .excepthook = hook
249
+ try :
250
+ yield result
251
+ finally :
252
+ sys .excepthook = sys .__excepthook__
253
+
254
+
255
+ def format_captured_exceptions (exceptions ):
256
+ """
257
+ Formats exceptions given as (type, value, traceback) into a string
258
+ suitable to display as a test failure.
259
+ """
260
+ message = 'Qt exceptions in virtual methods:\n '
261
+ message += '_' * 80 + '\n '
262
+ for (exc_type , value , tback ) in exceptions :
263
+ message += '' .join (traceback .format_tb (tback )) + '\n '
264
+ message += '%s: %s\n ' % (exc_type .__name__ , value )
265
+ message += '_' * 80 + '\n '
266
+ return message
267
+
268
+
255
269
@pytest .yield_fixture
256
270
def qtbot (request ):
257
271
"""
@@ -261,17 +275,11 @@ def qtbot(request):
261
275
that they are properly closed after the test ends.
262
276
"""
263
277
result = QtBot (request .config .qt_app_instance )
264
- with result . _capture_exceptions () as exceptions :
278
+ with capture_exceptions () as exceptions :
265
279
yield result
266
280
267
281
if exceptions :
268
- message = 'Qt exceptions in virtual methods:\n '
269
- message += '_' * 80 + '\n '
270
- for (exc_type , value , tback ) in exceptions :
271
- message += '' .join (traceback .format_tb (tback )) + '\n '
272
- message += '%s: %s\n ' % (exc_type .__name__ , value )
273
- message += '_' * 80 + '\n '
274
- pytest .fail (message )
282
+ pytest .fail (format_captured_exceptions (exceptions ))
275
283
276
284
result ._close ()
277
285
0 commit comments