After trawling the web for information as to how to write unit tests using Silverlight 4, and not coming into much success, I hope that this article will help you in creating unit tests for your Silverlight projects.
Step 3 - Add references to project:
Step 1 - Install Silverlight Toolkit:
Download and install the Silverlight Toolkit from http://silverlight.codeplex.com/
Step 2 - Create Test Project:
In Visual Studio, create a new Silverlight Application project. You can do this by right clicking on your solution and clicking Add -> New Project. In the project selection screen, select "Silverlight" and then select "Silverlight Application". Give your project a name that will distinguish it as a test project:
Step 3 - Add references to project:
Add "Microsoft.Silverlight.Testing.dll" and "Microsoft.VisualStudio.QualityTools.UnitTesting.Silverlight.dll" as references to your project. The default location for these files using the April 2010 Toolkit is: "C:\Program Files\Microsoft SDKs\Silverlight\v4.0\Toolkit\Apr10\Testing\"
Step 4:
You will notice that the project contains a MainPage.Xaml and an App.Xaml. You can delete the MainPage.Xaml page.
Step 5 - create test page:
Open App.Xaml.cs and locate the Application_Startup method. Modify it to look as follows:
private void Application_Startup(object sender, StartupEventArgs e)
{
this.RootVisual = UnitTestSystem.CreateTestPage();
}
You will need to add using Microsoft.Silverlight.Testing; to the using directives of this class.
Step 6 - Create test class:
You are now ready to create your first test. Create a new class in the project by right clicking on the project and selecting "New --> Class". For this example, I have called my class "TestClass.cs".
And thats it! :)