def test_silence_passed_exception(self): exception = None try: with silence_exception(ValueError): raise ValueError('Testing') except Exception as exc: exception = exc self.assertIsNone(exception)
def test_silences_passed_exception_with_correct_message(self): exception = None exc_message = 'Testing with msg argument.' try: with silence_exception(ValueError, msg=exc_message): raise ValueError(exc_message) except Exception as exc: exception = exc self.assertIsNone(exception)
def test_not_silences_different_exception_with_same_message(self): exc_message = 'Testing with msg argument.' with self.assertRaises(TypeError): with silence_exception(ValueError, msg=exc_message): raise TypeError(exc_message)
def test_not_silences_passed_exception_with_different_message(self): exc_message = 'Testing with msg argument.' with self.assertRaises(ValueError): with silence_exception(ValueError, msg=exc_message): raise ValueError(f'{exc_message} - different.')
def test_not_silences_passed_exception_outside_context_manager(self): with self.assertRaises(ValueError, msg='Testing outside with-block'): with silence_exception(ValueError): raise ValueError('Testing inside with-block') raise ValueError('Testing outside with-block')
def test_when_raised_expected_error(self): with silence_exception(ValueError): # nothing should happen raise ValueError('Test')
def test_not_silences_different_exception_from_passed_one(self): with self.assertRaises(ValueError): with silence_exception(TypeError): raise ValueError('Testing')
def test_when_raised_unexpected_error_with_msg(self): with self.assertRaisesRegex(ValueError, 'Test'): with silence_exception(ValueError, 'Testing.'): # the error should be re-raised since it is not expected. raise ValueError('Test')
def test_with_same_error_should_not_raise(self): with silence_exception(ValueError): # nothing should happen raise ValueError('Test')
def test_with_differents_error_but_same_message_should_raise(self): with self.assertRaises(TypeError): with silence_exception(ValueError, 'Test'): raise TypeError('Test')
def test_with_same_error_but_different_message_should_raise(self): with self.assertRaises(ValueError): with silence_exception(ValueError, 'Testing.'): # the error should be re-raised since it is not expected. raise ValueError('Test')
def test_with_different_errors_should_reraise(self): with self.assertRaises(TypeError): with silence_exception(ValueError): # the error should be re-raised since it is not expected. raise TypeError('Test')