Testing project using NUnit


Nunit is a testing framework for all the .NET languages.

For our note that Nunit is completely written in C#. You can download the Nunit framework from http://www.nunit.org/index.php?p=download .The latest version currently available in Nunit is Nunit 2.2.5.

Now open a new project, add a reference of NUnit.framework.dll. Now you can import the namespace of Nunit, NUnit.framework. I have created a class called NunitTest.

We also need to let the Nunit framework know that this class is a fixture, so we simple add a [TestFixture ()] attribute on top of the class name.

[TestFixture ()]
public class NUnitTest
{
}

Now let us go to method scope. Before a method, use the attribute called [Test ()]

A test in a fixture is defined as a public method that returns void and accepts no parameters, marked with the [ Test()] attribute and with one or more assertions inside.

I'm creating a method as follows...

[Test()]
public void Divide()
{
         int a=100;
         int b=0;
         int c=a/b;
}

Compile and run the application. Now go to Nunit Gui, open the dll file of your project.

You will get the one as shown below for my project

Now click on 'Run' button in the Nunit Gui. You will get the output as shown below.

Thus you could keep track of the exceptions you get in the project. But you have to note that if the above method is handled using Try and Catch statements, you wouldn't have got error in the Nunit.It is shown in the method DivideHandled.

[Test()]

public void DivideHandled()

{

      try

      {

          int a=100;

          int b=0;

          int c=a/b;

      }

      catch(DivideByZeroException ex)

      {

      }

}

Look at the picture below

 

Now we are going to use the Ignore Attribute of the Nunit. It accepts reason as parameter that will be displayed in the Nunit.

[Ignore("No code in this method and is ignored")]
[Test()]
public void IgnoreThisMethodForTest()
{
}

Look at the output below

 

Now we look at the Assert attribute of the Nunit. It contains only static methods that allow you to make sure that a value is not null, equals other values and just send in any Boolean expression you like. You can also send in messages that explain the meaning of this failure.

[Test()]
public void TestAssert()
{
    int a=10;
    int b=20;
    int c=a+b;
    Assert.AreEqual(25,c, "The numbers are'nt equal");
}

Let me show u the output in Nunit when testing the above method

Like the 'AreEqual' methods for the Assert class, there are also methods like AreNotEqual, IsNull, Fail etc.

Some of the other common attributes are

  • You can have a [Setup] and [TearDown] method inside your fixture. The [Setup] runs before each test in the current fixture is run, and the [TearDown] runs after each test.  These methods are very useful for when you want all your tests to use the same set of clean initialized data. In there you can initialize global variables, delete or create needed files and so on. This of [Setup] as an implicit contructor for each test, and have as a destructor for it. Methods that are marked by these attributes should not be marked as tests as well!
  • You can have a [ TestFixtureSetUp ] and [ TestFixtureTearDown ]  methods as well. These methods will be run only once for each test fixture tested. Use them for global initialization and cleanup of resources that can be shared by all tests in that fixture.
  • Another excellent attribute we get is the [ExpectedException] attribute. When a test method is decorated with this attribute and no exception of the type specified in the attribute is thrown inside the test, the test has failed.


Similar Articles