在单元测试中,我正在尝试发送键盘快捷键Command + N(在Mac OS上),这对应于我应用中的菜单项。我正在使用PySide.QtTest模块。
在下面的代码中,我使用QTest.keyClicks
,这不会产生我所期望的。不会调用与快捷方式对应的操作。
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")
有些问题:
答案 0 :(得分:2)
对于“普通”按键单击测试(如在行编辑中输入文本),不必显示窗口。这与您在正常运行应用程序期间将关键事件发送到隐藏窗口小部件时的预期一致。
但是为了测试快捷方式,必须显示目标窗口 < - >这也符合您的预期。如果目标窗口不可见,则键盘快捷键不应在正常运行期间激活命令。
所以你的设置代码应该包括这样的东西:
self.window.show()
QTest.qWaitForWindowShown(self.window)
在异步显示窗口的系统上需要qWaitForWindowShown调用(对于Qt5,请使用qWaitForWindowExposed)。
修改强>:
这是一个适合我的测试脚本:
import unittest
from PySide.QtCore import Qt
from PySide.QtGui import QApplication, QMainWindow, QLineEdit
from PySide.QtTest import QTest
class Window(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
menu = self.menuBar().addMenu('File')
menu.addAction('Test', self.handleTest, 'Ctrl+N')
self.edit = QLineEdit(self)
self.setCentralWidget(self.edit)
def handleTest(self):
self.edit.setText('test')
class AppTestCase(unittest.TestCase):
def setUp(self):
qApp = QApplication.instance()
if qApp is None:
self.app = QApplication([])
else:
self.app = qApp
class WindowTestCase(AppTestCase):
def setUp(self):
super(WindowTestCase, self).setUp()
self.window = Window()
self.window.show()
QTest.qWaitForWindowShown(self.window)
def test_input_object_in_new_file(self):
text = 'test'
self.assertNotEqual(text, self.window.edit.text())
QTest.keyClicks(self.window, 'n', Qt.ControlModifier)
self.assertEqual(text, self.window.edit.text())
def test_enter_text(self):
text = 'foobar'
self.assertNotEqual(text, self.window.edit.text())
QTest.keyClicks(self.window.edit, text)
self.assertEqual(text, self.window.edit.text())
if __name__ == "__main__":
unittest.main()
<强>更新强>:
以上是上述脚本的PyQt5版本:
import unittest
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QApplication, QMainWindow, QLineEdit
from PyQt5.QtTest import QTest
class Window(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
menu = self.menuBar().addMenu('File')
menu.addAction('Test', self.handleTest, 'Ctrl+N')
self.edit = QLineEdit(self)
self.setCentralWidget(self.edit)
def handleTest(self):
self.edit.setText('test')
class AppTestCase(unittest.TestCase):
def setUp(self):
qApp = QApplication.instance()
if qApp is None:
self.app = QApplication([''])
else:
self.app = qApp
class WindowTestCase(AppTestCase):
def setUp(self):
super(WindowTestCase, self).setUp()
self.window = Window()
self.window.show()
QTest.qWaitForWindowExposed(self.window)
def test_input_object_in_new_file(self):
text = 'test'
self.assertNotEqual(text, self.window.edit.text())
QTest.keyClicks(self.window, 'n', Qt.ControlModifier)
self.assertEqual(text, self.window.edit.text())
def test_enter_text(self):
text = 'foobar'
self.assertNotEqual(text, self.window.edit.text())
QTest.keyClicks(self.window.edit, text)
self.assertEqual(text, self.window.edit.text())
if __name__ == "__main__":
unittest.main()