def test_handling_exceptions(self): with self.assertRaises(CustomException) as caught: with exception_handling.handling_exceptions(): with exception_handling.handling_exceptions(): with exception_handling.handling_exceptions(): raise self.raised self.assertIs(caught.exception, self.raised)
def test_handling_exceptions(): value = CustomException() with pytest.raises(CustomException) as caught: with exception_handling.handling_exceptions(): with exception_handling.handling_exceptions(): with exception_handling.handling_exceptions(): raise value assert caught.value is value
def test_handling_exceptions(): value = CustomException() with slash.Session(), pytest.raises(CustomException) as caught: with exception_handling.handling_exceptions() as handled1: with exception_handling.handling_exceptions() as handled2: with exception_handling.handling_exceptions() as handled3: raise value assert caught.value is value assert handled1.exception is value assert handled2.exception is value assert handled3.exception is value
def test_fake_traceback(self): with slash.Session(), pytest.raises(ZeroDivisionError): with exception_handling.handling_exceptions(fake_traceback=False): self._expected_line_number = inspect.currentframe( ).f_lineno + 1 a = 1 / 0 return a with slash.Session(), pytest.raises(ZeroDivisionError): with exception_handling.handling_exceptions(): self._expected_line_number = inspect.currentframe( ).f_lineno + 1 a = 1 / 0 return a
def test_passthrough_types(): value = CustomException() with pytest.raises(CustomException) as caught: with exception_handling.handling_exceptions(passthrough_types=(CustomException,)): raise value assert value is caught.value assert not exception_handling.is_exception_handled(value) with pytest.raises(CustomException) as caught: with exception_handling.handling_exceptions(passthrough_types=(AttributeError,)): raise value assert value is caught.value assert exception_handling.is_exception_handled(value)
def test_reraise_after_exc_info_reset(): @gossip.register('slash.exception_caught_before_debugger') def exception_hook(): # pylint: disable=unused-variable sys.exc_clear() with pytest.raises(CustomException): with exception_handling.handling_exceptions(): raise CustomException()
def test_swallow_types(): value = CustomException() with slash.Session(): with exception_handling.handling_exceptions( swallow_types=(CustomException, )) as handled: raise value assert sys.exc_info() == exception_handling.NO_EXC_INFO assert handled.exception is value
def test_handling_exceptions_skip_test_would_not_return_skip_test(): with pytest.raises(SkipTest) as caught: with slash.Session(): with exception_handling.handling_exceptions() as handled: raise SkipTest('Unittest') assert isinstance(caught.value, SkipTest) assert handled.exception is caught.value assert exception_handling.is_exception_handled(caught.value)
def test_handling_exceptions_inside_assert_raises_with_session(with_session): value = CustomException() with ExitStack() as ctx: if with_session: session = ctx.enter_context(slash.Session()) ctx.enter_context(session.get_started_context()) else: session = None with slash.assert_raises(CustomException): with exception_handling.handling_exceptions(): raise value assert not exception_handling.is_exception_handled(value) if with_session: assert session.results.get_num_errors() == 0
def test_handling_exceptions_inside_assert_raises_with_session(with_session): value = CustomException() with ExitStack() as ctx: if with_session: session = ctx.enter_context(slash.Session()) ctx.enter_context(session.get_started_context()) # https://github.com/PyCQA/pylint/issues/2056: pylint: disable=no-member else: session = None with slash.assert_raises(CustomException): with exception_handling.handling_exceptions(): raise value assert not exception_handling.is_exception_handled(value) if with_session: assert session.results.get_num_errors() == 0
def test_handling_exceptions_inside_allowing_exceptions_with_session( with_session): value = CustomException() with ExitStack() as ctx: if with_session: session = ctx.enter_context(slash.Session()) ctx.enter_context(session.get_started_context()) else: session = None with logbook.TestHandler() as handler: with slash.allowing_exceptions(CustomException): with exception_handling.handling_exceptions(): raise value assert not exception_handling.is_exception_handled(value) if with_session: assert session.results.get_num_errors() == 0 assert len(handler.records) == 0 # pylint: disable=len-as-condition
def test_handling_exceptions_log(context, is_fatal): raised = CustomException() if is_fatal: exception_handling.mark_exception_fatal(raised) with slash.Session(): with logbook.TestHandler() as handler: with exception_handling.handling_exceptions(context=context, swallow=True): raise raised assert len(handler.records) == 3 assert handler.records[1].message.startswith('Error added') assert handler.records[2].message.startswith('Swallowing') handle_exc_msg = handler.records[0].message assert handle_exc_msg.startswith('Handling exception') if context: assert 'Context: {}'.format(context) in handle_exc_msg else: assert 'Context' not in handle_exc_msg if is_fatal: assert 'FATAL' in handle_exc_msg else: assert 'FATAL' not in handle_exc_msg
def _raise_exception_in_context(self, exception_type): with self.assertRaises(exception_type): with exception_handling.handling_exceptions(): raise exception_type()
def __code__(): # pylint: disable=unused-variable from slash.exception_handling import handling_exceptions with handling_exceptions(swallow=True): slash.skip_test() __ut__.events.add('NEVER') # pylint: disable=undefined-variable
def test_swallow_types(): value = CustomException() with exception_handling.handling_exceptions(swallow_types=(CustomException,)): raise value
def test_global_result_error_without_started_context(): with slash.Session() as session: with handling_exceptions(swallow=True): 1 / 0 # pylint: disable=pointless-statement assert not session.results.is_success()
def __code__(): from slash.exception_handling import handling_exceptions with handling_exceptions(swallow=True): slash.skip_test() __ut__.events.add('NEVER')