Some while ago, I’ve ported NUnit to Silverlight; that post has been a follow-up to to Jeff Wilcox’s description of how to write unit tests instead of the standard MSTest syntax.
Here’s an update for the ported version of NUnit for version 2.5.5, and for both Silverlight 3 and Silverlight 4.
To create a Silverlight unit test project that uses NUnit, follow these steps:
- Download and extract the binaries, or download the source code and build the assemblies on your computer
- Create a new Silverlight unit test project using the Silverlight unit testing framework (and of course if you don’t have it installed, you should do that by now).
- Reference the three assemblies from the NUnit for Silverlight project.
- In the App.xaml.cs file, register the NUnit unit test metadata provider. This is done in the StartUp event handler for the application, before the test page is created:
private void Application_Startup( object sender, StartupEventArgs e ) { UnitTestSystem.RegisterUnitTestProvider( new Microsoft.Silverlight.Testing.UnitTesting.Metadata.NUnit.NUnitProvider() ); RootVisual = UnitTestSystem.CreateTestPage(); }
UPDATE
- 11-May-2010: Fixed a problem with the Silverlight 4 port; an assembly from the Silverlight 3 testing framework had been referenced.
- 11-June-2010: Updated the NUnit port, please see NUnit for Silverlight: Now with data driven tests
- 01-Dec-2010: Moved the project to Google code, updated to NUnit version 2.5.8.
Download
Please download from nunit-silverlight on Google Code.
Thanks for publishing this, it has really helped getting my project going with silverlight unit testing.
Thanks for publishing. Can you for future updates please sign the assemblies as well?
Good point, will do.
Is there a way to run these in nunit-console? I want to use this to run tests with ncover but am having some trouble figuring it out.
Nope. The NUNit console does not support the Silverlight runtime. This project is only a port of the NUnit.Framework part that provides the test syntaxand assertions, nut the test runner. The tests still need to run under the MS Silverlight test runner. It is possible to collect covrage data, but you need Visual Studio 2010 Ultimate for that: http://www.jeff.wilcox.name/2009/11/november-walkthrough/
Thanks for the quick reply. I guess I am out of luck for now.
Code coverage for Visual Studio 2010 got discontinued and stopped working in the RTM. I guess they were not kidding when they said it was experimental!
Thanks so much for this port. It is going to help. I actually like the syntax of Nunit a lot, especially the newer constraint-based assertions; I believe that it makes the tests a lot more readable.
Just one quick comment for other people who are trying to use this: after you follow your instructions, you set the testing project as the startup project, and then you hit f5.
Thank you very much, finally I can run my SL test without any error
Thank you so much. Finally I can run my test on TeamCity without any problem (at least for now). Good work.