Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to test a WPF user interface?

Tags:

.net

testing

wpf

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.

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

like image 708
Matt David Avatar asked Sep 12 '08 05:09

Matt David


People also ask

What is WPF testing?

WPF is one of the largest technologies used to build desktop applications. It is a next generation UI Framework built by Windows, and a subsystem of the . NET framework (version 3.0 and above). Simply put, it is a UI framework for building desktop applications on Windows.


1 Answers

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).

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.

like image 187
Kjetil Klaussen Avatar answered Sep 20 '22 10:09

Kjetil Klaussen