How to use mock_logging_error method in autotest

Best Python code snippet using autotest_python

test_exceptions.py

Source:test_exceptions.py Github

copy

Full Screen

1# -*- coding: utf-8 -*-2###3# (C) Copyright (2012-2017) Hewlett Packard Enterprise Development LP4#5# Permission is hereby granted, free of charge, to any person obtaining a copy6# of this software and associated documentation files (the "Software"), to deal7# in the Software without restriction, including without limitation the rights8# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell9# copies of the Software, and to permit persons to whom the Software is10# furnished to do so, subject to the following conditions:11#12# The above copyright notice and this permission notice shall be included in13# all copies or substantial portions of the Software.14#15# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR16# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,17# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE18# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER19# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,20# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN21# THE SOFTWARE.22###23import traceback24import unittest25import logging26import mock27import os28import tempfile29import pickle30from hpOneView.exceptions import handle_exceptions31from hpOneView.exceptions import HPOneViewException32from hpOneView.exceptions import HPOneViewInvalidResource33from hpOneView.exceptions import HPOneViewUnknownType34from hpOneView.exceptions import HPOneViewTaskError35from hpOneView.exceptions import HPOneViewResourceNotFound36from hpOneView.exceptions import HPOneViewValueError37class ExceptionsTest(unittest.TestCase):38 def test_exception_constructor_with_string(self):39 exception = HPOneViewException("A message string")40 self.assertEqual(exception.msg, "A message string")41 self.assertEqual(exception.oneview_response, None)42 self.assertEqual(exception.args[0], "A message string")43 self.assertEqual(len(exception.args), 1)44 def test_exception_constructor_with_valid_dict(self):45 exception = HPOneViewException({'message': "A message string"})46 self.assertEqual(exception.msg, "A message string")47 self.assertEqual(exception.oneview_response, {'message': "A message string"})48 self.assertEqual(exception.args[0], "A message string")49 self.assertEqual(exception.args[1], {'message': 'A message string'})50 def test_exception_constructor_with_invalid_dict(self):51 exception = HPOneViewException({'msg': "A message string"})52 self.assertEqual(exception.msg, None)53 self.assertEqual(exception.oneview_response, {'msg': "A message string"})54 self.assertEqual(exception.args[0], None)55 self.assertEqual(exception.args[1], {'msg': "A message string"})56 def test_exception_constructor_with_invalid_type(self):57 exception = HPOneViewException(['List, item 1', "List, item 2: A message string"])58 self.assertEqual(exception.msg, None)59 self.assertEqual(exception.oneview_response, ['List, item 1', "List, item 2: A message string"])60 self.assertEqual(exception.args[0], None)61 self.assertEqual(exception.args[1], ['List, item 1', "List, item 2: A message string"])62 def test_invalid_resource_exception_inheritance(self):63 exception = HPOneViewInvalidResource({'message': "A message string"})64 self.assertIsInstance(exception, HPOneViewException)65 self.assertEqual(exception.msg, "A message string")66 self.assertEqual(exception.oneview_response, {'message': "A message string"})67 self.assertEqual(exception.args[0], "A message string")68 self.assertEqual(exception.args[1], {'message': 'A message string'})69 def test_unknown_type_exception_inheritance_with_string(self):70 exception = HPOneViewUnknownType("A message string")71 self.assertIsInstance(exception, HPOneViewException)72 self.assertEqual(exception.msg, "A message string")73 self.assertEqual(exception.oneview_response, None)74 self.assertEqual(exception.args[0], "A message string")75 self.assertEqual(len(exception.args), 1)76 def test_exception_constructor_with_unicode(self):77 exception = HPOneViewException(u"A message string")78 self.assertEqual(exception.msg, "A message string")79 self.assertEqual(exception.oneview_response, None)80 self.assertEqual(exception.args[0], "A message string")81 self.assertEqual(len(exception.args), 1)82 def test_task_error_constructor_with_string(self):83 exception = HPOneViewTaskError("A message string", 100)84 self.assertIsInstance(exception, HPOneViewException)85 self.assertEqual(exception.msg, "A message string")86 self.assertEqual(exception.oneview_response, None)87 self.assertEqual(exception.args[0], "A message string")88 self.assertEqual(len(exception.args), 1)89 self.assertEqual(exception.error_code, 100)90 def test_oneview_resource_not_found_inheritance(self):91 exception = HPOneViewResourceNotFound("The resource was not found!")92 self.assertIsInstance(exception, HPOneViewException)93 self.assertEqual(exception.msg, "The resource was not found!")94 self.assertEqual(exception.oneview_response, None)95 self.assertEqual(exception.args[0], "The resource was not found!")96 def test_oneview_value_error_inheritance(self):97 exception = HPOneViewValueError("The given data is empty!")98 self.assertIsInstance(exception, HPOneViewException)99 self.assertEqual(exception.msg, "The given data is empty!")100 self.assertEqual(exception.oneview_response, None)101 self.assertEqual(exception.args[0], "The given data is empty!")102 def test_pickle_HPOneViewException_dict(self):103 message = {"msg": "test message"}104 exception = HPOneViewException(message)105 tempf = tempfile.NamedTemporaryFile(delete=False)106 with tempf as f:107 pickle.dump(exception, f)108 with open(tempf.name, 'rb') as f:109 exception = pickle.load(f)110 os.remove(tempf.name)111 self.assertEqual('HPOneViewException', exception.__class__.__name__)112 def test_pickle_HPOneViewException_message(self):113 message = "test message"114 exception = HPOneViewException(message)115 tempf = tempfile.NamedTemporaryFile(delete=False)116 with tempf as f:117 pickle.dump(exception, f)118 with open(tempf.name, 'rb') as f:119 exception = pickle.load(f)120 os.remove(tempf.name)121 self.assertEqual('HPOneViewException', exception.__class__.__name__)122 @mock.patch.object(traceback, 'print_exception')123 @mock.patch.object(logging, 'error')124 def test_should_log_message(self, mock_logging_error, mock_traceback):125 message = "test message"126 exception = HPOneViewException(message)127 traceback_ex = None128 handle_exceptions(exception.__class__, exception, traceback_ex, mock_logging_error)129 log_message = "Uncaught Exception: HPOneViewException with message: test message"130 mock_logging_error.error.assert_called_once_with(log_message)131 @mock.patch.object(traceback, 'print_exception')132 @mock.patch.object(logging, 'error')133 def test_should_print_exception(self, mock_logging_error, mock_traceback):134 message = "test message"135 exception = HPOneViewException(message)136 traceback_ex = None137 handle_exceptions(exception.__class__, exception, traceback_ex, mock_logging_error)138 mock_traceback.assert_called_once_with(exception.__class__, exception, traceback_ex)139 @mock.patch.object(traceback, 'print_exception')140 @mock.patch.object(logging, 'error')141 def test_should_log_oneview_reponse(self, mock_logging_error, mock_traceback):142 message = {"msg": "test message"}143 exception = HPOneViewException(message)144 traceback_ex = None145 handle_exceptions(exception.__class__, exception, traceback_ex, mock_logging_error)146 log_message = "Uncaught Exception: HPOneViewException with message: \n{'msg': 'test message'}"147 mock_logging_error.error.assert_called_once_with(log_message)148 @mock.patch.object(traceback, 'print_exception')149 @mock.patch.object(logging, 'error')150 def test_should_log_python_exception(self, mock_logging_error, mock_traceback):151 message = "test message"152 exception = Exception(message)153 traceback_ex = None154 handle_exceptions(exception.__class__, exception, traceback_ex, mock_logging_error)155 log_message = "Uncaught Exception: Exception with message: test message"...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run autotest automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful