What’s New in ASP.NET Core 10 – Key Features & Expert Insights
We tracked the evolution of .NET 9 on Reddit, and watched the heated debates on .NET 6 vs .NET 8. And just when we thought that things had finally settled, Microsoft drops another...
Listening is fun too.
Straighten your back and cherish with coffee - PLAY !

Unit testing is used to test individual components or units to determine if there are any issues by the developer himself. The unit testing is used to validate that each component of the software code performs as expected. The unit testing is concerned with the functional correctness of standalone modules and it is done by the developer during the development phase of an application.
Unit testing is important because the software can fail in unexpected ways. Manual testing is slow, least reliable, and expensive to test an application. Whereas, unit testing saves time and money if it is implemented properly.
Here, are a few key reasons to perform unit testing:
There are many automated tools available to test the application.
Few examples as below:

Junit is a free testing tool used by the developer for Java programming language. It implements assertions to identify the test method. Junit is used to test data first and then inserted in the piece of code.

NUnit is a framework widely used by the developer to unit-test an application that is developed in all .NET languages. Nunit is open source and supports data-driven tests that run parallelly. Nunit allows the developer to write a script manually.
PHPUnit is a unit testing tool used by the developer to test an application. PHPUnit tests a small portion of code known as units. PHPUnit provides a pre-define assertion to assert that the system behaves in a proper manner.
JMockit is open-source Unit testing with line and path metrics. JMockit allows the developer to record and verifying syntax in API and offers Line coverage, Path Coverage, and Data Coverage.
EMMA is an open-source toolkit used by the developer for analyzing and reporting code written in Java language. Emma is a java-based external library and support method, line, basic block coverage type.
There are many test frameworks available in the market but we will be going to use the xUnit, which is a very popular testing framework. The simplest way to create unit test case for an ASP.NET Core web app project is to create a new test project using a template.
We can create a test class for each application class. The simple unit test includes three-step:
To create ASP.NET Core Console Application follow the below steps:
Step 1:Open Visual Studio, select File option -> New option -> Project
Step 2:elect the ASP.NET Core Console Application template and click Next.
Step 3:Enter the project name, and then click Create.
Step 4:Create a new class
Add new class Calculator.cs
namespace UniTestDemo
{
public static class Calculator
{
public static double Addition(double num1, double num2)
{
return (num1 + num2);
}
public static double Subtraction(double num1, double num2)
{
return (num1 - num2);
}
public static double Multiplication(double num1, double num2)
{
return (num1 * num2);
}
public static double Division(double num1, double num2)
{
return (num1 / num2);
}
}
}
1: In Visual Studio 2019, Right-click on solution-> Add -> Project.
2: In the Create New Project dialog, select the Test ->xUnit Test Project (.Net Core) template, and click Next.
3: Enter the project name, and then click Create.
Step 5:Add Reference to your project
Right-click on your Testing project (XTestDEmo)->Add->reference
Step 6:Now, Add one class in 'XTestDEmo' project named 'TestCalculator.cs'
In 'TestCalculator.cs' class we will write test case for method define in 'Calculator.cs'. These methods follow three-step: Arrange, Act, Assert.
The following class contains test cases for Calculator:
using System;
using UniTestDemo;
using Xunit;
namespace XTestDEmo
{
public class TestCalculator
{
[Fact]
public void Test_Addition()
{
// Arrange
var number1 = 4.9;
var number2 = 3.1;
var expectedValue = 8;
// Act
var sum = Calculator.Addition(number1, number2);
//Assert
Assert.Equal(expectedValue, sum, 1);
}
[Fact]
public void Task_Subtract_TwoNumber()
{
// Arrange
var number1 = 3.2;
var number2 = 1.5;
var expectedValue = 1.7;
// Act
var sub = Calculator.Subtraction(number1, number2);
//Assert
Assert.Equal(expectedValue, sub, 1);
}
[Fact]
public void Test_Multiplication()
{
// Arrange
var number1 = 2.5;
var number2 = 3.5;
var expectedValue = 8.75;
// Act
var mult = Calculator.Multiplication(number1, number2);
//Assert
Assert.Equal(expectedValue, mult, 2);
}
[Fact]
public void Test_division()
{
// Arrange
var number1 = 2.9;
var number2 = 3.1;
var expectedValue = 0.94; //Rounded value
// Act
var div = Calculator.Division(number1, number2);
//Assert
Assert.Equal(expectedValue, div, 2);
}
}
}
Step 7: Now, Run your test cases
For running all test cases open Test Explorer where you will find you're all test case. To run click on RunAll this will start executing test cases.
To open Test Explorer in visual studio Click on Test option->Window->Test Explorer.
Once your test case runs successfully, the output screen will be shown as below.
In this blog, we have discussed the unit testing with ASP.NET which is used by the developer for issuing defects and we have also portrayed the purpose of unit testing. We illustrated a simple example of a unit test with Xunit using the ASP.NET Console application. We hope you will acquire a clear understanding on how to integrate unit testing.
We tracked the evolution of .NET 9 on Reddit, and watched the heated debates on .NET 6 vs .NET 8. And just when we thought that things had finally settled, Microsoft drops another...
Several experts, particularly those just starting out, often find themselves confused about WPF and UWP, wondering if they are the same. Since they are used to create great UIs for...
Many companies rely on advanced technologies to make informed decisions in their business. For instance, some Chief Technology Officers (CTOs) turn to Power BI consulting services,...