单元测试 MFC UI 应用程序?
如何对大型 MFC UI 应用程序进行单元测试?
How do you unit test a large MFC UI application?
我们有一些大型 MFC 应用程序已经开发了多年,我们使用一些标准的自动化 QA 工具来运行基本脚本来检查基础、文件打开等.这些由 QA 小组在每日构建后运行.
We have a few large MFC applications that have been in development for many years, we use some standard automated QA tools to run basic scripts to check fundamentals, file open etc. These are run by the QA group post the daily build.
但我们希望引入一些程序,以便个别开发人员在将代码提交到日常构建之前,可以针对应用程序的对话框、菜单和其他可视元素构建和运行测试.
But we would like to introduce procedures such that individual developers can build and run tests against dialogs, menus, and other visual elements of the application before submitting code to the daily build.
我听说过这样的技术,例如仅在调试版本中出现的对话框上的隐藏测试按钮,是否有任何标准工具包.
I have heard of such techniques as hidden test buttons on dialogs that only appear in debug builds, are there any standard toolkits for this.
环境是 C++/C/FORTRAN, MSVC 2005, Intel FORTRAN 9.1, Windows XP/Vista x86 &x64.
Environment is C++/C/FORTRAN, MSVC 2005, Intel FORTRAN 9.1, Windows XP/Vista x86 & x64.
推荐答案
这取决于应用程序的结构.如果逻辑和 GUI 代码是分开的 (MVC),那么测试逻辑很容易.看看 Michael Feathers "Humble Dialog Box" (PDF).
It depends on how the App is structured. If logic and GUI code is separated (MVC) then testing the logic is easy. Take a look at Michael Feathers "Humble Dialog Box" (PDF).
如果您考虑一下:如果应用程序的结构不是这样,您应该非常仔细地重构.没有其他技术可以测试逻辑.模拟点击的脚本只是表面上的.
If you think about it: You should very carefully refactor if the App is not structured that way. There is no other technique for testing the logic. Scripts which simulate clicks are just scratching the surface.
其实很简单:
假设您的控件/窗口/任何东西在用户单击按钮时会更改列表框的内容,并且您希望确保列表框在单击后包含正确的内容.
Assume your control/window/whatever changes the contents of a listbox when the user clicks a button and you want to make sure the listbox contains the right stuff after the click.
- 重构,以便有一个单独的列表,其中包含要显示的列表框的项目.这些项目存储在列表中,并且不会从您的数据来源中提取.使列表框列表事物的代码只知道新列表.
- 然后创建一个包含逻辑代码的新控制器对象.处理按钮点击的方法只调用mycontroller->ButtonWasClicked().它不知道列表框或其他任何东西.
- MyController::ButtonWasClicked() 为预期的逻辑做了需要做的事情,准备项目列表并告诉控件更新.为此,您需要通过为控件创建接口(纯虚拟类)来解耦控制器和控件.控制器只知道该类型的对象,而不知道控件.
就是这样.控制器包含逻辑代码并且仅通过接口知道控制.现在您可以通过模拟控件为 MyController::ButtonWasClicked() 编写常规单元测试.如果您不知道我在说什么,请阅读 Michaels 的文章.两次.在那之后又一次.
(提醒自己:一定要学会不要胡说八道)
Thats it. The controller contains the logic code and knows the control only via the interface. Now you can write regular unit test for MyController::ButtonWasClicked() by mocking the control. If you have no idea what I am talking about, read Michaels article. Twice. And again after that.
(Note to self: must learn not to blather that much)
相关文章