14. December 2010 12:14
If you want to write unit tests in Silverlight, it’s a pretty solid bet that you’ll be using using Jeff Wilcox’s unit testing framework that ships with the Silverlight Toolkit. It’s essentially a port of mstest, combined with a Silverlight based test runner. Handily, the test runner allows for unit test providers, allowing us to plug in a different test framework.
And that’s exactly what the latest release of xunitcontrib is – a port of xUnit.net, and a provider for the unit testing framework.
It’s a pretty faithful port of xunit (i.e. with as few changes to the source as possible), and includes the majority of the features that you’ll require day-to-day for testing, but it’s also running on a different platform and in a different execution environment to the desktop CLR version of xunit, so THERE ARE DIFFERENCES.
I strongly suggest checking out what’s different or not included in the xunit port, as well as the details of the unit testing framework integration.
In the mean time, you can download the release from codeplex. It supports Silverlight 4 using the April 2010 toolkit and Silverlight 3 using the semi-official build based on the April 2010 toolkit (Windows Phone 7 support is coming in the next release). Here’s the quick run down on how to use it:
- Create a Silverlight application
- Add references to Microsoft.Silverlight.Testing.dll and the Silverlight-appropriate versions of xunit-silverlight.dll, xunit.extensions-silverlight.dll and xunitcontrib.runner.silverlight.toolkit.dll
- In App.xaml.cs, replace Application_Startup with:
RootVisual = UnitTestSystem.CreateTestPage();
- Then just add tests, and run the application
Alternatively, if you’ve installed the latest Silverlight Toolkit, you’ll get a unit testing project template in the File->New dialog that makes this a little easier, although you’ll still need to add the xunit/xunitcontrib references and register the xunitcontrib provider (if you don’t register it, you’ll get no tests!)
As ever, let me know, either via codeplex or on twitter (@citizenmatt) if there are problems or questions.