What are unit tests?

According to Wikipedia (http://en.wikipedia.org/wiki/Unit_testing):
In computer programming, unit testing is a method by which individual units of source code are tested to determine if they are fit for use. A unit is the smallest testable part of an application. In procedural programming a unit may be an individual function or procedure. In object-oriented programming a unit is usually a method. Unit tests are created by programmers or occasionally by white box testers during the development process.

Unit tests are typically written and run by software developers to ensure that code meets its design and behaves as intended. Its implementation can vary from being very manual (pencil and paper) to being formalized as part of build automation.
If you build an application or any non-trivial library, you won’t be able to test all functionality using only unit tests. “Pure” unit tests should verify all components in isolation, which is often difficult for parts of your code that interact with user or that communicate with external systems, such as database or web services. The best way to increase the amount of your code that can be tested is to design it correctly. You can use abstractions and patterns such as MVVM to separate your application logic from user interface and external systems. Sometimes you can also use mocking objects in your tests; these frameworks will let you replace calls that your product makes to external classes and replace them with your own implementations.

But even when you design your application very carefully and allow each component to be tested independently, unit tests won’t directly verify scenarios visible to the user. They will help you a lot with maintaining quality of your internal implementation and let you change it more easily in the future, but if you want to make sure that your application is usable to the end user, you will also have to implement other kinds of functional tests, such as integration tests and GUI tests.

Available Frameworks

For the desktop .NET Framework, there are many mature testing libraries: MSTest (supported by Microsoft), NUnit, xUnit, MBUnit, etc. Unfortunately, all of them were created before Silverlight existed and they use .NET features not available in our framework. There are some unofficial ports, but they are not stable yet and are not supported at all.
Although mentioned libraries are not yet available to Silverlight, there are available two new Silverlight-only testing frameworks:
  1. Silverlight Unit Testing Framework. This is open source project developed by Jeff Wilcox from the Silverlight team. It is shipped with the Silverlight Toolkit and available on CodePlex.
  2. SilverUnit. This is also open source project hosted on CodePlex, but which uses very different concept: instead of running tests inside browser, this frameworks intercept calls and runs them in the full .NET Framework. It also has very useful helper classes for testing visual states for controls.
  3. UnitDriven. This is a cross-platform framework for testing Silverlight, Windows Phone and regular .NET applications. Currently it is in very early stage (last release had number 0.14), so it's not ready for production yet.
Click on above links to find more information about each of these frameworks.

Which framework should I choose?

I recommend using Silverlight Unit Testing Framework. XUnit is in very early alpha stage right now, and Jeff Wilcox’s framework has currently a few advantages over SilverUnit:
  • It has much better support. It is maintained by Microsoft and released together with the Silverlight Toolkit, so it is very likely that there will be a new version for every future release of Silverlight.
  • It uses standard MSTest attributes to decorate tests. If Visual Studio adds support to Silverlight in future release, it will be much easier to move your tests to the official Microsoft testing framework.
  • It’s completely free, even for commercial use. The SilverUnit itself is also free, but it depends on TypeMock library, which is a commercial product free only to open-source projects.
  • Silverlight Unit Testing Framework executes tests directly in the target environment – in a browser or on the phone. Although creators of SilverUnit consider that as a disadvantage for pure unit tests, this is much safer approach. Silverlight can be hosted and run against desktop CLR (in fact, Visual Studio and Expression Blend use it this way), but SilverUnit is not even using that – it’s ignoring the whole native part of Silverlight. It can be appropriate for some class, but if you use SilverUnit to verify a class that somehow interacts with the framework, you are really executing different code than your users will execute.
Roy Osherove, the creator of SilverUnit, has different different opinion, and for him the Silverlight Unit Testing Framework is hard to use for even simple tests. Please watch this presentation if you want to get more information.

Last edited May 18, 2011 at 10:37 AM by _incognito_, version 3


No comments yet.