Thursday 15 May 2014

MS Unit Test vs NUnit Test

Unit Test
Unit testing is used to test a small unit of code.
MS Unit Test - Build with Visual Studio
NUnit Test - Can be downloaded from http://www.nunit.org/index.php?p=download and referred from the Visual Studio Project

MS Unit Test VS NUnit Test
MS Unit Test
NUnit Test
Take more time to run your test suite
Faster
Uses much resources
Uses Less resources
Not Integrated fully with Visual Studio (You may try integrated runners including Resharper and TestDriven.NET however, some attributes may not be supported)
Fully Integrated with Visual Studio
NUnit has frequent version updates
MS-Test has only one per VS version
Exception message assertion - can be done using attribute in NUnit
Must be done using Try-Catch in MS-Test
NUnit wins for Unit Testing
MS Test has much better abilities for integration based testing and team testing

Now let us implement NUnit and MSUnit Testing to test Addition and Subtraction methods in the following Project:
1.      Open Visual Studio Create  a Console Application named  NUnitTestSample



2.      Paste the following code in Program.cs file

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

namespace NUnitTestSample
{
    class Program
{
    static void Main(string[] args)
    {

        try
        {
            // Get two numbers
            Console.WriteLine("Enter two numbers \n");
            Console.WriteLine("First number --> Press Enter --> Second number --> Press
            Enter\n");
            int number1;
            int number2;
            number1 = int.Parse(Console.ReadLine());
            Console.WriteLine("\n");
            number2 = int.Parse(Console.ReadLine());

            // Create an onject for the class MathsHelper
            MathsHelper helper = new MathsHelper();

            // Call the Add method
            int x = helper.Add(number1, number2);
            Console.WriteLine("\nThe sum of " + number1 +
                " and " + number2 + " is " + x);
            Console.ReadKey();
            // Call the Subtract function
            int y = helper.Subtract(number1, number2);
            Console.WriteLine("\nThe difference between " +
                  number1 + " and " + number2 + "  is " + y);
            ConsoleKeyInfo info = Console.ReadKey();
            if (info.Key == ConsoleKey.Enter)
                goto Finish;
              
            // Throw any Exception
            throw new System.Exception();

            Finish:
            Console.WriteLine("\nThank you\n");
        }
            // Catch the exception
        catch
        {
            Console.WriteLine("Please Enter a valid number following the instruction\n");
        }
              
    }
}
   
    // Class which contains a method to add and subtract
public class MathsHelper
{
        public MathsHelper() { }
    // Add method
    public int Add(int a, int b)
    {
        int x = a + b;
        return x;
    }
    // Subtract method
    public int Subtract(int a, int b)
    {
        int x = a - b;
        return x;
    }
}


}


3.      Right Click NUnitTestSample Solution and add a new class Library and give it the name ClassLibrary.Test (this is the NUnitTest project)



4.      Pate the following code in the created TestClass.cs file listed within ClassLibrary.Test
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NUnitTestSample
{
    class Program
{
    static void Main(string[] args)
    {

        try
        {
            // Get two numbers
            Console.WriteLine("Enter two numbers \n");
            Console.WriteLine("First number --> Press Enter --> Second number --> Press
            Enter\n");
            int number1;
            int number2;
            number1 = int.Parse(Console.ReadLine());
            Console.WriteLine("\n");
            number2 = int.Parse(Console.ReadLine());

            // Create an onject for the class MathsHelper
            MathsHelper helper = new MathsHelper();

            // Call the Add method
            int x = helper.Add(number1, number2);
            Console.WriteLine("\nThe sum of " + number1 +
                " and " + number2 + " is " + x);
            Console.ReadKey();
            // Call the Subtract function
            int y = helper.Subtract(number1, number2);
            Console.WriteLine("\nThe difference between " +
                  number1 + " and " + number2 + "  is " + y);
            ConsoleKeyInfo info = Console.ReadKey();
            if (info.Key == ConsoleKey.Enter)
                goto Finish;
              
            // Throw any Exception
            throw new System.Exception();

            Finish:
            Console.WriteLine("\nThank you\n");
        }
            // Catch the exception
        catch
        {
            Console.WriteLine("Please Enter a valid number following the instruction\n");
        }
              
    }
}
   
    // Class which contains a method to add and subtract
public class MathsHelper
{
        public MathsHelper() { }
    // Add method
    public int Add(int a, int b)
    {
        int x = a + b;
        return x;
    }
    // Subtract method
    public int Subtract(int a, int b)
    {
        int x = a - b;
        return x;
    }
}


}

5.      Right click NUnitTestSample and click Add New Project search Unit test in the Installed section and add a new Unit test Project called MSUnitTests




6.       Rename the class file listed within MSUnitTest as MSUnitTest.cs and paste the following code inside it

using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using NUnitTestSample;


namespace MSUnitTests
{
    [TestClass]
    public class MSUnitTest
    {
            [TestMethod]
            public void Add()
            {
                MathsHelper helper = new MathsHelper();
                int result = helper.Add(20, 10);

                // Check if the add function return 30

                Assert.AreEqual(result,30);                        
            }
       
            [TestMethod]
            public void Sub()
            {
                MathsHelper helper = new MathsHelper();
                int result = helper.Subtract(20, 10);

                // Check if the sub function return 10

                Assert.AreEqual(result,10);
            }
    }
}


7.      Now the Reference needed to added for both the Unit Test Projects
8.      For MSUnitTests Right click “References” and then “Add reference” now browse and and Click NUnitTestSample, this allows you to use the MathsHelper  methods in the test Project.

9.      Install the downloaded and install the  NUnitTest stable release (win)  from http://www.nunit.org/index.php?p=download



10.  Now you need to add a reference to the NUnit Test framework from the ClassLibrary.Test (NUnitTest) project.
11.  Right click References of the ClassLibrary.Test Project, click Add Reference, Browse to the location where the NUnit stable release is installed in your PC, (in my case it is C:\Program Files (x86)\NUnit 2.6.3\bin\) and reference all the dll required.




12.  Press Control + F5 to run the application
13.  To run the MSUnitTests , Open Test – Window – Test Explorer and run all tests

14.  To Run ClassLibrary.Test  (NUnit Test ) open the NUnit Stable version installed.

15.  Click File – Open Project and browse the ClassLibrary .Test project and click Run.



16.  This how you can test an application using NUnit and MSUnit testing framework. Now it is your personal preference about which you like to choose. 

This project will be uploaded to https://github.com/shaliniac/ShaliniNETSamples/tree/master
 

No comments:

Post a Comment