MSTest and Silverlight

One thing missing from the Microsoft Silverlight testing framework is Visual Studio integration. Because of this many people end up using the standard non-Silverlight MSTest framework to test their Silverlight code. At first this may sound a little balmy, however, Silverlight assemblies are byte code compatible with normal .NET assemblies and will quite happily run within CLR 2.0. There are a few things, however, you should be aware of:

  1. You can only make calls to APIs that do not require the underlying Silverlight core (agcore), so anything inheriting from DependencyObject is out, as is WCF.
  2. The [SecurityCritical] attribute is ignored, meaning that calls that would normally be prohibited within Silverlight can be made without resulting in a MethodAccessException.

The limitations described in point one may at first seem quite significant, however, if you are abstracting your UI logic using some flavour of MVVM, MVC, MVP, etc. you will more than likely find that your code does not fall into this category, and WCF calls in most cases should be mocked.

The advantage of running your tests within Visual Studio is that you do not need to run all of your tests for each test run, which is particularly useful if you are following a TDD approach. The downside of course is that you have to be confident that running your tests within this environment does not affect the outcome of your tests, its a trade off.

Creating the Test Project

In Visual Studio create a normal (non-Silverlight) test project.

image

(At iMeta we use the “Hybrid” prefix to indicate we are mixing Silverlight and .Net code).

Remove all references from the project except for the reference to Microsoft.VisualStudio.QualityTools.UnitTestFramework

image

Re-add any required references but this time selecting Silverlight assemblies, usually located within “C:\Program Files\Reference Assemblies\Microsoft\Framework\Silverlight\v3.0” and “C:\Program Files\Microsoft SDKs\Silverlight\v3.0\Libraries

5 Comments Filed Under [ Silverlight Testing ]

Comments

# re: MSTest and Silverlight
Gravatar Hi,

this is exactly how we do our silverlight unit testing. One problem we are having with this currently is that if a solution contains both unit test projects that refer to the standard .NET dlls (eg system) and test projects that are pointing to the silverlight variants of these dll's, the tests can never be run together. When you run all .NET tests they run fine and the same goes for all silverlight tests. If, however, you trigger all these tests (what the build server is doing obviously) either one set or the other fails entirely. The only information we see is an error message that states: Unit Test Adapter threw exception: "Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information..". Have you experienced any such issues?
Left by Phil on 10/29/2009 10:31 AM
# re: MSTest and Silverlight
Gravatar Hi,

We don't experience this issue, which continuous integration/build environment do you run, we are using Team System.
Left by jyoung on 10/29/2009 8:57 PM
# re: MSTest and Silverlight
Gravatar Hi
I followed the steps you described, but when I try to run my tests in Visual Studio I get the following exception:

Test method <testprj>.<testclass>.TestMethod1 threw exception: System.TypeInitializationException: The type initializer for '<prj>.<class>' threw an exception. ---> System.DllNotFoundException: Unable to load DLL 'agcore': The specified module could not be found. (Exception from HRESULT: 0x8007007E).

The exception is thrown on the line where I call the constructor of the class I want to test. The class is a view-model class inside a Silverlight 3 Class Library.

Do you have any ideea why I get this exception?

Thanks
Left by Ciprian on 3/31/2010 8:32 AM
# re: MSTest and Silverlight
Gravatar As mentioned in the article there are a few limitations with this technique. If your method accesses anything inherited from DependencyObject or uses the Dispatcher you will get these exceptions. You can often remove these references using Dependency Inversion. You may also want to check out SilverUnit on codeplex http://cthru.codeplex.com/
Left by jyoung on 4/5/2010 10:17 PM
# re: MSTest and Silverlight
Gravatar I'm using the Silverlight Unit Testing Framework and followed the Jeff Wilcox article how to set things up (using his project and item templates). I can run the tests succesfully manually and see the results in a browser but when I try to run the test with mstest, I get the previous error messages.....
Left by macchinette mangiasoldi on 4/14/2010 11:10 PM

Leave Your Comment

Title*
Name*
Email (never displayed)
 (will show your gravatar)
Url
Comment*

Please add 1 and 7 and type the answer here:

Preview Your Comment.