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.
Step 2: Create a new project in Visual Studio 2013.
Seelct "FILE" -> "New" -> "Project...".
Step 3: Select the project language as C# and select a Console Application and enter the name of the project as Mathematics (Project Name).
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.
- 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.
- public static void TrianlgeSidesInput(double side1, double side2, double side3)
- {
- string TriangleType = "";
- if ((side1 == side2) && (side1 == side3))
- {
- TriangleType = "Equilateral";
- }
- else if ((side1 == side2) || (side1 == side3) || (side2 == side3))
- {
- TriangleType = "Isosceles";
- eqSide1 = side1; eqSide2 = side2; eqSide3 = side3;
- }
- else
- {
- TriangleType = "Scalene";
- }
-
- switch (TriangleType)
- {
- case "Equilateral":
- Console.WriteLine("It is Equilateral Traingle ! Reason: It's all sides are equal.\n\n\t i. e. {0}=={1}== {2}", side1, side2, side3);
- Console.WriteLine("For Next : Press Enter \n");
-
- break;
-
- case "Isosceles":
- Console.WriteLine("It is Isosceles Traingle ! Reason: It's two sides are equal.");
- Console.WriteLine("For Next : Press Enter \n");
- break;
-
- case "Scalene":
- Console.WriteLine("It is Scalene Traingle ! Reason: It's all sides have different lenght !!");
- Console.WriteLine("For Next : Press Enter \n");
- break;
-
- default:
- Console.WriteLine("Sorry ! Try Again !");
- break;
- }
-
- }
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.
- static void Main(string[] args)
- {
- Console.Write("Triangle Type Test :-\n----------------------------\n\n");
- Trigonometric.TrianlgeSidesInput(12, 12, 12);
- Console.ReadLine();
-
- Trigonometric.TrianlgeSidesInput(12, 12, 15);
- Console.ReadLine();
-
- Trigonometric.TrianlgeSidesInput(12, 20, 25);
- Console.ReadLine();
- }
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.
Now select Unit Test Project from the New Project Dialog Box.
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.
Now check the project exe (Mathematics for this projecdt) , if not found go to the project folder and find Bin--> Debug --- Mathematics.exe
Step 10: Now open the class generated with the UnitTest project and the following method inside the class.
UnitTest1.cs
- [TestClass]
- public class UnitTest1
- {
- [TestMethod]
- public void TestMethod1()
- {
-
- Trigonometric.TrianlgeSidesInput(10, 10, 15);
-
- }
-
- [TestMethod]
- public void TestMethod2()
- {
-
- Trigonometric.TrianlgeSidesInput(10.5, 10.5, 15);
-
- }
-
- [TestMethod]
- public void TestMethod3()
- {
-
- Trigonometric.TrianlgeSidesInput(10.5, 12.5, 15.5);
-
- }
- }
STEP 11: Now right-click the test method and select Run Test, then watch the results in Text Explore.
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.
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.
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.