WPF Unit Testing

[UPDATED – 16th October 2007] 

What is development without unit testing?
For me it is just chaos! You make a bug fix and you end up giving birth to another 3… Also if you don’t have unit tests, refactoring becomes a risk!

When developing software using WPF you will hit a brick wall when creating your first unit test. You will encounter the InvalidOperationException curse.. The NUnit test runner will show you the red light and tell you: InvalidOperationException Failed to set the specified COM apartment state.

The cause of this problem is that NUnit runs in an MTA while WPF must run in STA. I found the solution to this problem here. I also added this class in my AvalonUnitTesting library. Basically to run unit tests that are WPF related you can use a class called AvalonTestRunner and call the RunInSta method passing a delegate. This will auto magically switch the current thread apartment to STA for you… an example of such a test is the following

/// <summary>
/// Test running on an STA Thread
/// </summary>
public void TestMainWindow()
           MainWindow window = new MainWindow();
Assert.AreEqual(300, window.Height);

WPF is a very strong and powerful UI platform. I don’t know why I said that but I just felt like saying it 🙂 Anyway back to unit testing. How can you unit test control and maybe even simulate user interaction? I found a very interesting solution here.Yet I still wanted more… So I kept searching …One thing that you will surely notice is that when using cool features of WPF such as DataBinding, you never get exceptions. The reason is that WPF has silent exceptions (If i might say sometimes silent but deadly :0 ). The exceptions in data binding are still happening but they are handled by the WPF framework and displayed in the output window of the Visual studio. For more information about the Tracing in WPF have a look here.

While reading this blog I got an idea. I said why don’t I create a trace listener that can assert when ever a WPF data binding exception is raised.. Basically this solution can make your XAML data binding testable! So I created a class that listens to these data binding warning and asserts to show the developer that there is a problem in his XAML. To use these feature all you have to do is 1 line of code as you will se in the example.

/// <summary>
/// Unit Tests wpf data binding
/// </summary>
public class WPFSampleApplicationTests
{           [Test]
           public void TestDataBindingForControls()
                                    //test the main window XAML.
                                   //This will assert all data binding errors
                                  AvalonTestRunner.RunDataBindingTests(new MainWindow());
                                  //runs the test for a specific user control
                                AvalonTestRunner.RunDataBindingTests(new UserControlTests());                       



In this example I am testing a Window for data binding. I am also testing a user control in the second line of code of the TestDataBindingForControls method. So basically with this solution you can test both Window, pages and also custom controls (or user controls)… Please note that when you pass an object that is not of type Window the RunDataBindingTests methods takes care of wrapping your control in a window and run all data binding tests. If you have a control that uses data bindings that assumes that the control is always in a specific window (which would be a situation where your control needs some serios re thinking) this solution would not work. Since the wrapper window would not have the same objects loaded! Any way feel free to download the full source and start playing around… Please let me know if you need any help using the library…DOWNLOAD FULL SOURCE CODE

4 thoughts on “WPF Unit Testing

  1. Nice post. I wish you had written this about 6 months ago, it would have really helped out! 🙂

    In case you’re interested, a while back I solved the problem of unit testing a class which uses a DispatcherTimer. It was much more complicated and interesting than I imagined it would have been, so I wrote an article about it. Here’s the link: http://www.codeproject.com/WPF/UnitTestDispatcherTimer.asp

    Josh Smith

  2. Hi there,

    First of all let me tell you, it’s an honour to have a comment from you. You really rock Josh!!!

    Thanks for the link I will have a look at it… All your posts are usually great and I always learn something new from you…


  3. Pingback: Random Reading On The Train « Tales from a Trading Desk

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s