Running NUnit 2.53 tests for Silverlight

Jeff Wilcox has described in this article how to make NUnit tests run in the Silverlight unit testing framework. That article provides all the groundwork for what you need to do:

  • Compile a version of the NUnit framework that runs in the Silverlight 3 context
  • Provide metadata for the Silverlight unit testing framework so it knows which attributes to look for in order to locate what method is a test (and a little more)
  • Register this metadata in your Silverlight unit test project.

I’d like to thank Jeff for his great work, and provide some raisins on top: an update to version 2.53 of NUnit. Here’s what’s to do:

  • Take the compatibility shim and test metadata provider from Jeff’s article.
  • Download the current NUnit source code (in this case 2.5.3). Create a new Silverlight project, and add the code for the NUnit framework into it. Compile: a whole bunch of exceptions come up.
  • NUnit’s source code uses the #if precompiler directive in order to exclude parts that are incompatible with different version of the runtime. By defining the NET_2_0 symbols, we can make a lot of the errors disappear: the nongeneric implementation of Stack, for example, is excluded now. We need to exclude some more code: Instead of deleting it altogether, I chose to use the SILVERLIGHT symbol and add a #if !SILVERLIGHT directive to get rid of unsupported code instead. In cases where a whole method body is excluded, add a section on the bottom that throws a NotSupportedException:
    #if SILVERLIGHT
          throw new NotSupportedException( "Not supported in Silverlight" );
    #endif
    
  • We need to get rid of the serialization constructors from all the exceptions.
  • Remove the attributes for threading demands – they’re useless in Silverlight anyway.
  • Remove the attributes for threading demands – they’re useless in Silverlight anyway.
  • Add a new shim class for the non-generic System.Collections.Specialized.ListDictionary to the Compatibility project. It is a simple derivation of the genetic Dictionary.
  • In the PathComparison class, there are some unsupported string operations. I didin’t bother exluding and replacing these, and just refactored them to supported alternatives. (Thinking about it, I should probably have used the .NET2.0 switch. Next time, promised)
  • Finally, the metadata provider uses a changed property for in the ExpectedException attribute. Corrected, aaaaaand…
  • Done. Now all you need to do is reference the three generated assemblies, follow Jeff’s article on how to set up the testing framework for the NUnit provider, and start writing tests.

This was easy enough that I hope it can be done for any upcoming version of NUnit.

The point where I’d like to disagree with Jeff Wilcox the idea that NUnit was a legacy solution, and that you should start new projects with Visual Studio testing. NUnit provides much richer assertions, and a better syntax for setting them up: you won’t find assertions for examining collection content or thrown exceptions in Visual Studio testing. Or the upcoming ability to run tests in parallel compartments to simulate a distributed environment. NUnit is far from being a discontinued legacy solution, it is the de facto standard and quickly developing. And: thanks to the extensibility of the Silverlight unit testing framework, there’s not a reason not to use it for Silverlight as well.

UPDATE

The project is now hosted as nunit-silverlight on Google Code.

About these ads
About

Christian is a software architect/developer. He lives in Germany, reads a lot, and likes cycling.

Tagged with: , , ,
Posted in Coding, Testing
5 comments on “Running NUnit 2.53 tests for Silverlight
  1. […] for Silverlight updated Some while ago, I’ve ported NUnit to Silverlight; that post has been a follow-up to to Jeff Wilcox’s description of how to write unit tests […]

  2. Pete Magsig says:

    I’ve been trying to get SL4 NUnit tests to run via the nunit console. After a bit of fussing, I got it to work. I had to rebuild nunit with 4.0 as my target framework.

    The error I was getting from nunit was a TargetFrameworkAttribute missing from mscorlib. It wouldn’t even load the assembly. Well, if you build nunit with mscorlib 2.0.5.0 or later, this attribute is present, and the problem goes away.

    The nunit gui runner still crashes, but I don’t use that. I run testdriven.net from inside vs2010 for my interactive tests. I needed the console to work so I can get it set up Cruise Control.

  3. Christian says:

    From porting the project over from Silverlight 3, there way an old version of Microsoft.Silverlight.Testing assembly kicking around in a bin directory – which made the project work happily, unless of course you’re working on a different computer than mine… D-uh. Inside the Microsoft.Silverlight.Testing namespace, there have been some breaking changes (an interface ITestHarness has been removed); and referencing this type caused a TypeLoadException. The project has been fixed; NUnit.Silverlight.Metadata is now version 1.0.1 and does not reference that interface any more. Sorry for the screw-up.

    The NUnit unit test provider should now be fit for all means and purposes, including the browser UI runner.

    • jonathan says:

      Hey christian, you mentioned that the problem has been fixed? i’ve been using your recent 2.5.5 release and i’m hitting the same error as Pete is with the GUI runner.

      • Christian says:

        Hmm, interesting. I think I can positively say that I myself got the same problem and fixed it, but haven’t heard from Pete afterwwards. I’m currently in Vietnam without much time the laptop I’ve got the project on at hands; I’ll try to repro when I get home.

        In order to see that I can or can’t get the exact same problem as you: can you send me a call stack?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: