如何测试 WPF 用户界面? [英] How to test a WPF user interface?

查看:28
本文介绍了如何测试 WPF 用户界面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用带有 MVC/MVP 架构,我通常会使用一个类来包装视图来测试 UI,同时使用模拟来测试 UI模型和控制器/演示者.包装类将使 UI 中的大部分内容成为测试运行程序通过属性和事件可观察到的属性.

Using win forms with an MVC/MVP architecture, I would normally use a class to wrap a view to test the UI while using mocks for the model and controller/presenter. The wrapper class would make most everything in the UI an observable property for the test runner through properties and events.

这是测试 WPF 应用程序的可行方法吗?有没有更好的办法?有什么需要注意的问题吗?

Would this be a viable approach to testing a WPF app? Is there a better way? Are there any gotchas to watch out for?

推荐答案

至于测试本身,您可能最好使用 UI 自动化 框架.或者,如果您想要更流畅且独立于 wpf/winforms/win32/swt 的框架使用方式,您可以下载 White 来自 Codeplex(前提是您能够在您的环境中使用开源代码).

As for the testing itself, you're probably best off using the UI Automation framework. Or if you want a more fluent and wpf/winforms/win32/swt-independent way of using the framework, you could download White from Codeplex (provided that you're in a position to use open source code in your environment).

对于陷阱;如果您正在尝试测试您的视图,您可能会遇到一些线程问题.例如,如果您正在运行 NUnit,则默认测试运行程序将在 MTA(多线程应用程序)中运行,而 WPF 需要作为 STA(单线程应用程序)运行.Mike Two 入门非常简单在单元测试 WPF,但没有考虑线程问题.Josh Smith 在这篇文章,他还指向 这篇文章 作者:Chris Hedgate.Chris 使用了 Peter Provost 的 CrossThreadTestRunner 的修改版本以更友好的方式解决 MTA/STA 问题.

For the gotchas; If you're trying to test your views, you will probably run in to some threading issues. For instance, if you're running NUnit the default testrunner will run in MTA (Multi-Threaded Appartment), while as WPF needs to run as STA (Single-threaded Appartment). Mike Two has a real easy getting-started on unit testing WPF, but without considering the threading issue. Josh Smith has some thoughts on the threading issue in this post, and he also points to this article by Chris Hedgate. Chris uses a modified version of Peter Provost's CrossThreadTestRunner to wrap the MTA/STA issues in a bit more friendly way.

这篇关于如何测试 WPF 用户界面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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