From 84f80b2a1dcb664d58938027926a73ed90460f5a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20H=C3=B6rist?= Date: Sun, 28 May 2023 21:56:00 +0200 Subject: new: Tests: Add decorator to catch all exceptions --- test/lib/util.py | 27 +++++++++++++++++++++++++++ test/unit/test_http.py | 3 ++- 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/test/lib/util.py b/test/lib/util.py index 2bb3b26..18f27e6 100644 --- a/test/lib/util.py +++ b/test/lib/util.py @@ -1,3 +1,4 @@ +import sys import unittest from unittest.mock import Mock @@ -18,3 +19,29 @@ class StanzaHandlerTest(unittest.TestCase): self.dispatcher.reset_parser() self.dispatcher.process_data(STREAM_START) + + +def raise_all_exceptions(func): + # Exceptions which are raised from async callbacks + # in GLib or GTK do not bubble up to the unittest + # This decorator catches all exceptions and raises them + # after the unittest + def func_wrapper(self, *args, **kwargs): + + exceptions = [] + + def on_hook(type_, value, tback): + exceptions.append((type_, value, tback)) + + orig_excepthook = sys.excepthook + sys.excepthook = on_hook + try: + result = func(self) + finally: + sys.excepthook = orig_excepthook + if exceptions: + tp, value, tb = exceptions[0] + raise tp(value).with_traceback(tb) + + return result + return func_wrapper diff --git a/test/unit/test_http.py b/test/unit/test_http.py index fc22624..f0a4a39 100644 --- a/test/unit/test_http.py +++ b/test/unit/test_http.py @@ -9,6 +9,7 @@ from gi.repository import Soup from nbxmpp.const import HTTPRequestError from nbxmpp.http import HTTPSession +from test.lib.util import raise_all_exceptions SMALL_FILE_URL = 'https://gajim.org/downloads/ci/unittest_small_file' # 200 KB @@ -236,6 +237,7 @@ class HTTP(unittest.TestCase): self.assertTrue(request4.is_finished()) self.assertTrue(request4.is_complete()) + @raise_all_exceptions def test_content_overflow(self): mainloop = GLib.MainLoop() @@ -243,7 +245,6 @@ class HTTP(unittest.TestCase): session = HTTPSession() request = session.create_request() - def _on_starting(req) -> None: req._received_size = 100000000000 -- cgit v1.2.3