A short guide to NUnit

Installation

Download and install NUnit from here.

Writing tests with NUnit

In NUnit we have Test Fixtures containing Tests. A Test Fixture is the class that contain the tests we want to run. We typically write one test fixture for each class we want to test. As a convention we name the test fixture <Class to be tested>Tests.cs. e.g. test fixture for Simple.cs would be SimpleTests.cs and so on. A test fixture will contain a list of tests, typically one for each method in the class being tested.

Given below is a typical test fixture.

--------------------------------------------------------------------------------------------

using System;
using NUnit.Framework;


namespace example {
    /// <summary>
    ///
Summary description for SimpleTests.
    /// </summary>
   
[TestFixture]
    public class SimpleTests {
       [SetUp]
      
public void SetUp() {
          _mySimple = new Simple();
          // put other startup code here
       }

       [TearDown]
      
public void CleanUp() {
          _mySimple.Dispose(); // assumes Disposable is implemented
          _mySimple = null;
          //put other cleanup code here
       }

       [Test]
       public void TestAddPositiveMethod() {
          Assert.AreEqual(_mySimple.AddPositive(2,3),5);
          Assert.AreEqual(_mySimple.AddPositive(0,3),3);
          Assert.AreEqual(_mySimple.AddPositive(2,0),0);
          Assert.AreEqual(_mySimple.AddPositive(0,0),0);
       }

       [Test]
       [ExpectedException(typeof(Exception))]
       public void TestAddNegative(){
            Assert.AreEqual(_mySimple.AddNegative(-5,0),-5);
        }

       [Test]
       [Ignore("Implementation not complete yet.")]

       public void MyThirdTest() {
          // your test implementation goes here
       }

       private Simple _mySimple = null;
    }
}

--------------------------------------------------------------------------------------------

and here's the Simple.cs class being tested.

--------------------------------------------------------------------------------------------

using System;
using System.Diagnostics;

namespace example{
    /// <summary>
    /// Summary description for Simple.
    /// </summary>
    public class Simple{
 

    public Simple(){}

    public int AddPositive(int arg1, int arg2){
        Debug.Assert(arg1> -1 && arg2 > -1);
        return arg1 + arg2;
    }

    public int AddNegative(int arg1, int arg2){
        throw new Exception("not implemented yet");
    }

    public void Dispose(){}

    }
}


--------------------------------------------------------------------------------------------

Note that this scenario is for illustration only and hence too simplistic.

Let us analyze the SimpleTests class first. The [TestFixture] attribute at the beginning indicates that this class is a test fixture so that NUnit can identify it as a runnable test class. Similarly NUnit uses attributes to indicate various properties of classes/methods. Then you see two methods tagged [SetUp] and [TearDown]. SetUp method is run at the start of test fixture and TearDown method is run at the end, after running all the test cases in the test fixture. Each method is run only once per entire test fixture. Note that these methods are optional. You may leave out these methods in your test fixtures, if you wish.

This test fixture has 3 tests. First one is used to test AddPositive() method of the Simple class. Note how we have used Assert.AreEqual method to assert that we get the expected result from the method for different input values. Assert class (part of NUnit) has many more useful methods we can use for different type of verifications. Also note that we haven't checked the method for negative input values. That is because we have used Debug.Assert method of C# (not related to NUnit) inside Simple class to ensure that parameters are always positive.

Second test illustrates how we can check whether a method throws an exception as expected.

Third method is to be ignored by NUnit. i.e. NUnit will not run this test.

Compiling the Program

Create a DLL C# project using VS.Net 2005. Add two classes corresponding to the code snippets above.

To compile the above program, you'd need to add a reference to 'nunit.framework dll'. To do so right click the 'References' tab of your project in the 'Solution Explorer'. Choose 'Add References' as shown below. 

 

The above dialog box will appear. Choose 'nunit.framework' and click OK. Build the solution by Ctrl+Shift+B.

Running NUnit

After compiling the above code into a DLL, we can test it using NUnit. Open NUnit GUI - it can be accessed from 'Windows Start menu > All Programs > NUnit'. The initial snapshots of NUnit GUI is as follows.

From the menu bar, choose 'File>Open'. Select a DLL or executable to test.

In case there are failures, NUnit will indicate which test case went wrong and where, similar to the screen shot given below. Red for the failed test cases, yellow for the tests not run and green for the successful test cases.

 

Integration with VS.Net 2005

Integration allows you to run NUnit from inside VS.Net 2005. To do so, perform the following steps:

1. From NUnit GUI choose 'Tools>Options' from the menu bar. On the dialog box that appear select the 'Tests' tab. Click the 'Enable Visual Studio Support' checkbox.

2. At VS.Net 2005 choose 'Tools>External Tools...'. A dialog box will appear. Add a new entry as shown below. At the 'Command' text box, browse to the nunit-gui.exe at the directory where you install NUnit (usually at C:\Program Files). Enter '$(TargetPath)' and '$(TargetDir)' in the 'Arguments' and 'Initial directory' text boxes respectively. Click 'Apply' and 'OK'.

3. To invoke NUnit, simply choose 'Tools>NUnit' from the menu bar.

This should be enough to get you started with NUnit.  However, to put it into real use, you may need to read the full documentation available at http://www.nunit.org/

 


Last modified by Yinxing (Feb'09). Previous authors: David Lo, Damith, Beatrice Luca.