September 5, 2010

How to install and run NUnit in .Net

Being new to the agile world, all of these new tools can be overwhelming. One of the first things you may want to do as an developer is write a test. As developers, we are curious about how good our code is, but how do you write a test? What tools do you use in the .Net environment? By the end of this tutorial, I will show you how to install, setup, and use NUnit to write your very first test. Once you learn how to write these tests, you will realize just how easy it is to test your code.

Prerequisites:

In order to follow along in this tutorial, you will need the following
  • Visual C# 2010 Express (any version Visual Studio 2010 should do)
  • NUnit (Please note that in order to get the NUnit client to run, you need to have .Net Framework 2.0 installed. It won’t matter if you are using a different version of the framework for your development.)

NUnit Installation:

To install NUnit click on the “.msi” file from the download page, link above, and double click the file and open the file once it’s been downloaded. The “Complete” setup type will be fine. Everything else is pretty much your standard installation. Once the installation is complete, the necessary DLLs will be on your computer and you will be ready to incorporate them into your test project.

Writing NUnit Tests:

Writing a test with NUnit is fairly straight forward. Lets start with the following method called “AddTwoNumbers”.
namespace NUnitTutorial
{
   public class MathObject
   {
      public int AddTwoNumbers(int num1, int num2)
      {
         int result = num1 + num2;
         return result;
      }
   }
}
What you can see is that the method simply adds two numbers together and returns the result. Lets write an NUnit test to make sure this method is returning what we expect. We need to do the following to write a test:
  1. Add a new class library project to your solution. This new project should have the following naming conventions “ProjectUnderTest_Test” where “ProjectUnderTest” is the name of the project that contains the methods you are writing the tests against. image This project will contain all of your tests. Above you will notice that I have a project called “NUnitTutorial” that contains the “AddTwoNumbers” method and a project called “NUnitTutorial_Tests” that will contain the unit tests for that method.
  2. Next add the following DLL to the test project “nunit.framework” image
  3. Write our test.
To write the test we first need to add a using for the project under test (NUnitTutorial) as well as the NUnit framework (NUnit.Framework).
using NUnit.Framework;
using NUnitTutorial;
Now lets add our test to the MathObject_Tests.cs file. We first need to let NUnit know that this class contains tests that need to be ran. To do this we mark the class with the following attribute “[TestFixture]”.
using NUnit.Framework;
using NUnitTutorial;

namespace NUnitTutorial_Tests
{
     [TestFixture]
     public class MathObject_Tests
     {
     }
}
Now, lets add the test method “AddTwoNumbers_returns_3_when_given_1_and_2”. (I always like to name my test methods this way so that I know what method they are testing as well as what the expected inputs and outputs are.) Once we add this method we need to add the attribute “[Test]” to it so that NUnit knows that this is a test to be ran. Note, that all test methods should be void methods, not return anything.
using NUnit.Framework;
using NUnitTutorial;

namespace NUnitTutorial_Tests
{
  [TestFixture]
  public class MathObject_Tests
  {
       [Test]
       public void AddTwoNumbers_returns_3_when_given_1_and_2()
       {            
       } 
  }
}
All that’s left is to call the method with our two inputs and assert that the result from the method is what we expect.
using NUnit.Framework;
using NUnitTutorial;

namespace NUnitTutorial_Tests
{
  [TestFixture]
  public class MathObject_Tests
  {
    [Test]
    public void AddTwoNumbers_returns_3_when_given_1_and_2()
    {
       MathObject mObject = new MathObject();
       int calculatedResult = mObject.AddTwoNumbers(1, 2);
       int exptResult = 3;
       Assert.AreEqual(exptResult, calculatedResult);
    }
  }
}
The above test simply calls the “AddTwoNumbers” method, giving it a 1 and a 2 and asserts that the result from the method is what we are expecting, which is a 3. Now, that we finished writing the test, we need to run it. Build your solution and generate those DLLs.

Running NUnit:

Start up the NUnit client by going to Program Files –> NUnit x.x.x –> NUnit. Once it is up and running, click on File->Open Project and navigate to where the DLLs generated from the test project are located. You want to load the test project DLL into NUnit. image Once the DLL is loaded, it will show you the tests that are in that DLL. image From here all you need to do is click the “Run” button and NUnit will run your tests and output the results. image As you can see, our test passed. Now what would have happen if the test had failed? Let’s find out. Lets change our expected result to be 4 and see what happens. image There you go all the information you need to get NUnit installed and on your way to creating tests and rock solid code.

13 comments:

  1. Thanks!
    It's very clear explanation of NUnit testing for beginners.

    .net developer
    Alex

    ReplyDelete
  2. Cheers for the tutorial.

    Regards.

    ReplyDelete
  3. Good example Jerrel, many thanks. I started off by creating NUnitTutorial as an executable not a class library. That failed during the NUnit test run, then I thought why am I testing against a .exe, I want to test little sections in a dll class library. My mistake was a good learning exercise.

    ReplyDelete
  4. This is a great tutorial. I have used SharpDevelop and still was able to go through all the tutorial easily since it was written in a clear manner.

    ReplyDelete
  5. Very Useful and clear..Thanks to the Author...

    ReplyDelete
  6. Nice tutorial. I did find a missing step, which should be done between steps 2 and 3: Add a reference to NUnitTutotial to NUnitTutorial_Tests.

    ReplyDelete
  7. Nice article, it carries heaps of useful information about NUnit testing in .Net.

    ReplyDelete
  8. Great article.
    Thank you.

    ReplyDelete
  9. Great article for beginners like me. Thanks.

    ReplyDelete
  10. It was good , as reported by Another anonymous , that missing step should be added.

    ReplyDelete
  11. We have to incorporate it only in test project template or any other template?

    ReplyDelete
  12. Very good, just add that missing step and it would be perfect! Thanks for writing this tutorial.

    ReplyDelete
  13. Thanks Jerrel :) Awesome piece for beginners, it actually encouraged me to dig deeper into test based development.
    Just want to add a little useful information, to add the nunit.framework reference in Visual Studio 2013, you'll need to follow some extra steps that are given here

    http://blogs.msdn.com/b/africaapps/archive/2013/02/26/test-driven-development-using-nunit-natively-in-visual-studio-2012.aspx

    ReplyDelete