Home > C#, Unit Testing > Unit testing using NUnit in Visual Studio 2010

Unit testing using NUnit in Visual Studio 2010

What is Unit testing?

Unit testing is a method by which individual units of source code, sets of one or more computer program modules together with associated control data, usage procedures, and operating procedures, are tested to determine if they are fit for use. Intuitively, one can view a unit as the smallest testable part of an application. Let me take the example of the ‘Calculator’  for demonstrating the unit testing.

SOFTWARE REQUIREMENTS:

  • Windows 7 as OS
  • Microsoft Visual Studio 2010 as IDE
  • NUnit as Unit Testing Tool

SET-UP INSTRUCTIONS:

  • Create a new Project with C# Class Library and save it (‘Calculator’ in my example).
  • Rename the class ‘Class1.cs‘ to ‘Operator.cs‘.
  • Add the NUnit frameworks into the ‘Solution Explorer‘ by context-clicking on the project name and then select ‘Add Reference’ menu item.
  • When the Add Reference dialog appears, click on ‘Browse‘ and navigate to C:\Program Files (x86)\NUnit 2.5.10\bin\net-2.0\framework and select nunit.framework.dll.
  • Finally, the solution Explorer should look like this:

Create and writing scenarios for Unit tests:
  • Create scenarios for testing the operators such as Add, Subtract, Multiply, Divide, Square Root and Reciprocal.
  • Copy and paste the following code into the Operator.cs file in VS 2010.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
namespace Calculator
{
 public class Operator
 {

 public static double Add(double a, double b)
 {
 return a + b;
 }

 public static double Subtract(double a, double b)
 {
 return a - b;
 }

 public static double Multiply(double a, double b)
 {
 return a * b;
 }

 public static double Divide(double a, double b)
 {
 return a / b;
 }

 public static double SquareRoot(double a)
 {
 return Math.Sqrt(a);
 }

 public static double Reciprocal(double a)
 {
 return 1 / a;
 }

 [TestFixture]
 public class TestClass
 {

 [Test]
 public void AdditionTest()
 {
 double result = Operator.Add(2, 4);
 Assert.AreEqual(6, result);
 }

 [Test]
 public void SubtractionTest()
 {
 double result = Operator.Subtract(1, 3);
 Assert.AreEqual(-2, result);
 }

 [Test]
 public void MultiplicationTest()
 {
 double result = Operator.Multiply(4, 3);
 Assert.AreEqual(12, result);
 }

 [Test]
 public void DivisionTest()
 {
 double result = Operator.Divide(3, 0);
 Assert.AreEqual(0, result);
 }

 [Test]
 public void SquareRootTest()
 {
 double result = Operator.SquareRoot(-4);
 Assert.AreEqual(2, result);
 }

 [Test]
 public void ReciprocalTest()
 {
 double result = Operator.Reciprocal(0);
 Assert.AreEqual(0, result);
 }
 }
 }
}

—————-

Running the tests using NUnit:
  • In Visual Studio, Go to ‘Projects -> Project Properties’ (‘Calculator Properties’ in my example).
  • Click on the ‘Debug’ tab.
  • Set the ‘Start external program‘ to the location of NUnit exe file (C:\Program Files (x86)\NUnit 2.5.10\bin\net-2.0\nunit-x86.exe).
  • Build the solution, Go to ‘Build -> Build Solution‘ (hit the F6 key) in Visual Studio.
  • Execute the test, Go to ‘Debug -> Start Debugging’ (hit the F5 key) in Visual Studio. Visual Studio invokes the NUnit application.
  • In NUnit, click on ‘File -> Open Project‘ and choose the location of the Calculator.dll file as shown in the snapshot below.
  • In NUnit, click on the ‘Run‘ button to run the tests. Test is executed using NUnit and the result will be displayed in NUnit GUI window as shown in the snapshot below.

Only 3 tests are passed out of 6. Lets look into each of the failed tests – DivisionTest, ReciprocalTest and SquareRootTest.

