Unit testing In Web API

Introduction
 
Unit testing plays a very important role in software development processes. With the help of unit testing we will test individual methods, if I am to perform unit testing on a method, it not affect the any other methods. A unit is smallest testable part of the application example- function, interface, procedure, classes.
The unit testing basically comes after Integration testing depicted in below figure.
 
 
 
Let Create Unit Testing in Web API Step by Step.
 
Create MVC Application
 
Step 1
 
Go to File, New, then Project.
 
Step 2
 
Choose "ASP.NET MVC 4 Web Application" from the list, then provide the application name as "UnitTestingProj" and set the path in the location input where you want to create the application.
 
Step 3
 
Now choose the Project Template "Web API".
 
Step 4
 
Add API empty controller and Class
  1. public class unittestController : ApiController  
  2.     {  
  3.         public List<city> Getcitylist()  
  4.         {  
  5.   
  6.             List<city> Lts = new List<city>();  
  7.   
  8.             Lts.Add(new city { cityid = 1, cityname = "noida", Address = "e-block" });  
  9.             Lts.Add(new city { cityid = 2, cityname = "Roorkee", Address = "Iqbalpur" });  
  10.             Lts.Add(new city { cityid = 3, cityname = "Delhi", Address = "Rohini" });  
  11.             Lts.Add(new city { cityid = 4, cityname = "haridwar", Address = "shaket" });  
  12.             return Lts;  
  13.   
  14.         }  
  15.   
  16.         public class city  
  17.         {  
  18.             public int cityid { getset; }  
  19.             public string cityname { getset; }  
  20.             public string Address { getset; }  
  21.   
  22.   
  23.         }  
  24.     }  
For unit testing we need to add nuget package, Right-click on Your project and select Manage NuGet Packages as in the following
 
 
 
Find and install the Microsoft ASP.NET Web API 2.2 Core package as in the depicted figure.
 
 
Create another class TestClass
 
  1. public class testclass  
  2.     {  
  3.           
  4.  [TestMethod]  
  5.         public void testGetCityList()  
  6.         {  
  7.             try  
  8.             {  
  9.             var config = new HttpConfiguration();  
  10.             var Controller = new unittestController();  
  11.             var Route = config.Routes.MapHttpRoute("DefaultApi""api/unittest");  
  12.              var RouteData = new HttpRouteData(Route, new HttpRouteValueDictionary { { "controller""unittest" } });  
  13.               var Request = new HttpRequestMessage(HttpMethod.Get, "http://localhost:58068/api/unittest/9/9/9");  
  14.                 Controller.ControllerContext = new System.Web.Http.Controllers.HttpControllerContext(config, RouteData, Request);  
  15.                 Controller.Request = Request;  
  16.                 Controller.Request.Properties[HttpPropertyKeys.HttpConfigurationKey] = config;  
  17.                 GetUserProfileIdForErrorLog objUserProfileId = new GetUserProfileIdForErrorLog();  
  18.                 objUserProfileId.UserProfileIdForError(2792);  
  19.                 ErrorObj objerror = new ErrorObj();  
  20.                 objerror.UserProfileCurrentStatus = "8";  
  21.                 var respone = Controller.Getcitylist();  
  22.                // Assert.AreEqual(HttpStatusCode.OK, respone.StatusCode);  
  23.             }  
  24.           
  25.         catch (Exception ex)  
  26.         {  
  27.             Assert.Fail("Expected no exception, but got: " + ex.Message);  
  28.             throw;  
  29.         }      
  30.     }  
All of the test methods contain with the TestMethod attribute. Let us see the procedure to run the Test Method Select Test -> Run -> All Tests to run the tests.
 
 
Summary
 
In this article, we learned how to use unit testing in Web API and role of Web API
 
Read more articles on Testing: 

Up Next
    Ebook Download
    View all
    Learn
    View all