Changeset 9e426c1 in sasview for src/sas/qtgui/UnitTesting
- Timestamp:
- Jun 21, 2016 8:07:33 AM (9 years ago)
- Branches:
- ESS_GUI, ESS_GUI_Docs, ESS_GUI_batch_fitting, ESS_GUI_bumps_abstraction, ESS_GUI_iss1116, ESS_GUI_iss879, ESS_GUI_iss959, ESS_GUI_opencl, ESS_GUI_ordering, ESS_GUI_sync_sascalc
- Children:
- f82ab8c
- Parents:
- 1042dba
- Location:
- src/sas/qtgui/UnitTesting
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
src/sas/qtgui/UnitTesting/DataExplorerTest.py
r1042dba r9e426c1 72 72 loadButton = self.form.cmdLoad 73 73 74 # Mock the system file open method 75 QtGui.QFileDialog.getOpenFileName = MagicMock(return_value=None) 74 filename = "cyl_400_20.txt" 75 # Initialize signal spy instances 76 spy_file_read = QtSignalSpy(self.form, self.form.communicate.fileReadSignal) 77 78 # Return no files. 79 QtGui.QFileDialog.getOpenFileNames = MagicMock(return_value=None) 76 80 77 81 # Click on the Load button … … 79 83 80 84 # Test the getOpenFileName() dialog called once 81 self.assertTrue(QtGui.QFileDialog.getOpenFileName.called) 82 QtGui.QFileDialog.getOpenFileName.assert_called_once() 85 self.assertTrue(QtGui.QFileDialog.getOpenFileNames.called) 86 QtGui.QFileDialog.getOpenFileNames.assert_called_once() 87 88 # Make sure the signal has not been emitted 89 self.assertEqual(spy_file_read.count(), 0) 90 91 # Now, return a single file 92 QtGui.QFileDialog.getOpenFileNames = MagicMock(return_value=filename) 93 94 # Click on the Load button 95 QTest.mouseClick(loadButton, Qt.LeftButton) 96 97 # Test the getOpenFileName() dialog called once 98 self.assertTrue(QtGui.QFileDialog.getOpenFileNames.called) 99 QtGui.QFileDialog.getOpenFileNames.assert_called_once() 100 101 # Expected one spy instance 102 self.assertEqual(spy_file_read.count(), 1) 103 self.assertIn(filename, str(spy_file_read.called()[0]['args'][0])) 83 104 84 105 def testDeleteButton(self): … … 131 152 QTest.mouseClick(deleteButton, Qt.LeftButton) 132 153 133 134 154 def testSendToButton(self): 135 155 """ … … 166 186 # Assure the message box popped up 167 187 QtGui.QMessageBox.assert_called_once() 168 169 188 170 189 def testDataSelection(self): … … 272 291 Test the callback method updating the data object 273 292 """ 274 275 293 message="Loading Data Complete" 276 294 data_dict = {"a1":Data1D()} -
src/sas/qtgui/UnitTesting/GuiManagerTest.py
r5032ea68 r9e426c1 1 1 import sys 2 import subprocess 2 3 import unittest 4 import webbrowser 5 import logging 3 6 4 7 from PyQt4.QtGui import * 5 8 from PyQt4.QtTest import QTest 6 9 from PyQt4.QtCore import * 10 from PyQt4.QtWebKit import * 7 11 from mock import MagicMock 8 12 … … 10 14 from GuiManager import GuiManager 11 15 from UI.MainWindowUI import MainWindow 16 from UnitTesting.TestUtils import QtSignalSpy 12 17 13 18 app = QApplication(sys.argv) … … 56 61 pass 57 62 63 def testQuitApplication(self): 64 """ 65 Test that the custom exit method is called on shutdown 66 """ 67 # Must mask sys.exit, otherwise the whole testing process stops. 68 sys.exit = MagicMock() 69 70 # Say No to the close dialog 71 QMessageBox.question = MagicMock(return_value=QMessageBox.No) 72 73 # Open, then close the manager 74 self.manager.quitApplication() 75 76 # See that the MessageBox method got called 77 self.assertTrue(QMessageBox.question.called) 78 # sys.exit() not called this time 79 self.assertFalse(sys.exit.called) 80 81 # Say Yes to the close dialog 82 QMessageBox.question = MagicMock(return_value=QMessageBox.Yes) 83 84 # Open, then close the manager 85 self.manager.quitApplication() 86 87 # See that the MessageBox method got called 88 self.assertTrue(QMessageBox.question.called) 89 # Also, sys.exit() called 90 self.assertTrue(sys.exit.called) 91 92 def testCheckUpdate(self): 93 """ 94 Tests the SasView website version polling 95 """ 96 self.manager.processVersion = MagicMock() 97 version = {'update_url' : 'http://www.sasview.org/sasview.latestversion', 98 'version' : '3.1.2', 99 'download_url': 'https://github.com/SasView/sasview/releases'} 100 self.manager.checkUpdate() 101 102 self.manager.processVersion.assert_called_with(version) 103 104 pass 105 106 def testProcessVersion(self): 107 """ 108 Tests the version checker logic 109 """ 110 # 1. version = 0.0.0 111 version_info = {u'version' : u'0.0.0'} 112 spy_status_update = QtSignalSpy(self.manager, self.manager.communicate.statusBarUpdateSignal) 113 114 self.manager.processVersion(version_info) 115 116 self.assertEqual(spy_status_update.count(), 1) 117 message = 'Could not connect to the application server. Please try again later.' 118 self.assertIn(message, str(spy_status_update.signal(index=0))) 119 120 # 2. version < LocalConfig.__version__ 121 version_info = {u'version' : u'0.0.1'} 122 spy_status_update = QtSignalSpy(self.manager, self.manager.communicate.statusBarUpdateSignal) 123 124 self.manager.processVersion(version_info) 125 126 self.assertEqual(spy_status_update.count(), 1) 127 message = 'You have the latest version of SasView' 128 self.assertIn(message, str(spy_status_update.signal(index=0))) 129 130 # 3. version > LocalConfig.__version__ 131 version_info = {u'version' : u'999.0.0'} 132 spy_status_update = QtSignalSpy(self.manager, self.manager.communicate.statusBarUpdateSignal) 133 webbrowser.open = MagicMock() 134 135 self.manager.processVersion(version_info) 136 137 self.assertEqual(spy_status_update.count(), 1) 138 message = 'Version 999.0.0 is available!' 139 self.assertIn(message, str(spy_status_update.signal(index=0))) 140 141 webbrowser.open.assert_called_with("https://github.com/SasView/sasview/releases") 142 143 # 4. version > LocalConfig.__version__ and standalone 144 version_info = {u'version' : u'999.0.0'} 145 spy_status_update = QtSignalSpy(self.manager, self.manager.communicate.statusBarUpdateSignal) 146 webbrowser.open = MagicMock() 147 148 self.manager.processVersion(version_info, standalone=True) 149 150 self.assertEqual(spy_status_update.count(), 1) 151 message = 'See the help menu to download it' 152 self.assertIn(message, str(spy_status_update.signal(index=0))) 153 154 self.assertFalse(webbrowser.open.called) 155 156 # 5. couldn't load version 157 version_info = {} 158 logging.error = MagicMock() 159 spy_status_update = QtSignalSpy(self.manager, self.manager.communicate.statusBarUpdateSignal) 160 161 self.manager.processVersion(version_info) 162 163 # Retrieve and compare arguments of the mocked call 164 message = "guiframe: could not get latest application version number" 165 args, _ = logging.error.call_args 166 self.assertIn(message, args[0]) 167 168 # Check the signal message 169 message = 'Could not connect to the application server.' 170 self.assertIn(message, str(spy_status_update.signal(index=0))) 171 58 172 def testActions(self): 59 173 """ … … 66 180 """ 67 181 # Mock the system file open method 68 QFileDialog.getOpenFileName = MagicMock(return_value=None)182 QFileDialog.getOpenFileNames = MagicMock(return_value=None) 69 183 70 184 # invoke the action 185 self.manager.actionLoadData() 71 186 72 187 # Test the getOpenFileName() dialog called once 73 #self.assertTrue(QtGui.QFileDialog.getOpenFileName.called) 74 #QtGui.QFileDialog.getOpenFileName.assert_called_once() 188 self.assertTrue(QFileDialog.getOpenFileNames.called) 75 189 76 77 # test each action separately 190 def testActionDocumentation(self): 191 """ 192 Menu Help/Documentation 193 """ 194 #Mock the QWebView method 195 QWebView.show = MagicMock() 196 197 # Assure the filename is correct 198 self.assertIn("index.html", self.manager._helpLocation) 199 200 # Invoke the action 201 self.manager.actionDocumentation() 202 203 # Check if show() got called 204 self.assertTrue(QWebView.show.called) 205 206 def testActionTutorial(self): 207 """ 208 Menu Help/Tutorial 209 """ 210 # Mock subprocess.Popen 211 subprocess.Popen = MagicMock() 212 213 tested_location = self.manager._tutorialLocation 214 215 # Assure the filename is correct 216 self.assertIn("Tutorial.pdf", tested_location) 217 218 # Invoke the action 219 self.manager.actionTutorial() 220 221 # Check if popen() got called 222 self.assertTrue(subprocess.Popen.called) 223 224 #Check the popen() call arguments 225 subprocess.Popen.assert_called_with([tested_location], shell=True) 226 227 def testActionAcknowledge(self): 228 """ 229 Menu Help/Acknowledge 230 """ 231 pass 232 233 def testActionCheck_for_update(self): 234 """ 235 Menu Help/Check for update 236 """ 237 # Just make sure checkUpdate is called. 238 self.manager.checkUpdate = MagicMock() 239 240 self.manager.actionCheck_for_update() 241 242 self.assertTrue(self.manager.checkUpdate.called) 243 78 244 79 245 if __name__ == "__main__": -
src/sas/qtgui/UnitTesting/MainWindowTest.py
rf721030 r9e426c1 2 2 import unittest 3 3 4 from PyQt4 .QtGui import *5 from PyQt4 .QtTest import QTest6 from PyQt4 .QtCore import *4 from PyQt4 import QtGui 5 from PyQt4 import QtTest 6 from PyQt4 import QtCore 7 7 from mock import MagicMock 8 8 … … 11 11 from MainWindow import SplashScreen 12 12 13 app = Q Application(sys.argv)13 app = QtGui.QApplication(sys.argv) 14 14 15 15 class MainWindowTest(unittest.TestCase): 16 '''Test the Main Window GUI'''16 """Test the Main Window GUI""" 17 17 def setUp(self): 18 '''Create the GUI'''18 """Create the GUI""" 19 19 20 20 self.widget = MainSasViewWindow(None) 21 21 22 22 def tearDown(self): 23 '''Destroy the GUI''' 24 self.widget.close() 23 """Destroy the GUI""" 25 24 self.widget = None 26 25 27 26 def testDefaults(self): 28 '''Test the GUI in its default state'''29 self.assertIsInstance(self.widget, Q MainWindow)30 self.assertIsInstance(self.widget.centralWidget(), Q Workspace)27 """Test the GUI in its default state""" 28 self.assertIsInstance(self.widget, QtGui.QMainWindow) 29 self.assertIsInstance(self.widget.centralWidget(), QtGui.QWorkspace) 31 30 32 31 def testSplashScreen(self): 32 """ Test the splash screen """ 33 splash = SplashScreen() 34 self.assertIsInstance(splash, QtGui.QSplashScreen) 35 36 def testExit(self): 33 37 """ 38 Test that the custom exit method is called on shutdown 34 39 """ 35 splash = SplashScreen() 36 self.assertIsInstance(splash, QSplashScreen) 40 # Must mask sys.exit, otherwise the whole testing process stops. 41 sys.exit = MagicMock() 42 QtGui.QMessageBox.question = MagicMock(return_value=QtGui.QMessageBox.Yes) 43 44 # Open, then close the main window 45 tmp_main = MainSasViewWindow(None) 46 tmp_main.close() 47 48 # See that the MessageBox method got called 49 self.assertTrue(QtGui.QMessageBox.question.called) 37 50 38 51 if __name__ == "__main__":
Note: See TracChangeset
for help on using the changeset viewer.