Firstly, let us try with DivisionTest. When we divide 3/0, the calculator must display “Cannot divide by zero”. So, let us write an Exception error for Divide method.

public static double Divide(double a, double b)
{
 if (b == 0)
  {
  throw new InvalidOperationException("Cannot divide by zero");
  }
  return a / b;
}

Secondly, let us try with ReciprocalTest. When we reciprocate 0, the calculator must display “Cannot divide by zero”. So, let us write an Exception error for Reciprocal method.

public static double Reciprocal(double a)
{
if (a == 0)
{
throw new InvalidOperationException("Cannot divide by zero");
}
return 1 / a;
}

Thirdly, let us try with SquareRootTest. When we perform the square root operation for -4, the calculator must display “Invalid Input”. So, let us write an Exception error for SquareRoot method.

public static double SquareRoot(double a)
{
if (a < 0)
{
throw new InvalidOperationException("Invalid Input");
}
return Math.Sqrt(a);
}

Now, add valid numbers to each of the failed tests, build the code and run the test using NUnit. Final contents for the ‘Operator.cs‘ is as shown below:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
namespace Calculator
{
 public class Operator
 {

 public static double Add(double a, double b)
 {
 return a + b;
 }

 public static double Subtract(double a, double b)
 {
 return a - b;
 }

 public static double Multiply(double a, double b)
 {
 return a * b;
 }

 public static double Divide(double a, double b)
 {
 if (b == 0)
   {
   throw new InvalidOperationException("Cannot divide by zero");
   }
   return a / b;
 }

 public static double SquareRoot(double a)
 {
 if (a < 0)
   {
   throw new InvalidOperationException("Invalid Input");
   }
   return Math.Sqrt(a);
 }

 public static double Reciprocal(double a)
 {
 if (a == 0)
   {
   throw new InvalidOperationException("Cannot divide by zero");
   }
   return 1 / a;
 }

 [TestFixture]
 public class TestClass
 {

 [Test]
 public void AdditionTest()
 {
 double result = Operator.Add(2, 4);
 Assert.AreEqual(6, result);
 }

 [Test]
 public void SubtractionTest()
 {
 double result = Operator.Subtract(1, 3);
 Assert.AreEqual(-2, result);
 }

 [Test]
 public void MultiplicationTest()
 {
 double result = Operator.Multiply(4, 3);
 Assert.AreEqual(12, result);
 }

 [Test]
 public void DivisionTest()
 {
 double result = Operator.Divide(4, 2);
 Assert.AreEqual(2, result);
 }

 [Test]
 public void SquareRootTest()
 {
 double result = Operator.SquareRoot(4);
 Assert.AreEqual(2, result);
 }

 [Test]
 public void ReciprocalTest()
 {
 double result = Operator.Reciprocal(2);
 Assert.AreEqual(0.5, result);
 }
 }
 }
}

Advertisements
  1. November 13, 2012 at 1:18 pm

    Very nice article.Couldn’t be simpler. Thanks!

  2. November 13, 2012 at 1:19 pm

    Reblogged this on My private-public notes and commented:
    Simple How-To for NUnit in Visual Studio

  3. Med Amine Farkh
    January 21, 2013 at 9:29 pm

    Thanks very simple and very useful for a beginner like me

  4. madhu
    April 26, 2013 at 6:28 am

    thanks very simple and useful to configure and run nunit

  5. June 13, 2013 at 12:13 am

    It’s difficult to find educated people for this subject, but you sound like you know what you’re talking about!
    Thanks

  6. Shazee
    July 1, 2014 at 11:24 pm

    Thank You so much It was of great help. 🙂

  7. Kiran
    July 14, 2014 at 6:55 pm

    Hi .. please suggest me if selenium , c# .net works in Visual studio 2012 and with Nunit..I am unable to connect Excel spreadsheet through NUNIT.

    • July 17, 2014 at 10:17 am

      Yes, Visual studio 2012, NUnit, Selenium works with Excel.

  1. No trackbacks yet.

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

%d bloggers like this: