Testing takes up a lot of development time, usually exceeding value of 50% of total time so it's important to use the right tools for it.
NUnit is an open-source unit testing tool for .NET languages. If you're used to program in Java you probably encountered well-known JUnit tool, NUnit is its counterpart for .NET environment. Of course testing is one of the most important parts of creating a good software so you should do this as thoroughly as possible. Testing takes up a lot of development time, usually exceeding value of 50% of total time so it's important to use the right tools for it.
Firstly let me explain what unit testing is - it's a method where you test small units (the smallest, independent from others, parts of software that can be tested) if they work the way they were intended to. Nowadays in languages that are object-oriented these small units are usually classes. The main advantage of such tests is that you can detect errors in early stages of development. What's more these errors are usually quite easy to deal with because they are isolated from the rest of the code. Of course there are some drawbacks of unit testing. Because we test only the units of code and not the whole there can be still errors in integration of all these units so you should apply later also other kinds of tests.
To download NUnit software go to the following website: http://www.nunit.org/index.php?p=download and download the newest version of NUnit. The installation itself is very straightforward so there should be no problems with it.
Now open Visual Studio, create a new project and name it NUnitTests, it doesn't matter what project you will choose because we'll just create a new class and perform unit tests on it. Then right-click on the name of your project in Solution Explorer window and choose "Add Reference". In the window that will be opened go to "Browse" tab and find nunit.framework.dll file. It should be in the following directory: C:\Program Files\NUnit 2.5.10\bin\net-2.0\framework.
Before we'll create tests we have to create a class that we will test. So in order to do so right-click on the project name in the Solution Explorer window on right and choose Add - New Item. Then find a Class on list, name it Calculator and click Add button. In this class we'll insert methods that will add or subtract two numbers. In the beginning we'll create just one method for addition:
Now we'll create a second class to test Calculator. So again right-click on the name of the project in Solution Explorer and add class CalculatorTest. After you'll create this class add the following reference in the first part of this file:
In the beginning of each class that is used by NUnit for testing you should add the following attribute:
And in the beginning of each method that is used for testing add:
One of the most useful methods in NUnit are Assert.AreEqual, Assert.AreNotEqual and Assert.AreSame, Assert.AreNotSame. The first ones check if object values are equal (not equal) and the second ones check if objects refer (or not) to the same object. There are of course more options but once you know the general rule it will be easy for you to use them.
Let's add the following code to our test class:
Now let's build this solution to create exe file. It will be in the directory: NUnitTests\bin\Debug. To test it open NUnit program and go to File menu, choose "Open project" and find a file that was generated by Visual Studio. Now all you have to do is just click Run button to perform prepared tests, the result should be the same as in the picture below - all tests passed:
Now let's create a second method in Calculator class. This method will subtract two number, but we'll emulate a glitch there, if the result will be smaller then zero we'll return 0 instead of the proper value:
Let's create a test methods for this class, in order to do this go to CalculatorTest file and add the following piece of code there:
Try to run these tests in NUnit, as you can see there is now one failed test.
Now repair Subtract() method in Calculator class so that it will return actual result and run test in NUnit - all tests again will be successful.
As you can see using NUnit is relatively easy and it can discover potential errors in your software. Although testing is rather hard and arduous task, with tools such as NUnit it can be a bit easier and more agile. Remember that unit tests have to be well prepared so they check all possibilities where you can see potential source of errors. After unit testing you should also apply another method of testing to check your software as a whole, so you can almost be sure that there are no errors in integration of all units of your program. I wrote "almost" because in reality you can never be 100% sure if there are no errors in your code.