Wednesday, November 16, 2011

Unit Tests: Part Deux

Oh well, it's been a while, I know... sorry for that.


Like promised this post here is going to check show how to implement unit tests.


I created a simple project with 2 classes, one of them is implementing an interface. Why interfaces ? Because we are going to use what is called mocking, which will ensure that you will indeed test each units of your code and not many at the same time (which is called an integration test). It is the simplest scenario possible, but understanding it will help you a lot as a programmer.


The Setup
I used Visual Studio 2010 as my IDE. Since I'm using .NET 4, my test framework is NUnit (http://www.nunit.org/) and my mock library is Moq (http://code.google.com/p/moq/). Download these two tools and install them (or extract them somewhere you will find afterward). When you create your test project, link these two libraries to the project (and don't forget to link the project(s) you want to unit test.


The code !!
The project structure is simple:
  • IPingTool: An interface with only one method, void Ping();
  • PingTool: A concrete implementation of the IPingToolInterface
  • Server: A simple class that represents a monitored server.


The definition for IPingTool is simple:



And its concrete implementation is as simple:

Now, how do we test it ? Here is a simple guideline: You can test many "success" at the same time, but only one failure at a time. In our case, it's easy, it either works or not. That gives us two test cases:

Voilà ! You have unit tested a class using NUnit. Now, how about some quick explanations ? 

  • The [TestFixture] attribute tells NUnit that this class is a Test class.
  • Each method with a [Test] attribute is... a test ! 
  • Assertions are used to check your values, A LOT of methods are available for you
  • See that [ExpectedException] attribute, it speaks for itself, because sometimes, you do want to throw an exception

Now.. for the other class. The server class is simple:


Let's say this class is your monitored server and every now and then you send a reset command if your server is unreachable. Now.. the testing phase. If anyone thought that to test the class you have to use a PingTool is heading for the wrong way ! Read the code below and you will get the trick:



Yup, instead of using a real PingTool, we fool the Server object by giving him an object we can absolutely control and he won't notice ! This is what I meant by unit testing, we are now sure that each classes is tested separately. A mock object can be setup with a lambda to manage the behavior of each functions (and methods).


That's it folks ! Have fun unit testing.

No comments:

Post a Comment