add "documentary tests" showing exception raising/not-raising without decorator used
@@ -46,6 +46,34 @@ def test_moler_test_raise_exception_when_no_steps_end_for_global_method():
moler_test_raise_exception_when_no_steps_end_for_global_method()
+# connection observer running in background thread may raise exception
+# but such exception is not visible inside MainThread
+# However, in all such cases connection observer stores exception via conn_obs.set_exception()
+
+
+def test_exception_in_observer_is_raised_when_result_is_called_after_set_exception(do_nothing_connection_observer):
+ exc = Exception("some error inside observer")
+
+ def function_using_observer():
+ observer = do_nothing_connection_observer
+ # for real usage observer should be started to run background thread that will set_exception()
+ # but for unit tests we just call it (simulating background thread)
+ observer.set_exception(exc)
+ print(observer.result())
+
+ with pytest.raises(Exception) as err:
+ function_using_observer()
+ assert err.value == exc
+
+
+def test_exception_in_observer_is_ignored_if_no_result_called_nor_decorator_on_function(do_nothing_connection_observer):
+ def function_using_observer():
+ observer = do_nothing_connection_observer
+ observer.set_exception(Exception("some error inside observer"))
+
+ function_using_observer() # should not raise so test should pass
+
+
# --------------------------- resources ---------------------------
@pytest.yield_fixture
@@ -86,3 +114,14 @@ def moler_test():
@MolerTest.moler_raise_background_exceptions_steps_end()
def moler_test_raise_exception_when_no_steps_end_for_global_method():
MolerTest.log("Start global method with log and without steps_end")
+
+
+@pytest.fixture
+def do_nothing_connection_observer():
+ from moler.connection_observer import ConnectionObserver
+
+ class DoNothingObserver(ConnectionObserver):
+ def data_received(self, data): # we need to overwrite it since it is @abstractmethod
+ pass # ignore incoming data
+ observer = DoNothingObserver()
+ return observer
