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