Tuesday, September 21, 2010

Creating unit tests using Silverlight 4

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 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".



To identify this class as a class that contains your unit tests, you need to add the [TestClass] attribute above the class name:

using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace TestApplication
{
[TestClass]
public class TestClass
{

}
}

You are now ready to add test methods for the methods in your normal Silverlight class library / application projects.

Step 7 - Add test methods:
It is now time to set up your tests within your TestClass. In this example, I have a Silverlight class library that I would like to test. This library has a class called CommonFunctions and that has a method called HelloWorld() that should return "Hello World!". To add a test for this into my test class, I added the following:

using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace TestApplication
{
    [TestClass]
    public class TestClass
    {
        [TestMethod]
        public void Should_Return_HelloWorld()
        {
            Common.CommonFunctions functions = new Common.CommonFunctions();
            string returnString = functions.HelloWorld();
            Assert.IsTrue(returnString.CompareTo("Hello World!") == 0);
        }
    }
}

Step 8 - Run unit tests:
Now, lets test if the tests will pass. Firstly, set your test project as your startup project by right clicking on it and selecting "Set as startup project".

Then, click Run (F5). You will be presented with a web page that will then run your tests in the TestClass and show you which tests passed and which failed:




And thats it!  :)