Overview of Nunit Testing Framework


what is the Nunit ?

If you are new to Nunit, please go through this post How to configure Nunit in Visual Studio

It is the unit test case framework based on .Net Framework. Which is widely used by .net developer to write the unit test case. It is open source and free to use by any person. You don’t have to pay any money for this tool.

why should we use Nunit framework instead of MS-UnitTest Framework ?

These are the few important point to use Nunit instead of MS-Test

1. It is matured and stable unit test case framework
2. It gives good performance as compare to MS-Test
3. It has good community support
4. It is the popular Open Source project
5. Suite attribute - can aggregate tests and execute them separately. It will be useful for large projects.
6. NUnit has frequent version updates - MS-Test has only one per VS version.
7. It takes less memory to execute the test case as compare to MS-Test.

what are the important attributes of Nunit Framework?

There are following important attributes of NUnit Framework
a> TestFixtureAttribute

>>It is used to indicate the class which contains the test method.

b> Description

>> as the name suggest, it gives the description of class or test method

using System;
  using NUnit.Framework;

  [TestFixture, Description("Fixture description here")]
  public class SomeTests
  {
    [Test, Description("Test description here")] 
    public void TestMethod()
    { /* ... */ }
  }
}

c. SetUP
>> It is generally used for declaration of object before running the unit test case.

d. TearDown : It is generally used for cleaning the object at end of the test case.

d. Test : This attribute generally keep on top of method name. It is used for writing the unit test case related code for check the expected functionality of application.

