Set up unit testing
A very well readable and working HOWTO on how to set up unit testing with the May 2010 version of the Silverlight unit testing framework can be found in this blog post by Roger Peters, thanks a lot! Follow the steps laid out there. Adding NUnit support is an additional step on top.
Add NUnit binaries
Download the binary release of the nunit-silverlight project. Unpack the zip file. Navigate to the folder "Silverlight3-NUnit2.5.8-Apr2010". Unblock the DLLs in the directory: since they have been downloaded from the Internet, they cannot be executed unless you specificly tell Windows it's safe to do so (right click on file -> Properties -> Unblock button at the bottom of the "General" tab).
You should already have references to the two Microsoft assemblies, now add the three NUnit assemblies to you project references: NUnit.Silverlight.Compatibility.dll NUnit.Silverlight.Framework.dll NUnit.Silverlight.Metadata.dll
Like the unit testing framework itself, these have been compiled against the Silverlight 3 runtime and not the Windows Phone 7 runtime, but there are no known problems with this fact in this case (see blog post.
Tell the unit testing framework to run NUnit tests
Open the code-behind file of the MainPage in your project. You should already have a Loaded event handler. Now one more line needs to be added that tells the unit testing framework to use the NUnit test attributes instead of the default MSTest attributes:
``` void MainPage_Loaded(object sender, RoutedEventArgs args) { // Hide the system tray to have more space SystemTray.IsVisible = false;
// Register NUnit as the provider for test metadata (attributes etc)
UnitTestSystem.RegisterUnitTestProvider(
new Microsoft.Silverlight.Testing.UnitTesting.Metadata.NUnit.NUnitProvider() );
// Let the unit testing framework take over.
var testPage = UnitTestSystem.CreateTestPage() as IMobileTestPage;
BackKeyPress += (x, xe) => xe.Cancel = testPage.NavigateBack();
(Application.Current.RootVisual as PhoneApplicationFrame).Content = testPage;
} ```
Now any NUnit test fixture you add to the project should work. Note that you can only have NUnit tests or MSTest tests in a project, not a mixed combination.