source: sasview/src/sas/qtgui/Utilities/UnitTesting/SasviewLoggerTest.py @ a7067ef2

Last change on this file since a7067ef2 was 464cd07, checked in by Piotr Rozyczko <rozyczko@…>, 7 years ago

Use singleton QApplication in unit tests to avoid issues on Ubuntu. SASVIEW-485

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