In a unit test, I'm trying to send the keyboard shortcut Command+N (on Mac OS), which corresponds to a menu item in my app. I'm using the PySide.QtTest module.
In the code below, I'm using QTest.keyClicks
, which doesn't produce what I'm expecting. The action corresponding to the shortcut is not called.
class AppTestCase(TestCase):
def setUp(self):
qApp = QApplication.instance()
if qApp is None:
self.app = QApplication([])
else:
self.app = qApp
class IdfEditorTestCase(th.AppTestCase):
def setUp(self):
super(IdfEditorTestCase, self).setUp()
self.window = IdfEditorWindow()
def test_input_object_in_new_file(self):
if os.path.exists("current_running_test.idf"):
os.remove("current_running_test.idf")
self.window.selectClass("ScheduleTypeLimits")
QTest.keyClicks(self.window, "n", Qt.ControlModifier)
self.window.saveFileAs("current_running_test.idf")
self.assertIdfFileContentEquals("current_running_test.idf", "ScheduleTypeLimits,,,,,;\n")
Some questions:
- Should I send this to the window itself? Or to the menubar? Neither seem to work...
- Is that the right way of sending a keyboard shortcut?
For "normal" key-click tests (like entering text in a line-edit), it is not necessary to show the window. This is in line with what you'd expect if you sent key events to a hidden widget during normal running of the application.
But for testing shortcuts, the target window must be shown - which is again in line with what you'd expect. A keyboard shortcut should not activate commands during normal running if the target window is not visible.
So your setup code should probably include something like this:
The qWaitForWindowShown call is necessary on systems where windows are shown asynchronously (for Qt5, use qWaitForWindowExposed).
EDIT:
Here's a test script that works for me:
UPDATE:
Here's a PyQt5 version of the above script: