Author - StudySection Post Views - 35 views
unit-test

Unit Testing in Asp.Net

What is UnitTesting?

It is the first level of testing to check the functionality of an application where individual units/ components of a software are tested. It is a component of TDD (Test Driven Development). Unit testing has the greatest effect on the quality of your code and integrated part of your software development workflow.

Testing for ASP.Net Application

Testing for ASP.Net applications can be done with the help of Visual Studio. Visual Studio provides a flexible and efficient way to run your unit tests and view their results in Visual Studio. We write a small piece of code for a particular method to test a particular situation or scenario. A method can have multiple scenarios so we can create multiple test cases for one method.

Create a project and unit tests manually

Steps to add a unit test project to a solution:

  1. Add the new project from the visual studio.

    img1

  2. Configure Your Project Details

    web-app

  3. We can also add the ‘Unit Test Project’ into an existing project then we have to add the reference of that project and also while creating a new project as shown below :

    create-app

    unit testing asp

To create unit test cases into a project :

The unit test project (DemoUnitTesting) is created. After this we create test cases with the help of methods as an example given below:


HomeController.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace DemoUnitTesting.Controllers
{
public class HomeController : Controller
{
public ActionResult TestUnitTesting()
{
ViewBag.Message = "Hello Unit Testing"; // message for testing
return View();
}

}
}

Case 1: Test passed

HomeControllerTest.cs

using System.Text;
using System.Web.Mvc;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using DemoUnitTesting;
using DemoUnitTesting.Controllers;

namespace DemoUnitTesting.Tests.Controllers
{
[TestClass] public class HomeControllerTest
{
[TestMethod] public void TestMethod()
{
// Arrange : sets the value of the data that is passed to the method under test

HomeController controller = new HomeController(); // create instance of controller
string viewbagMessage = "Hello Unit Testing"; //expected result

// Act: invokes the method under test
ViewResult result = controller.TestUnitTesting() as ViewResult;

// Assert
Assert.IsNotNull(result);
Assert.AreEqual(viewbagMessage, result.ViewBag.Message); //compare the result
}
}
}

Testing the Action Result returned by a Controller

We can run the unit test either by entering the keyboard combination Ctrl-R, A or by clicking the RunTests as shown below. Status for the test (passed or failed ) is visible on the test explorer window.

editor unit testing

unit test-method

Case 2: Test failed

HomeControllerTest.cs

using System.Text;
using System.Web.Mvc;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using DemoUnitTesting;
using DemoUnitTesting.Controllers;

namespace DemoUnitTesting.Tests.Controllers
{
[TestClass] public class HomeControllerTest
{
[TestMethod] public void TestMethod()
{
// Arrange : sets the value of the data that is passed to the method under test

HomeController controller = new HomeController(); // create instance of controller
string viewbagMessage = "Hello Testing"; //change value of expected result

// Act: invokes the method under test
ViewResult result = controller.TestUnitTesting() as ViewResult;

// Assert
Assert.IsNotNull(result);
Assert.AreEqual(viewbagMessage, result.ViewBag.Message); //compare the result
}
}
}

test-method

Debug test

We can debug the unit test by clicking Debug Tests as shown below:

debug
demounit

Here, an exception is shown that the expected result for the viewing message is “ Hello Testing” but the actual value in the controller is “Hello Unit Testing”. This is how we can test more scenarios with debug exceptions.

Microsoft Windows 10 is a widely used operating system in computers all over the world. If you have skills in Microsoft Windows 10 then you can get a Windows 10 Certification from StudySection which can help you in getting hired. A beginner level certification exam for newbies and an advanced level certification exam for experts is available on StudySection.

Leave a Reply

Your email address will not be published. Required fields are marked *