As announced during the MIX'08 keynote by Scott Guthrie, not only are we making available the source code of the Silverlight controls
, but we're also releasing nearly 2,000 unit tests and a unit test framework for the Silverlight runtime. This is a project I've been working on for some time now, so I'm planning on a series of in-depth tutorials about this exciting ability. In the meantime, I'll send you towards some other links and a quick preview...
It is my hope that this provides the ability for teams to do some level of test-driven development using Silverlight, and so I'm very interested in receiving your feedback on how it works. We've had pretty good success using this framework for developing the managed Silverlight controls that were released in the Silverlight 2 Beta 1 SDK
Exploring the control unit tests:
Shawn Burke gives an overview
of the control source code, unit tests and framework
: I've also added a quick video post
with a screencast that shows the control tests running.
Using the unit test framework:
Coming soon :-), once I'm back from MIX'08 I'll get the tutorial posts off to a start - subscribe to my feed
Microsoft.Silverlight.Testing running DataGrid control tests
Built on the power of the Visual Studio Team Test unit test framework
If you've used the unit test framework built into the Visual Studio Team Test editions (now available with Visual Studio 2008 Professional as well), then you know just how easy and powerful the framework is.
You'll find that pretty much everything in the desktop unit test framework is available here when it comes to metadata and attributes. There's no data-driven tests or advanced host attribute support, but we've also added some helpful attributes such as "Exclusive" attribute (that lives in the Microsoft.Silverlight.Testing namespace), since there's no Visual Studio integration with the framework.
Hopefully this will also help you to get more familiar with the Visual Studio unit test framework so that you can use it for your desktop apps, since the metadata and assertion class knowledge transfers perfectly.
If you're new to the unit test framework, check out the MSDN documentation on the Assert, StringAssert, and CollectionAssert classes, plus of course the attribute metadata.
MSDN Library Documentation: Microsoft.VisualStudio.TestTools.UnitTesting
A sample testcase that uses the unit test framework's attributes; this particular test source code can run using Microsoft.Silverlight.Testing or the desktop Visual Studio Team Test unit test framework, if the references for the project are updated.
Downloading the framework
To get started, download
the Silverlight 2 Beta 1 controls source code
. Inside the self-extracted Zip file, there is a subdirectory called 'bin' that contains the three assemblies that make up the unit test framework:
||Unit test framework
||Base classes and interfaces for test harnesses
||Visual Studio Team Test metadata built against the Silverlight libraries
These are typical Silverlight class library assemblies, so you can reference them in your projects like any other library.
Installing the framework
To make it easier to add the test framework assemblies as references' to your projects, I'd recommend copying the framework assemblies into the client libraries directory where the SDK installs the controls and other bits.
To do this, simply copy the three into "%programfiles%\Microsoft SDKs\Silverlight\v2.0\Libraries\Client". The next time you use Visual Studio's Add Reference window from within a Silverlight application project, you'll see the assemblies present:
Please note that the unit test framework isn't part of the SDK, but this is a convenient place to store them.
Helpful C# test project templates
Although it's pretty easy to tie into the framework by adding a new Silverlight application project to your solution, that requires a few hookup details, such as removing Page.xaml.cs, updating the root visual, and some other magic.
It's much easier to instead use these helpful Visual Studio 2008 project and item templates:
- SilverlightTestProject.zip (project template, adds a test project Silverlight application to the solution)
Copy this into your "%userprofile%\Documents\Visual Studio 2008\Templates\ProjectTemplates"
- SilverlightTestClass.zip (item template, adds a test class to your Silverlight test project)
Copy this into your "%userprofile%\Documents\Visual Studio 2008\Templates\ItemTemplates"
Using the C# test project templates
To create a new Silverlight test project, with your Silverlight application or class library open:
- Right-click on the solution
- Select the Add->New Project menu item
- Click on the root "Visual C#" project type node
- Under "My Templates", select "Silverlight Test Project", and give your test project a name
There's a default test.cs file that you can use as a starting point, or to add additional test classes to your test project:
- Right-click on the test project
- Select the Add->New Item menu option
- Click on the root "Visual C#" category
- Select "Silverlight Test Class" and provide a name for your new class
Hope you find this is pretty useful, let me know!