NUnit Core Concepts…

I started learning NUnit. Currently I am reading the book Test-Driven Development in Microsoft .NET by James W. Newkirk ,Alexei A. Vorontsov. I have finished reading and working through examples from a couple of chapters . I would recommend this book for who wish to get started in Test Driven Development. I was of the thought that getting started in NUnit would be a difficult task. But the truth is otherwise. Its easy. Here are the some of the basics and core concepts about NUnit to get started

This attribute is to specify the class that contains the test case. This attribute is placed before the classes that contain test code.
using System;
using NUnit.Framework;
namespace NUnitCoreConcepts
{
[TestFixture]
public class CoreConcepts()
{

}
}

The [Test] attribute specifies the Test Methods inside the Test Class. The Test Methods are the methods that contains code for testing.

using System;
using NUnit.Framework;
namespace NUnitCoreConcepts
{
[TestFixture]
public class CoreConcepts
{
[Test]
public void AdditionTest()
{
int num1 = 3;
int num2 = 4;
Assert.AreEqual(7, num1 + num2);
}
}
}

In NUnit, Assertions are the way to determine whether the TestCode in the Test method passed or failed. In the above example Assert.AreEqual() method verifies the actual and expected and says whether the test passed or not. There are many static methods in Assert class. Some of them are
Assert.AreEqual()
Assert.IsTrue()
Assert.IsNull() …

The [Setup] attribute specifies the method that has to be executed before every Test method is executed. It normally contains initialization code.
The [TearDown] attribute specifies the method that has to be executed after the Test method is executed. Its normally contains code to release the resources.
using System;
using NUnit.Framework;
namespace NUnitCoreConcepts
{
[TestFixture]
public class CoreConcepts
{
private int num1 ;
private int num2 ;
[SetUp]
public void Init()
{
num1 = 3;
num2 = 4;
}
[Test]
public void AdditionTest()
{
Assert.AreEqual(7, num1 + num2);
}
}
}

ExpectedException attribtue specifies that executing the test code would raise an exception. See the example below

using System;
using NUnit.Framework;
namespace NUnitCoreConcepts
{
[TestFixture]
public class CoreConcepts
{
[Test]
[ExpectedException(typeof(DivideByZeroException))]
public void DivideByZeroExceptionTest ()
{
int zero = 0;
int num = 100;
int result = num / zero;
Assert.Fail(“We are failing the test if it doesn’t raise an divide by zero exception”);
}
}
}

Write some test code using NUnit and you can start using it in your projects right away.

Advertisements

Author: azagappan

Hi.. I am Azagu. I live in Chennai, Tamil Nadu, India. I am a .NET developer. You can reach me at azagappan at gmail.com

1 thought on “NUnit Core Concepts…”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s