我一直在研究单元测试pyside代码,得出的结论是,将python的
unittest模块与qt的
QTest模块结合使用非常好。
您将不得不
QApplication实例化一个对象,但是您不需要运行其
exec_方法,因为您不需要运行事件循环。
这是一个有关如何测试
QCheckBox对话框中的a是否应该执行的示例:
class Test_PwsAddEntryDialog(TestCase): """Tests the class PwsAddEntryDialog.""" def test_password_strength_checking_works(self): """Tests if password strength checking works, if the corresponding check box is checked. """ d = PwsAddEntryDialog() # test default of internal flag self.assertFalse(d.testPasswordStrength) # type something QTest.keyClicks(d.editSecret, "weak", 0, 10) # make sure that entered text is not treated as a password self.assertEqual(d.labelPasswordStrength.text(), "") # click 'is password' checkbox QTest.mouseClick(d.checkIsPassword, Qt.LeftButton) # test internal flag changed self.assertTrue(d.testPasswordStrength) # test that label now contains a warning self.assertTrue(d.labelPasswordStrength.text().find("too short") > 0) # click checkbox again QTest.mouseClick(d.checkIsPassword, Qt.LeftButton) # check that internal flag once again changed self.assertFalse(d.testPasswordStrength) # make sure warning disappeared again self.assertEqual(d.labelPasswordStrength.text(), "")这完全可以在屏幕外进行,包括单击窗口小部件并在中键入文本
QLineEdit。
这是我测试的方法(相当简单)
QAbstractListModel:
class Test_SectionListModel(TestCase): """Tests the class SectionListModel.""" def test_model_works_as_expected(self): """Tests if the expected rows are generated from a sample pws file content. """ model = SectionListModel(SAMPLE_PASSWORDS_DICT) l = len(SAMPLE_PASSWORDS_DICT) self.assertEqual(model.rowCount(None), l) i = 0 for section in SAMPLE_PASSWORDS_DICT.iterkeys(): self.assertEqual(model.data(model.index(i)), section) i += 1
我希望这会有所帮助。



