-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtest_logging.py
125 lines (101 loc) · 5.38 KB
/
test_logging.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# _ __ _ ___ _ ___ _ _
# | |/ /_ _ __ _| |_ ___ __/ __| __ _| |___ _ __ ___| _ \ |_ _ __ _(_)_ _
# | ' <| '_/ _` | _/ _ (_-<__ \/ _` | / _ \ ' \/ -_) _/ | || / _` | | ' \
# |_|\_\_| \__,_|\__\___/__/___/\__,_|_\___/_|_|_\___|_| |_|\_,_\__, |_|_||_|
# |___/
# License: BSD License ; see LICENSE
#
# Main authors: Philipp Bucher (https://github.com/philbucher)
#
# set up testing environment (before anything else)
import initialize_testing_environment
# python imports
from sys import exc_info, executable
from subprocess import Popen, PIPE
from pathlib import Path
import unittest
from unittest.mock import patch, call, MagicMock
import logging
# plugin imports
from kratos_salome_plugin import IsExecutedInSalome
from kratos_salome_plugin import plugin_logging
# tests imports
from testing_utilities import QtTestCase
# qt imports
from PyQt5.QtCore import QTimer
from PyQt5.QtWidgets import QMessageBox
from PyQt5.QtCore import Qt
from PyQt5.QtTest import QTest
class TestLogging(unittest.TestCase):
@patch('kratos_salome_plugin.plugin_logging.CreateInformativeMessageBox')
def test_exceptions(self, create_msg_box_mock):
"""check if logging exceptions works properly"""
self.assertTrue(hasattr(plugin_logging, 'CreateInformativeMessageBox'), msg="Import failed, seems like pyqt import wasn't working")
with self.assertLogs(level="ERROR") as cm:
try:
1/0
except ZeroDivisionError:
plugin_logging._HandleUnhandledException(*exc_info())
err_name = exc_info()[0].__name__
err_value = str(exc_info()[1])
with self.subTest("Testing exception logs"):
self.assertEqual(len(cm.output), 1)
self.assertIn('ERROR:KRATOS SALOME PLUGIN:Unhandled exception\nTraceback', cm.output[0])
self.assertIn(err_name, cm.output[0])
self.assertIn(err_value, cm.output[0])
# in addition to checking if the exception loggin works also check if
# showing the exception in the message box works
with self.subTest("Testing exception message box"):
# checking if function was called
self.assertEqual(create_msg_box_mock.call_count, 1)
# checking arguments
msg_box_fct_args = create_msg_box_mock.call_args_list[0][0]
self.assertEqual(len(msg_box_fct_args), 4)
self.assertEqual(msg_box_fct_args[0], "An unhandled excepition occured!")
self.assertEqual(msg_box_fct_args[1], "Critical")
self.assertIn("Please report this problem under ", msg_box_fct_args[2])
self.assertIn("Details of the error:\nType: {}\n\nMessage: {}\n\nTraceback:\n".format(err_name, err_value), msg_box_fct_args[3])
@patch('kratos_salome_plugin.plugin_logging.CreateInformativeMessageBox')
def test_MessageBoxLogHandler(self, create_msg_box_mock):
handler = plugin_logging._MessageBoxLogHandler()
record_mock = MagicMock(spec=logging.LogRecord)
attrs = {'getMessage.return_value': "some message"}
record_mock.configure_mock(**attrs)
handler.emit(record_mock)
self.assertEqual(create_msg_box_mock.call_count, 1)
self.assertEqual(len(record_mock.mock_calls), 1)
self.assertEqual(call.getMessage(), record_mock.mock_calls[0])
msg_box_fct_args = create_msg_box_mock.call_args_list[0][0]
self.assertEqual(len(msg_box_fct_args), 4)
self.assertEqual(msg_box_fct_args[0], "Critical event occurred!")
self.assertEqual(msg_box_fct_args[1], "Critical")
self.assertIn("Please report this problem under ", msg_box_fct_args[2])
self.assertEqual(msg_box_fct_args[3], "some message")
def test_excepthook(self):
"""check if the exception hook is working properly
see https://stackoverflow.com/a/46351418
"""
proc = Popen([executable, str(Path('aux_files/excepthook_test.py'))], stdout=PIPE, stderr=PIPE, cwd=str(Path(__file__).parent))
stdout, stderr = proc.communicate()
self.assertEqual(proc.returncode, 1)
self.assertEqual(stdout, b'')
self.assertIn(b'ERROR', stderr)
self.assertIn(b'KRATOS SALOME PLUGIN : Unhandled exception', stderr)
self.assertIn(b'Exception', stderr)
self.assertIn(b'provocing error', stderr)
# make sure exection stops after the exception!
self.assertNotIn(b'RuntimeError', stderr)
self.assertNotIn(b'This should not show up in the log!', stderr)
@unittest.skipUnless(IsExecutedInSalome(), "This test can only be executed inside of salome")
@patch('kratos_salome_plugin.plugin_logging.CreateInformativeMessageBox')
def test_show_critical_in_messagebox_in_salome(self, create_msg_box_mock):
"""test if critical logs are shown in message boxes when running in salome"""
proc = Popen([executable, str(Path('aux_files/critical_in_msg_box_salome.py'))], stdout=PIPE, stderr=PIPE, cwd=str(Path(__file__).parent))
stdout, stderr = proc.communicate()
self.assertEqual(proc.returncode, 0)
self.assertEqual(stdout, b'')
self.assertIn(b'CRITICAL', stderr)
self.assertIn(b'aux_files/critical_in_msg_box_salome.py', stderr)
self.assertIn(b'This is a test message', stderr)
if __name__ == '__main__':
unittest.main()