1 | import sys |
---|
2 | import unittest |
---|
3 | import logging |
---|
4 | |
---|
5 | from PyQt4.QtGui import * |
---|
6 | from PyQt4.QtCore import * |
---|
7 | |
---|
8 | # Local |
---|
9 | from SasviewLogger import XStream |
---|
10 | from SasviewLogger import QtHandler |
---|
11 | |
---|
12 | app = QApplication(sys.argv) |
---|
13 | |
---|
14 | class SasviewLoggerTest(unittest.TestCase): |
---|
15 | def setUp(self): |
---|
16 | """ |
---|
17 | Prepare the logger |
---|
18 | """ |
---|
19 | self.logger = logging.getLogger(__name__) |
---|
20 | handler = QtHandler() |
---|
21 | handler.setFormatter(logging.Formatter("%(levelname)s: %(message)s")) |
---|
22 | self.logger.addHandler(handler) |
---|
23 | self.logger.setLevel(logging.DEBUG) |
---|
24 | |
---|
25 | self.outHandlerGui=QTextBrowser() |
---|
26 | |
---|
27 | |
---|
28 | def testQtHandler(self): |
---|
29 | """ |
---|
30 | Test redirection of all levels of logging |
---|
31 | """ |
---|
32 | # Attach the listeners |
---|
33 | XStream.stderr().messageWritten.connect( self.outHandlerGui.insertPlainText ) |
---|
34 | XStream.stdout().messageWritten.connect( self.outHandlerGui.insertPlainText ) |
---|
35 | |
---|
36 | # Send the signals |
---|
37 | self.logger.debug('debug message') |
---|
38 | self.logger.info('info message') |
---|
39 | self.logger.warning('warning message') |
---|
40 | self.logger.error('error message') |
---|
41 | sys.stdout.write('with stdout') |
---|
42 | sys.stderr.write('with stderr') |
---|
43 | |
---|
44 | out=self.outHandlerGui.toPlainText() |
---|
45 | |
---|
46 | # Assure everything got logged |
---|
47 | self.assertIn('DEBUG: debug message', out) |
---|
48 | self.assertIn('INFO: info message', out) |
---|
49 | self.assertIn('WARNING: warning message', out) |
---|
50 | self.assertIn('ERROR: error message', out) |
---|
51 | self.assertIn('with stdout', out) |
---|
52 | self.assertIn('with stderr', out) |
---|
53 | |
---|
54 | if __name__ == "__main__": |
---|
55 | unittest.main() |
---|