Skip to content

Commit c60d463

Browse files
committed
Small refactoring based on feedback from @jdreaver
1 parent 1a15bf8 commit c60d463

File tree

2 files changed

+50
-28
lines changed

2 files changed

+50
-28
lines changed

pytestqt/_tests/test_exceptions.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1+
import textwrap
12
import pytest
3+
import sys
4+
from pytestqt.plugin import capture_exceptions, format_captured_exceptions
25
from pytestqt.qt_compat import QtGui, Qt, QtCore
36

47

@@ -32,3 +35,14 @@ def test_catch_exceptions_in_virtual_methods(qtbot, raise_error):
3235
app.processEvents()
3336

3437

38+
def test_format_captured_exceptions():
39+
try:
40+
raise ValueError('errors were made')
41+
except ValueError:
42+
exceptions = [sys.exc_info()]
43+
44+
obtained_text = format_captured_exceptions(exceptions)
45+
lines = obtained_text.splitlines()
46+
47+
assert 'Qt exceptions in virtual methods:' in lines
48+
assert 'ValueError: errors were made' in lines

pytestqt/plugin.py

Lines changed: 36 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -213,26 +213,6 @@ def stopForInteraction(self):
213213
stop = stopForInteraction
214214

215215

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-
236216
def pytest_configure(config):
237217
"""
238218
PyTest plugin API. Called before the start of each test session, used
@@ -252,6 +232,40 @@ def exit_qapp():
252232
config._cleanup.append(exit_qapp)
253233

254234

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+
255269
@pytest.yield_fixture
256270
def qtbot(request):
257271
"""
@@ -261,17 +275,11 @@ def qtbot(request):
261275
that they are properly closed after the test ends.
262276
"""
263277
result = QtBot(request.config.qt_app_instance)
264-
with result._capture_exceptions() as exceptions:
278+
with capture_exceptions() as exceptions:
265279
yield result
266280

267281
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))
275283

276284
result._close()
277285

0 commit comments

Comments
 (0)