Unit-Testing-in-ASP.NET_

Unit Testing in ASP.NET

Table of Contents

Testing is an essential part of any programming language. Testing for any ASP.Net applications is possible with the help of Visual Studio.

Visual Studio can be used to create test code. It can also be used to run the test code for any ASP.Net application. Hence, it becomes easy to check for any errors in an ASP.Net application.

The first level of Unit Testing in ASP.NET project is the unit level testing. It tests the functionality of an application and to ensure that the application behaves the same as expected. The first task is the creation of a test project in the Visual Studio. The test project will contain all the necessary code to test the application.

Let us consider the below web page. On the page, we have the message “ASP.NET” displayed. Now we need to confirm that the correct message is displayed when the ASP.Net project runs. It is done by adding a test project to the ASP.Net solution. This test project would also ensure that the right message is displayed to the user.

Unit Testing in ASP.NET

How to create a .NET Unit Testing Project:

Before creating a test project, we need to perform the below steps.

  1. Use ‘DemoApplication’ as our application, which needs to be tested.
  2. We will now add a new class to the DemoApplication. This class will contain a string called “ASP.NET.’  We will test this string in our testing project.
  3. Finally, we will create a testing project to test the ASP.Net application.

So let us follow the below steps and see how to implement testing.

Step 1: Ensure that the Demo Application is open in Visual Studio.

Step 2: Let us now add a new class to the DemoApplication. This class will contain a string called ‘ASP.NET’ and will be tested in our testing project.

Follow the steps given below for adding a new class.

How to Create and Run Asp.Net Unit Testing Project
  1. In Visual Studio, right-click on the ‘DemoApplication’ in the Solution Explorer.
  2. Choose Add->Class from the context menu.

Step 3) In this step,

How to Create and Run Asp.Net Unit Testing Project
  1. Give the name ‘Tutorial.cs’ for the new class.
  2. Click on the ‘Add’ button to add the file to the DemoApplication.

Now, a new class is added to the file “DemoApplication.”

Step 4) Now, open the new Tutorial.cs file from “DemoApplication.” Add the string “ASP.NET.”

For opening the file, double-click on the Tutorial.cs file in the Solution Explorer.

How to Create and Run Asp.Net Unit Testing Project

This file will have some default code already written. Do not bother about that code; add the below line of code.

namespace DemoApplication
{  
  public class Tutorial
  {
     public String Name;
  public Tutorial()
  {
    Name = "ASP.NET";
  } 
  }
}

Code Explanation:

  1. The Name variable is of type string.
  2. Finally, the constructor of the Tutorial class assigns the value of the Name variable. The value is assigned to “ASP.NET.”

Step 5) Now go to the demo.aspx file and add the below lines of code to display the text “ASP.NET.”

How to Create and Run Asp.Net Unit Testing Project
<!DOCTYPE html>
<html xmlns="https://www.w3.ore/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server”>
<div>
<% DemoApplication.Tutorial tp=new DemoApplication.Tutorial();%>
<%=tp.Name%>
</div>
</form>
</body>
</html>

Code Explanation:

  1. The first line will create an object of the class ‘Tutorial’. This is the first step while working with classes and objects. The name given to the object here is ‘tp’.
  2. Finally, we will call ‘tutorial.cs’ from the demo.aspx file. It will display the value of the Name variable.

When you will run the above program in Visual Studio, you will get the following output.

Output:

Unit Testing in ASP.NET

From the output, you see the message “ASP.NET” displayed.

Step 6) Now, let us add our test project to the Demo Application with the help of Visual Studio.

How to Create and Run Asp.Net Unit Testing Project
  1. Right-click on the Solution – DemoApplication.
  2. In the context menu, select the option ‘New Project.’

Step 7) The step here involves the addition of the Unit Test project to the demo application.

  1. Click on an item type as ‘Test’ from the left panel.
  2. Choose the item named as ‘Unit Test Project’ from the list that appears in the dialog box’s center part.
  3. Give a name for the test project. In our case, the name given is ‘DemoTest.’
  4. Finally, click on the ‘OK’ button.

You will see that the DemoTest project added to the solution explorer. Here, you can also see other files such as UnitTest1.cs, properties, etc. that are generated by default.

How to Create and Run Asp.Net Unit Testing Project

Running the Test Project:

The test project created above is used to test our ASP.Net application. In the below steps, we are going to see how to run the Test project.

  • The first step is to add a reference to the Unit Testing in ASP.NET project and it is carried out so that out test project has access to the ASP.Net project.
  • Then we write our test code.
  • Finally, we run the test using Visual Studio.

Step 1) To test our Demo Application, the first test project must reference the Demo Application. Add a reference to the Demo.aspx solution.

How to Create and Run Asp.Net Unit Testing Project
  1. Right-click on the Demo Test project
  2. From the menu, select the option of Add->Reference.

Step 2) The next step will be to add a reference to the DemoApplication.

  1. Select the option Projects from the left side of the dialog box
  2. Click on the check box present next to DemoApplication.
  3. Click on the ‘OK’ button.

This allows a demotest project to test our DemoApplication.

Step 3) Now let us add the test code to our test project.

  • For this, double-click on the UnitTest1 (UnitTest1 file is automatically added by Visual Studio when the Test project is created) in the Solution Explorer.
  • This is the file that will run to test the ASP.Net project.
How to Create and Run Asp.Net Unit Testing Project

You will now see the below code added by Visual Studio in the UnitTest1.cs file. This is the most basic code needed for the test project to run.

Unit Testing in ASP.NET
Unit Testing in ASP.NET

Step 4) The next step now is to add the code used to test the string “ASP.NET.”

using System;
using Microsoft.VisualStudio.TestTools.UnitTesting; 
using DemoApplication;
namespace DemoTest
{
 [TestClass]
 public class UnitTestl
 {
   [TestMethod]
   public void TestMethodl()
   {
      Tutorial tp = new Tutorial();
   Assert.AreEqual(tp.Name,"ASP.NET");
   }
 }
}
  1. Create a new object named ‘tp’ of the type Tutorial
  2. The method Assert.AreEqual is used to test if a value is equal to something. So in this case, we are comparing the values of tp.Name to ASP.NET.

Step 5) Now, let us run our test project. For this, we will go to the menu option Test->Run->All Tests.

How to Create and Run Asp.Net Unit Testing Project

Output:

Unit Testing in ASP.NET
Unit Testing in ASP.NET

A test Explorer window will appear in the Visual Studio that shows the above result and displays that a successful test was run in Visual Studio.

Share this article
Subscribe
By pressing the Subscribe button, you confirm that you have read our Privacy Policy.
Need a Free Demo Class?
Join H2K Infosys IT Online Training
Enroll Free demo class