using System;
  using NUnit.Framework;

  [TestFixture]
  public class SuccessTests
  {
    [SetUp] 
    public void Init()
    { /* ... */ }

    [TearDown]
     public void Cleanup()
    { /* ... */ }

    [Test] 
    public void Add()
    { /* ...... */ }

There are so many other attribute available in Nunit. If you are looking to learn more about it. Please check the below URL

Nunit Attributes document URL

Advertisements

How to configure Visual Studio with Nunit Testing Framework


As we know that Nunit is testing framework which is widely used in .net community. It is open source and free to use. So in this post we will see how to configure the Nunit with Visual studio. Here i have used Visual Studio 2015.

There are the simple steps to configure the visual studio for Nunit Testing Framework.

Step 1: Open the visual studio –> Go to Tool Menu –> Extension and updates –>Select Nunit VS Templates and Nunit 3 Test Adapter

Step 2: Install the both plugin as shown below image

Step 3: Open the visual Studio and create new Test Unit Project like this

Step 4: Write some simple unit test case and click on Run All unit test as given below image.

Now no need to attach the dll into Nunit Software UI, we can run the unit test case in Visual studio Test Explorer as given above image.

Code Coverage Tool (Part 3)


This is the last part of code coverage tool, in this post we will see the open source code coverage tool,i.e. OpenCoverage which is light weight and totally free to use. Which will give almost all functionalities as similar to third party code coverage tool (Ncoder and DotCover)

NCover Code Coverage Tool
DotCover Coverage Tool

How to use OpenCover tool?

Step 1: Create some demo unit test case in visual studio. I have written some dummy unit test case as given below

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

namespace UnitTestCase_Sample
{
    [TestClass]
    public class UnitTestCaseDemo
    {
        [TestMethod]
        public void AdditionOfTwoNum()
        {
            var a = 0;
            var b = 0;
            if (a!=0 && b!=0)
            {
                var result = a + b;
                Assert.AreEqual(30, result);
            }
            else
            {
                var result = a + b;
                Assert.AreEqual(0, result);
            }
        }

        [TestMethod]
        public void SubstractionOfTwoNum()
        {
            var a = 20;
            var b = 10;
            var result = a - b;
            Assert.AreEqual(10, result);
        }

        [TestMethod]
        public void MultiplicationOfTwoNum()
        {
            var a = 20;
            var b = 10;
            var result = a * b;
            Assert.AreEqual(200, result);
        }

        [TestMethod]
        public void DivisionOfTwo_Num()
        {
            var a = 20;
            var b = 10;
            var result = a / b;
            Assert.AreEqual(2, result);
        }

        [TestMethod]
        public void Addition_Of_ThreeNum()
        {
            var a = 10;
            var b = 20;
            var c = 30;
            var result = a + b + c;
            Assert.AreEqual(60, result);
        }

        [TestMethod]
        [ExpectedException(typeof(DivideByZeroException))]
        public void Test()
        {
            int i = 10, j = 0;
            int x;
            x = i / j;
        }
       
    }
}

Step 2: Download the OpenCover UI from Microsoft store, from the given below URL

https://marketplace.visualstudio.com/items?itemName=jamdagni86.OpenCoverUI

In Visual Studio Tab will show like this

Step 3: Right click on solution explorer and click on Manage NuGet Packages and Install the OpenCover and ReportGenerator in your application as given below

Step 4: Go to Visual studio Tool ->> Options ->> OpenCoverUI Options then select the OpenCover.Console.exe from your application.

Step 5: Go to OpenCover tool of Visual Studio and Select OpenCover Test Explorer and select the test cases to be run like given below Image

Now we will get output like given below

In the above result it is showing that how much code has been covered in which test case.

If you have to fix the problem then just double click on given method, you will see the code like this given below image

This indicate that below code branch has not been executed. So we have to write the other unit test case to cover above code.

How to get the report as third party control as similar to Ncover and DotCover

For getting the report in user friendly manner we have to take help of Report Generator Tool.
For that we have to create the .bat file, which will generate the report in given folder.

Firstly create the notepad file keep this syntax as given below

"C:\Source\Test \packages\OpenCover.4.6.519\tools\OpenCover.Console.exe" -register:user -target:"C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\mstest.exe" -targetargs:"/noisolation /testcontainer:\"C:\Source\Test \Test\bin\Debug\Test.dll\" /resultsfile:C:\Reports\MSTest\.trx" -mergebyhash -output:C:\Reports\MSTest\projectCoverageReport.xml


"C:\Source\Test \packages\ReportGenerator.2.5.2\tools\ReportGenerator.exe" -reports:"C:\Reports\MSTest\projectCoverageReport.xml" -targetdir:"C:\Reports\CodeCoverage"

Now save the file as .bat extension and run the bat file.

Note: Before running the bat file, please create the Reports\CodeCoverage and MSTest directory in C folder.

Now go to the CodeCoverage folder and see the report after clicking on Index.html file like as given below

In the above tools compare we show that OpenCover contains almost all the major features which contains all the paid version tools and it is very light weight and flexible to use in any project.

Comparison between all the Tools

Code Coverage Tool (Part 2)


Hi,

Before reading this post, please go through my previous article what is the code coverage tool

In this post we will see one of famous tool i.e. DotCover Code Coverage

It is the JetBrains Code coverage tools.It is paid version tool. It has the following features
1. Continuous unit testing
2. Code highlighter
3. Hot spot feature for breaking code.
4. Save code coverage report in HTML,XML and Json format

For download the trail version of this tool, you ca download from this URL

https://www.jetbrains.com/dotcover/download/

Before using this tool you have to also download and install re-sharper Tool.

If you have installed the tool successfully then you will get the resharper Menu in Visual Studio and from there you can select the Cover unit test case. You can also right click solution explorer and select the Cover Unit Tests and click on it like given below image

It will show the result like this

In the above image, it is showing which method has how much the code coverage.

It has the different type of save report options like HTML,Json and XML format

Example of HTML format Report

Example of JSON format Report

Example of XML format Report

Example of Json format Report

using the above report we can easily find out the uncovered test case in our application.

What is the code coverage tool ? (Part1)


Code coverage is the process of doing analysis in C# unit test code to find out which block or statement of code is executed or not.

The basic code coverage criteria are

1. Function coverage
2. Statement coverage
3. Branch Coverage
4.Conditional Coverage

There are so many tools available for this task

1. Visual Studio code coverage (Only in enterprise edition)
2. NCover
3. JetBrains dotCover
4. OpenCover
5. NCrunch
6. NDepend

I had gone through the few important code coverage tool

1.Visual Studio code coverage (Only in enterprise edition)

>> In visual studio enterprise edition default code coverage tool will be there. Since i am not using the enterprise version, I did not do deep on analysis on this tool. If you are using the enterprise edition you can go through the below URL

https://msdn.microsoft.com/en-IN/library/dd537628.aspx

2. NCover Code Coverage
>> Ncover is third party tool for visual studio.It is not the free tool. For this we have to download the software from given below URL

http://www.ncover.com/products/desktop

This tool is very straight forward to use. Firstly we have to install the tool then we will get the Ncover Menu in visual Studio.
Click on Ncover Code Coverage then you will get the Ncover Test Runner. It will load all the test case written in your application

Here i have written the test code like this

In the above test method, it will run only else condition so we are not testing the code of if condition. So if we will run the NCover tool it will show covered code as green and not covered code as red as given in above image.

It helps to find out the uncovered code in test application. This tool has also option to view the result on Desktop. It has also option to download the report in different format like html,Json and XML.

Posted in Tools. 1 Comment »