• Home
  • Basics
  • DSA
  • MAD
  • Concept
  • Practice
  • Misc
    • Tips
    • QA’s
    • Misc
  • Course
  • Home
  • Basics
  • DSA
  • MAD
  • Concept
  • Practice
  • Misc
    • Tips
    • QA’s
    • Misc
  • Course
  • #News
  • #APPS
  • #Apple WWDC
  • #Google I/O
  • #Microsoft Ignite
  • #Let’s Talk
  • #Advertise

MyCodeTips mycodetips-newlogocopy1

  • Home
  • Basics
  • DSA
  • MAD
  • Concept
  • Practice
  • Misc
    • Tips
    • QA’s
    • Misc
  • Course
.Net

What is Unit Testing in .Net

Unit testing is a kind of testing done at the developer side. It is used to test methods, properties, classes, and assemblies. Unit testing is not testing done by the quality assurance department. To know where unit testing fits into development, look at the following image:
Unit testing is used to test a small piece of workable code (operational) called unit. This encourages developers to modify code without immediate concerns about how such changes might affect the functioning of other units or the program as a whole. Unit testing can be time consuming and tedious, but should be done thoroughly with patience.

What is NUnit?
NUnit is a unit testing framework for performing unit testing based on the .NET platform. It is a widely used tool for unit testing and is preferred by many developers today. NUnit is free to use. NUnit does not create any test scripts by itself. You have to write test scripts by yourself, but NUnit allows you to use its tools and classes to make unit testing easier. The points to be remembered about NUnit are listed below:

NUnit is not an automated GUI testing tool.
It is not a scripting language, all tests are written in .NET supported languages, e.g., C#, VC, VB.NET, J#, etc.
NUnit is a derivative of the popular testing framework used by eXtreme Programming (XP). It was created by Philip Craig for .NET. It is also available in the name of jUnit for Java code testing.

NUnit works by providing a class framework and a test runner application. They can be downloaded from here. The class framework allows to write test cases based on the application. The test is run using the test runner application downloaded from the above link.

Steps for Using NUnit
First, what one needs to do is download the recent version of the NUnit framework from the above mentioned website.

In development studio, create a new project. In my case, I have created a console application.
In the program.cs file, write the following code:

class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter two numbers\n");
int number1;
int number2;
number1 = int.Parse(Console.ReadLine());
number2 = int.Parse(Console.ReadLine());

MathsHelper helper = new MathsHelper();
int x = helper.Add(number1, number2);
Console.WriteLine("\nThe sum of " + number1 + 
" and " + number2 + " is " + x);
Console.ReadKey();
int y = helper.Subtract(number1, number2);
Console.WriteLine("\nThe difference between " + 
number1 + " and" + number2 + " is " + y);
Console.ReadKey();
}
}

public class MathsHelper
{
public MathsHelper() { }
public int Add(int a, int b)
{
int x = a + b;
return x;
}

public int Subtract(int a, int b)
{
int x = a - b;
return x;
}
}

Then to the solution of the project, add a new class library project and name it followed by “.Test” (it is the naming convention used for unit testing). Import the downloaded DLL files into the project and follow the steps given below.
In the newly added project, add a class and name it TestClass.cs.
In the class file, write the following code:

public class TestClass
{
[TestCase]
public void AddTest()
{
MathsHelper helper = new MathsHelper();
int result = helper.Add(20, 10);
Assert.AreEqual(30, result);
}

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

Now open the test runner (test runner is downloaded from the NUnit site along with the NUnit DLLs). In the NUnit API, click File > Open project. A file open dialog appears. Give the path of the NUunit test project DLL. Now run the test. If the test passes, then the following test screen is displayed:

 

  • Click to share on Reddit (Opens in new window)
  • Click to share on Facebook (Opens in new window)
  • Click to share on Twitter (Opens in new window)
  • Click to share on LinkedIn (Opens in new window)
  • Click to share on Tumblr (Opens in new window)
  • More
  • Click to share on Pocket (Opens in new window)
  • Click to share on Pinterest (Opens in new window)
Written by Ranjan - June 13, 2014 - 1373 Views
AUTHOR
Ranjan

This website is basically about of what we learnt from my years of experience as a software engineer on software development specifically on mobile application development, design patterns/architectures and its changing scenarios, security, troubleshooting, tools, tips&tricks and many more.

Next Post
Previous Post

Support us

Subscribe for updates

Join 8,278 other subscribers

Latest Posts

  • Exploring Single Point Failure
    Exploring Single Point Failures: Causes and Impacts
  • primitive-datatypes-new
    Exploring the Pros and Cons of Primitive Data Types
  • best practice clean code
    Essential Coding Standards and Best Practices for Clean Code
  • YT-Featured-Templates--lld
    What Business Problems Solves in Low Level Design (LLD)
  • SRP-SingleResopnsibility
    SRP : Single Responsibility Principle in Swift and Objective-C
whiteboard

Whiteboard(PRO)

whiteboard

Whiteboard(lite)

alphabets

Kids Alphabet

do2day

Do2Day

  • #about
  • #myapps
  • #contact
  • #privacy
  • #Advertise
  • #myQuestions

Android Database Interview IOS IOSQuestions Javascript Objective-c Programming Swift Tips&Tricks Web Wordpress

  • Exploring Single Point Failures: Causes and Impacts
  • Exploring the Pros and Cons of Primitive Data Types
  • Essential Coding Standards and Best Practices for Clean Code
MyCodeTips

©mycodetips.com

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.