Unit Testing With Console Application

Introduction

A Unit Test is a part of software development that is done by the development team at the time of development. Basically it is used to test the smallest unit of code, like methods, properties, classes and assemblies also. Where the smallest unit of code is known as a unit.

Good news for us is that Microsoft announced a new feature of unit testing with Visual Studio 2015 Preview at the day #vsconnect, in other words Smart Unit Test. When we apply a smart unit test, we can easily identify which unit of tests are not executing depending on our expectation and we can add some necessary code to fix that problem.

In other words, we can say that "Smart Unit Test" is an enhanced feature of Visual Studio 2015 that assists us in finding bugs inside the test units. Basically it generates a suite of tests for every possible code path in your .NET code. It will automatically evolve the test suit as your code under test evolves. But we will discuss it in a future article. We are now going to do the unit test in Visual Studio 2013 with a Console Application. Before starting the project for Unit Testing, let's see some basic concept of unit testing.

Purpose: The main purpose of unit testing is to take the small unit of code from the project, separate it from the code and check whether or not it behaves exactly the same as we expect. So we first do the unit test with each unit of test before integrating them into the project's modules.

Use the following procedure to do the Unit Test with a Console Application.

Step 1: Open Visual Studio 2013.

VisualStudio2013

Step 2: Create a new project in Visual Studio 2013.

Seelct "FILE" -> "New" -> "Project...".

NewProject

Step 3: Select the project language as C# and select a Console Application and enter the name of the project as Mathematics (Project Name). 

ConsoleApp1

Step 4: Now write the following method that will identify the type of triangle. Like the triangle may either be Equilateral, Isosceles or Scalene. Before writing the following methods in your application, first the replace the name of class as Trigonometric in the place of Program (default class name in Console Application) and define the following variables as global variables.

  1. public static double eqSide1, eqSide2, eqSide3;   

Step 5: Now write a method with the name TrianlgeSidesInput(...) with the three arguments Side1, Side2 and Side3. For more details see the following program code. 

  1. public static void TrianlgeSidesInput(double side1, double side2, double side3)  
  2.  {  
  3.       string TriangleType = "";  
  4.       if ((side1 == side2) && (side1 == side3))  
  5.       {  
  6.            TriangleType = "Equilateral";  
  7.       }  
  8.       else if ((side1 == side2) || (side1 == side3) || (side2 == side3))  
  9.       {  
  10.            TriangleType = "Isosceles";  
  11.            eqSide1 = side1; eqSide2 = side2; eqSide3 = side3;  
  12.       }  
  13.       else  
  14.       {  
  15.           TriangleType = "Scalene";  
  16.       }  
  17.   
  18.       switch (TriangleType)  
  19.       {  
  20.           case "Equilateral":  
  21.           Console.WriteLine("It is Equilateral Traingle ! Reason: It's all sides are equal.\n\n\t i. e. {0}=={1}==                                 {2}", side1, side2, side3);  
  22.           Console.WriteLine("For Next : Press Enter \n");  
  23.   
  24.           break;  
  25.   
  26.           case "Isosceles":  
  27.           Console.WriteLine("It is Isosceles Traingle ! Reason: It's two sides are equal.");  
  28.           Console.WriteLine("For Next : Press Enter \n");  
  29.           break;  
  30.   
  31.           case "Scalene":  
  32.           Console.WriteLine("It is Scalene Traingle ! Reason: It's all sides have different lenght !!");  
  33.           Console.WriteLine("For Next : Press Enter \n");  
  34.           break;  
  35.   
  36.           default:  
  37.           Console.WriteLine("Sorry ! Try Again !");  
  38.           break;  
  39.      }  
  40.   
  41. }  

Step 6: Write the following code in the Main() method that executes the method defined in the Trigonometric Class. See the code for more details. 

  1. static void Main(string[] args)  
  2.  {  
  3.        Console.Write("Triangle Type Test :-\n----------------------------\n\n");  
  4.        Trigonometric.TrianlgeSidesInput(12, 12, 12);  
  5.        Console.ReadLine();  
  6.   
  7.        Trigonometric.TrianlgeSidesInput(12, 12, 15);  
  8.        Console.ReadLine();  
  9.   
  10.        Trigonometric.TrianlgeSidesInput(12, 20, 25);  
  11.        Console.ReadLine();  
  12.  }  

Step 7: Now add a Unit Test project to the current project. For this use the following procedure.

Go to Solutions Explorer and right-cilck on the project in the solution to add a new Test Project. For an explanation see the following picture.

AddUnitTest

Now select Unit Test Project from the New Project Dialog Box.

UnitTestProject

Step 8: Now build the main project (Mathematics).

To build the project right-click on the project name in the Solution Explorer and select Build in the context menu.

Step 9: Now add the reference of the current project (Mathematics). To do so, right-click on the UnitTest project in the Solution Explorer and select Add --> References. 

AddReferences2

Now check the project exe (Mathematics for this projecdt) , if not found go to the project folder and find Bin--> Debug --- Mathematics.exe 

SelectExeofProject
 
Step 10: Now open the class generated with the UnitTest project and the following method inside the class.

UnitTest1.cs 

  1. [TestClass]  
  2. public class UnitTest1  
  3. {  
  4.     [TestMethod]  
  5.     public void TestMethod1()  
  6.     {  
  7.         //First  
  8.         Trigonometric.TrianlgeSidesInput(10, 10, 15);  
  9.   
  10.     }  
  11.   
  12.     [TestMethod]  
  13.     public void TestMethod2()  
  14.     {  
  15.         //Second Test  
  16.         Trigonometric.TrianlgeSidesInput(10.5, 10.5, 15);  
  17.   
  18.     }  
  19.   
  20.     [TestMethod]  
  21.     public void TestMethod3()  
  22.     {  
  23.         //Thisrd Test  
  24.         Trigonometric.TrianlgeSidesInput(10.5, 12.5, 15.5);  
  25.   
  26.     }  
  27. }  

 

 STEP 11: Now right-click the test method and select Run Test,  then watch the results in Text Explore. 

RunTest

Step 12: To see the output of the test go inside the test method of the UnitTest project and the TestPassed mark that appears just before the reference in the codelens then we will get a pop-up having an option link for Output.

OutPut1

If we click on the Output link then that is present in the popup, then we can see the output of the real method as test output. 

OutputResult1

Summary

In a future article we will do Smart Unit Testing that can be done with Visual Studio 2015 as well as Visual Studio 2013 Update 4.

Up Next
    Ebook Download
    View all
    Learn
    View all