Unit-Testing Silverlight Projects

In the last post, I wrote about, how to use ReSharper with Silverlight Unit Test Projects. Now I can do TDD with Silverlight very easily. :-)

But I don’t like the MSTest assertions syntax. In the Past I used NUnit to write my tests. How can I use the NUnit syntax inside the Silverlight unit tests?

My first Idea was to write the NUnit assertions syntax by my own to encapsulate the MSTest. But the easiest way is to really use NUnit assertions in MSTest :-)

How does this work?

First of all:

Download NUnit for Silverlight: http://code.google.com/p/nunit-silverlight/ and reference to NUnit.Framework.
(Be sure the assemblies are unblocked. If not, unblock the assemblies in the file properties dialog)

After that:

Write a small alias in the usings of your test class:

using Assert = NUnit.Framework.Assert;

Thats all:

Now you can use the NUnit assertions syntax in your MSTest:

// Assert
Assert.That(result, Is.EqualTo(Visibility.Visible));
Assert.Throws<Exception>(() => hasItemsConverter.Convert(items, null, null, null));

This is not only a solution for Silverlight Unit Test, you can do this with all other MSTest where you want to use NUnit assertions ;-)

Why does that work?

Assertions are only simple tests. If the tests fail, a Exception will be thrown. This is the same procedure in all unit test frameworks. So you can mix the frameworks in your unit test projects.

DotNetKicks-DE Image
MSTest, NUnit, Silverlight, tdd, unit tests , , , ,

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>