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

ESS_GUIESS_GUI_DocsESS_GUI_batch_fittingESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_iss879ESS_GUI_iss959ESS_GUI_openclESS_GUI_orderingESS_GUI_sync_sascalc
Last change on this file since 9909967 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
RevLine 
[0cd8612]1import sys
2import unittest
3import logging
4
5from PyQt4.QtGui import *
6from PyQt4.QtCore import *
7
[31c5b58]8# set up import paths
[83eb5208]9import sas.qtgui.path_prepare
[31c5b58]10
[0cd8612]11# Local
[83eb5208]12from sas.qtgui.Utilities.SasviewLogger import XStream
13from sas.qtgui.Utilities.SasviewLogger import QtHandler
[0cd8612]14
[464cd07]15if not QApplication.instance():
16    app = QApplication(sys.argv)
[0cd8612]17
[a95260d]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)
[0cd8612]57
58if __name__ == "__main__":
59    unittest.main()
Note: See TracBrowser for help on using the repository browser.