TechyMedia

A .NET Technology blog | .NET,C# Progamming,C# tutorials,WPF WCF|Software Engineer

NAVIGATION - SEARCH

nUnit - Simple Tutorial

Unit tests are essential since code refactoring and optimizations are inevitable part of software system life cycle. nUnit is a unit testing framework for all Microsoft .Net compatible languages. In nUnit Attributes are used to get information about a .NET test assembly. Test Fixture, Test , SetUp and TearDown are major attributes used in nUnit.

nUnit is a unit testing framework for all Microsoft .Net compatible languages.In nUnit Attributes are used to get information about a .NET test assembly. The nUnit Test Runner application loads the assembly and scans for specific attributes which specifies which classes and methods are tests. Once identified the runner execute those test methods using reflection. So decorating with proper attributes will do most of the work for you in nUnit. There are various attributes in nUnit that you will be using during the course of your nUnit test coding. Test Fixture, Test , SetUp and TearDown are some of them.

Conventional way of writing nUnit test cases are to have a separate test project and refer the code assembly to be tested.It is also possible to write the nUnit test code along with the main assembly to be tested but not a good practice due to unwanted complexities both to test and main deliverable code.

It is possible to have an nUnit test code as a dll or exe. But preferred and suggested way is to have a class library(dll) since it doesn’t make any sense to have a standalone test code application which runs by nUnit test runner.

nUnit Attributes

As metioned above nUnit uses various  Attributes to indicate testing information. Following are the key nUnit attributes which you will be using during the course of your nUnit test coding.

TestFixture

Every class containing nUnit test cases need to mandatorily have the TestFixture attribute  to specify that the class contains test methods. The test runner application will then identify this as a test class and scan it for test methods. The class with TestFixture attribute needs to be public with a default public constructor. See the sample below.

[TestFixture]

Public class UnitTest

{

}

Test

Every test case method within a test fixture should have a Test attribute to specify that as test function to run by the Test Runner application. Mandatory that the method should be public .Also the method would not take any parameter and the return type should be void as mentioned below.

[TestFixture]

Public class UnitTest

{

[Test]

Public void TestMethod()

{

//Test case code goes here

}

}

SetUp

Most of the time you have to do some common activities before execution of any nUnit Test function like declaring and initializing some variables instances and some other dependent objects etc. SetUp attribute can be used for this purpose.That is instead of creating private variables and calling it from each and every test method  we can use the Setup attribute to specify that it should be executed before every method execution .

[TestFixture]

Public class UnitTest

{

Private Employee employee;

[SetUp]

Public void SetUp()

{

employee = new Employee();

}

[Test]

Public void TestMethod()

{

//Test case code goes here

}

} 

TearDown

Similar as SetUp you have to do a some common activities() after execution of any nUnit Test function TearDown attribute can be used.That is instead of doing it from each and every test method  we can use the TearDown attribute to specify that it should be executed after every method execution .

[TestFixture]

Public class UnitTest

{

Private Employee employee;

[SetUp]

Public void SetUp()

{

employee = new Employee();

}

[TearDown]

Public void TearDown()

{

employee = null;

}

[Test]

Public void TestMethod()

{

//Test case code goes here

}

} 

How to set up and Run nUnit

Firstly download nUnit from  website here.

After downloading and installing the nUnit Framework you need to refer the nUnit Framework library (NUnit.Framework) to the test project to start your nUnit coding.

Use the nUnit framework namespace in the test classes. ( using NUnit.Framework )

Once the tests are created build it successfully.

Then go to nUnit installation folder and launch the nUnit exe.

Select the previous built Test project Library . Once loaded just click the Run button. That’s all.

Test Cases

In nUnit every test function mandatorily needed to be attributed with the attribute  [Test].  See an example test case below.

[Test]

public void SampleTestCase() 

{

int result == 100;    // Hard coded value will be replaced with function call in actual scenario.

Assert.AreEqual(100, result, "Test Passed");

Assert.AreEqual(90, result, "Test Failed");

}

Pingbacks and trackbacks (1)+

Add comment

  Country flag

biuquote
  • Comment
  • Preview
Loading