Nunit - A Unit Test
Nunit - A Unit Test
Features
NUnit is simple to understand and easy to learn.
Object Oriented.
NUnit is flexible - Test code can be in any .Net language.
NUnit uses the "attribute" feature of .NET to identify tests.
Download & Installation
http://www.nunit.org
http://nunit.org/download.html
http://sourceforge.net/projects/nunit/
Download Nunit-2.2.0.msi from http://nunit.org/download.html and
install nunit.
Add nunit.frameworks.dll to your project references. This dll resides in
nunit\bin folder.
The next few screen shots show how to add this dll file if your project
has been opened as a classlibrary .
Simple concepts
. Test fixture : a class that contains one ore more test methods
. Test method : a method that executes a specific test
. Test runner : an application that finds and executes test methods on test fixtures
. Assertion : a Boolean expression that describes what must be true when some
action has been executed
. Expected Exception : the type of an Exception we expect to be thrown during
execution of a test method
. Setup : Code that is run before every test method is executed (eg, logging in as a
particular user or initializing a singleton)
. Teardown : Code that is run after every test method has finished (eg, deleting rows
from a table that were inserted during the test)
Pros
NUnit is Open Source and highly extensible.
Platform independent.
Simple and easy to learn syntax.
When integrated with NAnt, can be used for incremental projects.
Cons
C based extensions not available.
Lack of awareness among .Net Community.
The Input & Output of nUnit
[TestFixture]
public class calcTest{
[Test]
public void AdditionTests(){
calculator cal=new calculator();
cal.Addition(5,10); //CASE 1
Assert.AreEqual(15,cal.output());
cal.Addition(-5,10); // CASE 2
Assert.AreEqual(15,cal.output());
}
[Test]
public void DivisionTests(){
calculator cal=new calculator();
cal.Division(10,2); // CASE 1
Assert.AreEqual(5,cal.output());
cal.Division(10,0); //CASE 2
Assert.AreEqual(2,cal.output());
}
}//for testfixture
How to write your test code
Suppose you want to test the functions in a class,
your testfixture(test stub) should look somewhat
like this
using NUnit.Framework;
Namespace sample1{
public class sampleclass(){
public void func1(){}
}//for sampleclass
[TestFixture]
public class sampleTest(){
[Test]
public void test1(){
testcase1;
testcase2;
}//for test1()
}//for sampleTest textfixture
}//for namespace
Calculator Example
using System;
using NUnit.Framework;
namespace ClassLibrary1
{
public class calculator
{
private int result;
cal.Addition(-5,10);Console.Write("TESTING -5+10\n");
Assert.AreEqual(5,cal.output());
}
[Test]
public void DivisionTests(){
calculator cal=new calculator();
cal.Division(10,2); Console.Write("TESTING 10div2\n");
Assert.AreEqual(5,cal.output());
Example:
nunit-console.exe classlibrary1.dll
-XML=ourfile.xml
The different options are
Nunit-console [inputfiles][options]
Options
Thank you