[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
/// Test running on an STA Thread
public void TestMainWindow()
MainWindow window = new MainWindow();
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.
/// Unit Tests wpf data binding
public class WPFSampleApplicationTests
public void TestDataBindingForControls()
//test the main window XAML.
//This will assert all data binding errors
//runs the test for a specific user control
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