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