问题描述
我正在构建一个基于 PySide 1.1.0 的应用程序,并且一直在寻找好的示例来查看我的应用程序的单元和功能测试.我希望能够对 UI 进行功能测试(模拟点击、按键等),对改变 UI 布局的 UI 插槽进行单元测试(大概使用部分模拟的发送器和接收器),以及单元测试涉及小部件但不需要渲染任何窗口的代码.
I'm building a PySide 1.1.0-based application, and have been looking for good examples to look at for unit and functional testing my application. I want to be able to do functional testing of the UI (simulating clicks, key presses, etc), unit testing of UI slots that alter the layout of the UI (presumably using a partially-mocked sender and receiver), as well as unit testing of code that involves widgets, but without requiring any windows to be rendered.
举个例子,当一个项目被添加到一个模型(QAbstractItemModel 派生的对象)时,我会在菜单栏中动态创建一个菜单的子菜单,该模型为 QTreeView 提供数据.模型和子菜单必须保持同步,所以我希望能够编写一个单元测试,将数据提交给管理模型和子菜单的控制器,并断言模型和子菜单都已正确更新.
As one example, I dynamically create submenus of one menu in the menubar when an item is added to a model (QAbstractItemModel-derived object) that provides data to a QTreeView. The model and submenu must stay in sync, so I want to be able to write a unit test that submits data to the controller that manages the model and submenu, and asserts that both the model and submenu were properly updated.
如果可以避免的话,我宁愿不必在我的测试代码中设置 QApplication.当我只关心验证小部件中的数据结构而不是它们的可视化时,我也不想显示任何窗口.
I would prefer to NOT have to set up a QApplication in my test code if I can avoid it. I also would like to not have to display any windows when I only care about validating data structures in widgets, not their visualization.
我在 http://www.pyside.org 或我的谷歌搜索.有没有人有任何经验或知道我应该查看的好的示例代码?
I can't find anything of suitable value at http://www.pyside.org or in my Google searches. Does anyone have any experience or know of good sample code that I should look at?
推荐答案
我现在一直在玩单元测试 pyside 代码,得出的结论是将 python 的 unittest
模块与qt 的 QTest
模块运行良好.
I've been playing around a bit now with unit-testing pyside code and came to the conclusion that combining python's unittest
module with qt's QTest
module works pretty good.
您必须实例化一个 QApplication
对象,但您不需要运行其 exec_
方法,因为您不需要运行事件循环.
You will have to have a QApplication
object instantiated, but you do not need to run its exec_
method, because you don't need the event loop to be running.
以下是我如何测试对话框中的 QCheckBox
是否执行它应该做的事情的示例:
Here is an example on how I test if a QCheckBox
in a dialog does what it is supposed to do:
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
中输入文本.
This completely works off-screen, involves clicking widgets and typing text in a QLineEdit
.
这是我测试(相当简单的)QAbstractListModel
的方法:
Here is how I test a (rather simple) 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
我希望这会有所帮助.
这篇关于基于 PySide 的应用程序的单元和功能测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!