Nunit V.2 for People Who Can't Cook

Description:
In this article you will discover how to use how to use NUnit V.2 efficiently with your .Net projects.For your information this article was created with NUnitV.2 RC2 (which you can get here) and VS.Net 7.0.

Introduction:
Before I get going I have to say that I don’t automate the complete process anymore (yes I do have to use the mouse now for testing), since I feel the benefits of automasation are not offsetting the cost of having to modify your programming environment for every project. So the described setup will be relatively straight forward.

A lot of things have changed from Version 1 and I must say that it is now relatively easy to get an efficient workflow with NUnit2. The main advantages of NUnit 2 are:
-Now your TestFixture does not need to inherit from TestCase anymore. That means that we don’t have to write any ugly constructors anymore.
-NUnit2 can automatically find all TestCases in a compilation unit.

0.Create your project
Create a new C# project in VS.Net and now go in the “solution explorer”.

1.Add the NUnit reference

Right click on the reference line and click on add a reference.

Now click on Browse.

Navigate to your NUnitV2 directory and go into the bin subdirectory. For me this directory is in “C:\Program Files\NUnit V2.0\bin”.Select nunit.framework.dll and click OK.

 

2. Add your TestCase
Now we can finally proceed to write some code. In order to get the basic skeleton setup for NUnit you need to add two things

First you need to add the reference in your code to the NUnit namespace:

 

using NUnit.Framework;

Additionally you need to add your testing class to your source file:

[TestFixture]//telling NUnit that this class contains test functions
public class MyTestClass
{
     [Test]//telling NUnit that this function should be run during the tests
    public void TestFunction()
    {
        //add here your Assertions
    }

}
Compile your project by clicking F7.

3.Starting NUnit-GUI
OK now we will start the NUnit-GUI application which you can find under your windows programs menu or in the “C:\Program Files\NUnit\bin” directory.

Now go under File->Open and navigate to your projects “bin” directory. In other words to need to supply NUnit with the executable of your project in order to run the containing tests.

Select the exe or dll file of your compiled project (it will have the same name as your project) and click Open.

You will immediately notice that NUnit2 is looking through the complete compilation unit and finds any TestFixtures. In our case the TestFixture is called MyTestClass and the only test is TestFunction.

4.Testing with NUnit-GUI
Click on the “Run” button.

You will see a green bar which tells you that all tests succeeded. To change this let us rewrite our function to this:

[Test]//telling NUnit that this function should be run during the tests
public void TestFunction()
{
    //add here your Assertions
    Assertion.Assert(1==0);
}

Compile your project with this new code and switch to NUnit-GUI. You will notice that NUnit has noticed that the executable has changed and it has reloaded it for you. Now simply click run again.

In order to fix this broken test you would simply change the assertion and go back to NUnit-GUI and hit the run button.

OK this is already it. There is not much more to explain since the creators of NUnitV.2.0 have done a pretty good job in designing this new version.

For your information, the workflow to use NUnit2 is something like this
0.Startup NUnit-GUI
1.Write a test
2.Compile your project
3.Switch to NUnit-GUI (it will recognize automatically that your executable has changed and reload it)
4.Hit the Run button
5.Write necessary code to get test to run and go back to Step 1

Granted this is not as smooth as with NUnit1, but it is connected with much fewer hassles and feels a bit cleaner than the previous process.

If you have any questions or comments email me under kalina@octuraNOSPAM.com. (without the NOSPAM).