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
- public class unittestController : ApiController
- {
- public List<city> Getcitylist()
- {
-
- List<city> Lts = new List<city>();
-
- Lts.Add(new city { cityid = 1, cityname = "noida", Address = "e-block" });
- Lts.Add(new city { cityid = 2, cityname = "Roorkee", Address = "Iqbalpur" });
- Lts.Add(new city { cityid = 3, cityname = "Delhi", Address = "Rohini" });
- Lts.Add(new city { cityid = 4, cityname = "haridwar", Address = "shaket" });
- return Lts;
-
- }
-
- public class city
- {
- public int cityid { get; set; }
- public string cityname { get; set; }
- public string Address { get; set; }
-
-
- }
- }
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
- public class testclass
- {
-
- [TestMethod]
- public void testGetCityList()
- {
- try
- {
- var config = new HttpConfiguration();
- var Controller = new unittestController();
- var Route = config.Routes.MapHttpRoute("DefaultApi", "api/unittest");
- var RouteData = new HttpRouteData(Route, new HttpRouteValueDictionary { { "controller", "unittest" } });
- var Request = new HttpRequestMessage(HttpMethod.Get, "http://localhost:58068/api/unittest/9/9/9");
- Controller.ControllerContext = new System.Web.Http.Controllers.HttpControllerContext(config, RouteData, Request);
- Controller.Request = Request;
- Controller.Request.Properties[HttpPropertyKeys.HttpConfigurationKey] = config;
- GetUserProfileIdForErrorLog objUserProfileId = new GetUserProfileIdForErrorLog();
- objUserProfileId.UserProfileIdForError(2792);
- ErrorObj objerror = new ErrorObj();
- objerror.UserProfileCurrentStatus = "8";
- var respone = Controller.Getcitylist();
-
- }
-
- catch (Exception ex)
- {
- Assert.Fail("Expected no exception, but got: " + ex.Message);
- throw;
- }
- }
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: