Introduction
- Unit Testing is a software design pattern that is used to test the smallest components in the software development phase.
- Unit Testing is used to validate the functionality which is to create expected output before going to the production environment and QA Team.
- It helps to detect issues at the early phase of the software development cycle
- There are many unit test tools that are already present while using .NET Framework like xUnit, NUnit, and many more.
xUnit
- xUnit is a free and open-source Unit testing framework for .NET development
- xUnit has many features which provide for writing a clean and good unit test case.
- It has many attributes like Fact, Theory, and many more to write test cases effectively and cleanly and also provides a mechanism to create our own attribute
Attributes of xUnit
[Fact] attribute is used by xUnit in .NET which identifies the method for unit test
[Fact]
public void EvenNumberTest() {
//Arrange
var num = 6;
//Act
bool result = Mathematics.IsEvenNumber(num);
//Assert
Assert.True(result);
}
[Theory] attribute is used to supply parameters to the test method
[Theory]
[InlineData(5)]
public void OddNumberTest(int num) {
//Act
bool result = Mathematics.IsOddNumber(num);
//Assert
Assert.True(result);
}
Test Pattern
Arrange-Act-Assert is a great way to write clean and more readable unit test cases
Arrange
In the arrange section we setup and declare some inputs and configuration variable
Act
In the Act section, we put main things and functionality like method calls, API calls, and something like that
Assert
Assert checks expected outputs and check whether they will match our functional requirement or not
Let’s start with a practical example using .NET Core 6 and xUnit.
Step 1
Open the VS Code and create one directory “UnitTesting” and then create a new solution over there using the below command
dotnet new sln
Step 2
Create a new TestDemo class library project
dotnet new classlib -o TestDemo
Step 3
Then go to the directory where the solution file is and then add the TestDemo project into the solution file
dotnet sln add .\TestDemo\TestDemo.csproj
Step 4
Later on, create the xUnit project TestDemo.Tests
dotnet new xunit -o TestDemo.Tests
Step 5
Add the TestDemo.Tests xUnit project into the solution file
dotnet sln add ./TestDemo.Tests/TestDemo.Tests.csproj
Step 6
Add the TestDemo project reference to the TestDemo.Tests project which is related to xUnit
dotnet add ./TestDemo.Tests/TestDemo.Tests.csproj reference ./TestDemo/TestDemo.csproj
Step 7
Project Structure
Step 8
Create one Mathematics class in TestDemo Project as shown below
namespace TestDemo;
public static class Mathematics {
public static bool IsEvenNumber(int num) {
return num % 2 == 0 ? true : false;
}
public static bool IsOddNumber(int num) {
return num % 2 == 1 ? true : false;
}
public static int SquareOfNumber(int num) {
int sum = 0;
for (int count = 1; count <= num; count++) {
sum = count * count;
}
return sum;
}
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);
}
}
Step 9
Next, create the UnitTest1 class in the TestDemo.Tests in that we write different test cases
namespace TestDemo.Tests;
public class UnitTest1 {
[Fact]
public void EvenNumberTest() {
//Arrange
var num = 6;
//Act
bool result = Mathematics.IsEvenNumber(num);
//Assert
Assert.True(result);
}
[Fact]
public void OddNumberTest() {
//Arrange
var num = 5;
//Act
bool result = Mathematics.IsOddNumber(num);
//Assert
Assert.True(result);
}
[Fact]
public void SquareOfNumberTest() {
//Arrange
var num = 5;
var expectedResult = 25;
//Act
var result = Mathematics.SquareOfNumber(num);
//Assert
Assert.Equal(expectedResult, result);
}
[Fact]
public void AdditionOfNumber() {
//Arrange
var num1 = 5;
var num2 = 2;
var expectedResult = 7;
//Act
var result = Mathematics.Addition(num1, num2);
//Assert
Assert.Equal(expectedResult, result);
}
[Fact]
public void SubtractionOfNumber() {
//Arrange
var num1 = 5;
var num2 = 2;
var expectedResult = 3;
//Act
var result = Mathematics.Subtraction(num1, num2);
//Assert
Assert.Equal(expectedResult, result);
}
[Fact]
public void MultiplicationOfNumber() {
//Arrange
var num1 = 5;
var num2 = 2;
var expectedResult = 10;
//Act
var result = Mathematics.Multiplication(num1, num2);
//Assert
Assert.Equal(expectedResult, result);
}
[Fact]
public void DivisionOfNumber() {
//Arrange
var num1 = 10;
var num2 = 2;
var expectedResult = 5;
//Act
var result = Mathematics.Division(num1, num2);
//Assert
Assert.Equal(expectedResult, result);
}
}
Step 10
I will suggest you install the .NET Core Test Explorer for the test case
Step 11
Finally, you can run your test case as shown below and you can the output of all your test cases if that will be executed properly then the green click comes in front of the test case name and below the [Fact] attribute in the Mathematics class and if the expected output is not produced by the application then cross sign is come in front of test case and below the [Fact] attribute
Conclusion
We discussed what is a unit test case and its purpose, and later on, the introduction of xUnit, attribute, and some patterns. Also, the practical implementation using .NET Core 6 in VS Code step-by-step