Reputation: 18649
I'm using the Silverlight UnitTest framerwork does anyone have a good example have how to unit test an application with it? I'm using it quite successfully to unit test a silverlight class library.
Any pointers and links would be greatly appreciated.
Thanks, Nath
Upvotes: 7
Views: 1083
Reputation: 1010
Here is my definite guide to unit testing in Silverlight 5. This will guide to all the points you have to do, if you have no clue, where to start. This guide is about testing inside your project. You will not need a 2nd odr 3rd silverlight project to unit test.
0.) Close your Silverlight Project.
1.) Download the Silverlight 5 toolkit here and install it.
2.) Open your Windows Explorer, go to C: and then search for these two files:
Microsoft.Silverlight.Testing.dll
Microsoft.VisualStudio.QualityTools.UnitTesting.Silverlight.dll
If you don't find them, something is wrong.
3.) Register these two DLLs in the GAC. For that do the follwing:
Open the Visual Studio console as Administrator. Copy each of the statements and execute them in the console:
gacutil /i "C:\Program Files\Microsoft SDKs\Silverlight\v5.0\Toolkit\dec11\Testing\Microsoft.Silverlight.Testing.dll"
gacutil /i "C:\Program Files\Microsoft SDKs\Silverlight\v5.0\Toolkit\dec11\Testing\Microsoft.VisualStudio.QualityTools.UnitTesting.Silverlight.dll"
Please keep in mind, that the paths to the DLLs could be different on your machine. Just type the correct path for your machine.
4. Open your Silverlight Project and add the two DLLs from above as a reference to Silverlight project. Not to your web project.
5. Add the following class to your project. It does not matter where you put it. I have an extra folder for my tests, but does'nt matter.
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Silverlight.Testing;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace Test
{
[TestClass]
public class Test_Svc_Login
{
[TestMethod]
public void Always_True()
{
Assert.IsTrue(true);
}
[TestMethod]
public void Always_False()
{
Assert.IsTrue(false);
}
[TestMethod]
public void Even_MoreAlways_False()
{
Assert.IsTrue(false);
}
}
}
6. Hit F6, try to compile. Everything should be ok at this point.
7. Find the file "App.xaml.cs" in your project. Inside this file you will find the following statement. Of course "new Gui.MainPage();" will be different in your project. Just search for RootVisual.
this.RootVisual = new Gui.MainPage();
You replace this statement with the following code block.
#if DEBUG
if (System.Diagnostics.Debugger.IsAttached)
{
//You hit F5 ONLY
this.RootVisual = new Gui.MainPage();
}
else
{
//You hit CTRL + F5
RunUnitTests();
}
#else
//You are in Release Mode. You hit whatever you want.
this.RootVisual = new Gui.MainPage();
#endif
Now you add the following routine to your App.xaml.cs class:
private void RunUnitTests()
{
#if DEBUG
//You hit CTRL + F5
var settings = new UnitTestSettings();
settings.TestHarness = new UnitTestHarness();
settings.StartRunImmediately = true;
settings.TestAssemblies.Add(Assembly.GetExecutingAssembly());
this.RootVisual = UnitTestSystem.CreateTestPage(settings);
#endif
}
8. Hit F6 to check if everything is ok.
9. Hit F5 to see, if you can still debug normally. Hit Ctrl+F5 to start the Unit Test.
10. You are done! Now you can unit test in Silverlight 5.
Please keep in mind, that the two DLLs from the toolkit are still in "experimental" mode. There is no official support. Also keep in mind, that is approch is about running the tests inside your project. Now you could even run these test at a clients machine in release mode. If want to do that, you have to change #if DEBUG statements, but I think you see what it's all about.
Of course you can change this approach to unit test in an extra project.
Happy Coding.
HINT: If you have problems with System.Core.dll after registering the two DLLs, just use gacutil to register the System.Core.dll again. Like this:
gacutil /i "C:\Program Files\Reference Assemblies\Microsoft\Framework\Silverlight\v5.0\System.Core.dll"
Upvotes: 2
Reputation:
You should also try to use SilverUnit for real Silverlight unit testing, it is a really easy to use framework, which also protects you from the intricacies of Silverlight event processing.
Upvotes: 1
Reputation: 6531
I've written a couple of blog posts about the Silverlight Unit Testing Framework. The first one covers how to test asynchronous code using the asynchronous attribute and some of the more advanced features of the framework. The post can be found at http://jonas.follesoe.no/UnitTestingAsynchronousSilverlightCode.aspx
The second post is about organizing your tests using the Tag-attribute. This enables you to have unit/integration/performance/UI tests in the same test suite, but only execute one specific category at a time. The blog post can be found at http://jonas.follesoe.no/EfficientTestingInSilverlight2UsingTags.aspx
Upvotes: 8
Reputation: 10005
Nath, The Silverlight Control Toolkit uses it. Why not take a look at the unit tests there?
http://www.codeplex.com/silverlight
Upvotes: 4