Updated March 29, 2023
Introduction to ASP.NET Core Unit Testing
ASP.NET Core Unit Testing is the testing part of an application where we need to write tests to the application code. The name unit itself defines that the unit of work, so it is a single method testing in our code. Unit testing basically involves the testing part of the application in isolation from its dependencies and infrastructure. The unit test controller logic will test only the content of a single method or action, not the framework’s or dependencies’ behavior.
What is ASP.NET Core Unit Testing?
Unit testing is the software testing that engages in the accurate testing of components in isolation or single independent units. This unit testing will improve the quality of code, and it also helps to identify the various issues at the initial stage of the development process. The name unit itself defines that unit of work, so the smallest component of the software to be tested is a single method of testing in our code.
How Should We Write Unit Test Cases?
Basically, the unit test is the code we can write to test our application code; the test project will require the dependency of the app project for testing. Unit testing engages with the testing part of an application where we need to write tests to the application code. The controller logic of unit testing will test only the content of a single method or action but not the behavior of the framework or dependencies of the method.
The latest one is called xUnit.net, an open-source unit testing tool a community-focused tool for .NET Framework. There are various testing tools for .NET Framework, but xUnit.net was the popular one. xUnit make available with a set of attributes for testing methods with the help of writing codes to test the units and components; the attributes are as follows,
Fact – it is the attribute where we want a method to be a part of unit testing, and it executes during test execution must be decorated with these attributes.
Theory – in this attribute, if we want to send the parameters to the test method, we require using this attribute, which executes the method and offers the parameters to test the data.
InlineData – this attribute offers the parameters to test data; it is used along with the theory attribute.
While writing the unit test case, it should be easily maintainable and well organized; it is the best way to write tests for single functionalities. Mainly the unit tests, which should not depend on any other unit tests able to run our unit test in any order which we require. We need to follow the naming test conventions for each and every unit test we write. In unit testing, we need to follow such things they are as follows,
- The unit test should be clear by seeing the test name itself; no one should spend time for identifying what the test is, unit tests should be readable.
- The test code should be the same as the production code; we need to write our tests with minor changes only; it should not affect all of our tests, unit tests must be maintainable.
- The unit test should not take much time to execute because no one likes to wait for a long time for the tests to execute. Therefore, unit tests should be fast.
- The main important thing is that there should not be any dependency needed to execute the test without giving access to any database or any external system. The unit test requires running with complete Isolation.
- The final test must give us full assurance; we are able to identify the errors before they arrive at the production. Therefore, a unit test must give reliable tests.
Creating ASP.NET Core Unit Testing
To create the ASP.NET Core Web API Application project, let’s see the sample implemented for the Calculator Service application, add the interface called ICalculatorService for calculator operation, and to create the Concrete Class called CalculatorService in the Service Folder.
ICalculatorService.cs
public interface ICalculatorService
{
double Add_Calc(double val_1, double val_2);
double Subtract_Calc(double val_1, double val_2);
double Multiply_Calc(double val_1, double val_2);
double Divide_Calc(double val_1, double val_2);
}
CalculatorService.cs
public class CalculatorService : ICalculatorService
{
public double Add_Calc(double val_1, double val_2)
{
return (val_1 + val_2);
}
public double Divide_Calc (double val_1, double val_2)
{
if (val_2 == 0)
{
throw new DivideByZeroException("val_2 not be zero");
}
return (val_1 / val_2);
}
public double Multiply_Calc (double val_1, double val_2)
{
return (val_1 * val_2);
}
public double Subtract_Calc (double val_1, double val_2)
{
return (val_1 - val_2);
}
}
Lets will register for ICalculatorService in the startup.cs file in ConfigureService() method.
public void ConfigureServices(IServiceCollection services)
{
//remaining code
services.AddTransient<ICalculatorService, CalculatorService>();
}
To create the controller called CalculatorController, let’s see the following code below.
CalculatorController.cs
[Route("api/[controller]")]
[ApiController]
public class CalculatorController : ControllerBase
{
private ICalculatorService _calculatorService = null;
public CalculatorController(ICalculatorService calculatorServices)
{
_calculatorService = calculatorServices;
}
[HttpPost]
[Route("Add")]
public double Add_Calc(double val_1, double val_2)
{
return _calculatorService.Add(val_1, val_2);
}
[HttpPost]
[Route("Divide")]
public double Divide_Calc(double val_1, double val_2)
{
return _calculatorService.Divide(val_1, val_2);
}
[HttpPost]
[Route("Multiply")]
public double Multiply_Calc(double val_1, double val_2)
{
return _calculatorService.Multiply(val_1, val_2);
}
[HttpPost]
[Route("Subtract")]
public double Subtract_Calc(double val_1, double val_2)
{
return _calculatorService.Subtract(val_1, val_2);
}
}
The above code is the easy setup for the API project application will now see the unit test on this application by applying it.
ASP.NET Core Unit Testing API testing project
Let’s create the xUnit Project to testing project by creating it; here is the xUnit, which is an open-source unit testing tool, a community-focused tool for .NET Framework. There are various testing tools for .NET Framework, but xUnit.net was the popular one. xUnit makes available a set of attributes for testing methods with the help of writing codes to test the units and components.
In the same solution, create the xUnit Project, once creating; it looks like, as shown below,
Here the testing part consists of basically three logical parts they are,
Arrange – it is prepared for the testing which is to be required, e.g., data used for testing with expected results.
Act – it is used to call method for tested which prepared in arrange, it returns the exact result.
Assert – it is to compare with actual and expected results here we need to decide if test pass or fail.
CalculatorControllerTest.cs
public class CalculatorControllerTest
{
private CalculatorService _unitTesting = null;
public CalculatorControllerTest()
{
if (_unitTesting == null)
{
_unitTesting = new CalculatorService();
}
}
[Fact]
public void Add_Test()
{
//here is the arrange logic
double val_1 = 5;
double val_2 = 3;
double expected_result = 8;
//here is the act logic
var actual_result = _unitTesting.Add(a, b);
//here is the assert logic
Assert.Equal(expected_result, actual_result, 0);
}
}
In the Add_Test() with [Fact] attribute, let’s see the above code briefly, which makes part of the test runner, Assert.Equal used to compare the expected results with the exact one for confirming that whether the test succeeded. Like the way for the other operations.
When executing the tests, the “Test Explorer” likes, as shown below, the tests passed like this.
Conclusion
This article has explained the ASP.NET Core Unit testing; in this testing, we have created various tests for validations and the logic used for the above application. I hope the article helps you understand creating the unit testing process.
Recommended Articles
We hope that this EDUCBA information on “ASP.NET Core Unit Testing” was beneficial to you. You can view EDUCBA’s recommended articles for more information.