对 Python GUI 应用程序进行单元测试的推荐方法是什么? [英] What's the recommended way to unittest Python GUI applications?

查看:40
本文介绍了对 Python GUI 应用程序进行单元测试的推荐方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前愚蠢到尝试为 Python 桌面应用程序维护两个并行代码库,一个使用 PyGObject 自省用于 GTK 3,另一个使用 PyGTK 用于 GTK 2.我主要在 PyGObject 分支上工作,然后我移植更改转到 PyGTK 分支.由于这些实现之间的所有细微差别,我经常忽略一些东西并导致我错过并意外释放的破坏,结果却被用户抓住了.

I'm currently foolish enough to try to maintaintain two parallel code bases for a Python desktop application, one using PyGObject introspection for GTK 3 and one using PyGTK for GTK 2. I work mainly on the PyGObject branch and then I port changes over to the PyGTK branch. Due to all the minor differences between these implementations, I often overlook things and cause breakage that I miss and accidentally release, only to be caught by the users.

我正在尝试找出一种设计一些单元测试的好方法,这些单元测试最好适合在两个代码库上运行.它不是一个过于复杂的程序,(它本质上是一个图书馆管理工具,想象一下就像 iTunes):

I'm trying to figure out a good way to design some unittests that would, preferably, be suitable to run on both code bases. It's not an overly complicated program, (it's essentially a library management tool, imagine like iTunes):

- Main Window
  |- Toolbar with some buttons (add/edit/remove items, configure the program)
  |
  |- VPaned
  |--- Top HPaned
  |------ ListView (listing values by which a library of items can be filtered)
  |------ ListView (listing the contents of the library
  |--- Bottom HPaned
  |------ Image (displaying cover art for the currently selected item in the library)
  |------ TextView (displaying formatted text describing the currently selected item)
 - Edit dialog
 - Configuration dialog
 - About dialog 

我已尝试尽可能将视图与模型分开.这些项目中的每一个都在它自己的类中实现(好吧,在从列出的 GTK 类继承的类中).ListViews 与从 ListStores 继承的其他类耦合.库本身由不同的类处理.尽管如此,需要测试的小部件之间存在交互.例如,如果用户在过滤视图中选择一个特定的项目,过滤库,然后从过滤结果中选择一个项目,则文本视图必须显示正确的库条目的信息,这由于翻译而变得半复杂在 TreeModelFilter 和原始 ListStore 之间迭代,等等.

I've tried to separate views from models as much as possible. Each of those items is implemented in its own class (well, in classes which inherit from the GTK classes listed). The ListViews are coupled with other classes which inherit from ListStores. The library itself is handled by a different class. Nonetheless, there are interactions between the widgets that need to be tested. For example, if the user selects a particular item in the filter view, filtering the library, and then selects an item from the filtered results, the text view must display the information for the correct library entry, which is semi-complicated due to translating iters between TreeModelFilter and the original ListStore, etc etc.

那么,我问,为这样的 GUI 应用程序编写健壮的单元测试的推荐方法是什么?我已经看到有一些用于此的库,但 pygtk 的主要库多年没有更新,因此它们几乎肯定会因 PyGObject 自省而失败.也许我没有足够的创造力来找出使用 Python 的 unittest 模块来做这件事的好方法,所以我愿意接受建议.

So, I ask, what is the recommended way for writing robust unit tests for such a GUI application? I've seen that there are some libraries for this but the main ones for pygtk haven't been updated in years and so they will almost certainly fail with PyGObject introspection. Perhaps I'm not creative enough to figure out a good way to do it using Python's unittest module, so I'm open to suggestions.

推荐答案

您确定要对 GUI 进行单元测试吗?您的复杂性示例涉及 1 个以上的单元,因此是一个集成测试.

Are you sure you want to unit test the GUI? Your example of complexity involves more than 1 unit and is therefore an integration test.

如果你真的想进行单元测试,你应该能够实例化一个类,为其依赖项提供模拟或存根,然后像 GUI 框架一样调用它的方法,例如用户点击.这可能很乏味,而且您必须确切地知道 GUI 框架如何将用户输入分派给您的类.

If you really want to unit test, you should be able to instantiate a single class providing mocks or stubs for its dependencies, then call methods on it like the GUI framework would for e.g. a user click. This can be tedious and you have to know exactly how the GUI framework dispatches user input to your classes.

我的建议是在模型中加入更多的东西.对于您给定的示例,您可以创建一个 FilterManager,它将所有过滤器/选择/显示内容抽象为单个方法.然后进行单元测试.

My advice is to put even more stuff in models. For your given example you could create a FilterManager which abstracts all the filter/select/display stuff behind a single method. Then unit test it.

这篇关于对 Python GUI 应用程序进行单元测试的推荐方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