Friday, June 24, 2011

NUnit Sample Application

Steps to use NUnit

1) First download current version of NUnit here.

2) Install it in your machine.

3) Create one sample Class Library in visual studio,and name it Calculations.

4) Now, we just write some small caluculation methods like Addition and Subtracton etc..

Code(cs file) will be like this

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Calculations
{
public class Calc
{
private int sum;
private int subtr;

public int Addition(int a, int b)
{
sum = a + b;
return sum;
}
public int Subtraction(int a, int b)
{
subtr = a - b;
return subtr;
}
static void Main(string[] args)
{
}
}
}


5) Then Create another class library, where we can write the test cases for the above methods.

Let us assume it as a NunitSampleTest.cs

a)For this Class library we need to add nunit reference.

If you have installed NUnit in C drive, You can find this reference from the below path
C:\Program Files\NUnit 2.5.10\bin\net-2.0\framework\nunit.framework.dll

b)Then we need to add Calculations referce.

Now we need to write the test cases as below

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Calculations;
using NUnit.Framework;

namespace NUnitSampleTest
{
[TestFixture]
public class NUnitTestClass
{
[Test]
public void AdditionTest()
{
Calc newobj = new Calc();
int sum = newobj.Addition(20, 10);
Assert.AreEqual(30, sum);
}
[Test]
public void SubtractionTest()
{
Calc newobj = new Calc();
int subtr = newobj.Subtraction(20, 10);
Assert.AreEqual(10, subtr);
}
static void Main(string[] args)
{
}
}
}

[Note: In the above code we can find out two new attributes, i.e TestFixture and Test and one class Assert.
The TestFixture attribute is specified for the classes, When we attach this attribute to a class, the Test Runner application will scan it for test methods.
The only restrictions on classes that use the TestFixture attribute are that they must have a public default constructor (or no constructor which is the same thing).

The Test attribute marks a specific method inside a class that has already been marked as a TestFixture, as a test method.

Assertions are central to unit testing. The Assert claas provides a variety of static methods, using which we can test the result with the actual expected value.]

this all completes the codeing part

Now to test this application, we need to open NUnit UI, this we can find out at the below path
(C:\Program Files\NUnit 2.5.10\bin\net-2.0)
or
Goto start->All Programs ->NUnit 2.5.10


The window will be like as shown below




Now Goto File->open Project , then Go to the path and click on the NunitSampleTest.Dll



It will open the methods as shown below..





so right click on the individual methods or whole class and click on Run



If all the test cases are passed successfully, then we can see the green color Line as shown below




But if get Red color line, then it means TestCase is failed.



The above figure indicates that we have an issue in Subtraction Method..




Note: To Make things simpler we have hard coded the values in the TestCase methods.
Rather than hard coding the values, we can create one seperate class with values and can pass them to respective methods.