Unit Testing with xUnit in ASP.NET Core

Hi All! Hope you all are doing well. Today I am telling you about unit testing with xUnit in ASP.NET Core.

Developers should love to write unit testing, because it will help to improve our code, and also it will reduce some extra works in early-stage when we develop the application.

Unit Testing with xUnit in ASP.NET Core

In this article, I will tell you,

  1. Unit testing
  2. Unit testing is important
  3. Unit testing with ASP.NET Core

Unit Testing

It starts with the unit, the unit is a small piece of code. Usually, the unit is a single method in our code. So we test these units for making sure to its work and give output as we expect. It’s the basic and simple idea of unit testing.

Unit testing is important

  • It improves the quality of the code.
  • Find software bugs very early
  • It’s also useful for the process of agile.
  • Reduce Cost

Unit testing with ASP.NET Core

We can do unit testing with the xUnit tool in ASP.NET Core. xUnit is a free, open-source, community-focused unit testing tool for the .NET Framework

For that, first you have to create an ASP.NET Core project. Then you have to create a folder called Repository class for repository logics with their interface. This will be quite helpful when you write the test code for the controllers.

Also, we didn’t use a database for this example. Because I want to show how to do unit testing with xUnit in ASP.NET Core by using a simple example.

In this example, we are planning to test creating employee logic. So for that, first create Employee Model class.

Employee model class

So now we created the Employee model class. Now you have to create an interface called IEmployeeService and EmployeeService for the write our logical part in our code.

For that, you have to write a logical part to create employees.

IEmployeeService.cs

IEmployeeService for abstraction layer

EmployeeService.cs

EmployeeService class

You can see the IEmployeeService interface and EmployeeService class which implements the IStudentRepository interface. Also, this interface is planned to be an abstraction layer. Also you can see CreateEmployee function in EmployeeService class. We didn’t use any database logical part for this. Because I want to show you how to do unit testing with xUnit in ASP.NET Core by using a simple example.

So Now we created create employee function. Now we have to write test cases to do unit testing. For that, we have to create and add a new .NET Core xUnit Test project to our solution.

Create and add new xUnit test project
Solution structure after adding the xUnit test project

You can see the project structure after we created the new .NET Core xUnit Test project in above image. Also we have to add a FoodOrder project as project reference for FoodOrder.Test project like the below images. Then we can access the model class and other classes in that project.

add a FoodOrder project as project reference-1
add a FoodOrder project as project reference — 2

Now we have to rename the UnitTest.cs file as EmployeeTest.cs. After that, we have to write test case to create employee logic.

Before writing the test case,

You can see [Fact] attribute, which is used by xUnit framework marking them as the actual testing methods.

Also when writing unit tests it is usually the practice to follow the AAA principle (Arrange, Act, and Assert).

Arrange — this is where you would prepare everything for the test.(creating the objects and setting them up as necessary)

Act — this is where the method we are testing is executed.

Assert — this is the final part of the test where we compare what we expect to happen with the actual result of test method execution.

Now we have to write test case.

Test cases for creating employee function

So you can see our test cases that write for creating employee function. In these test cases, we check passing Invalid employee Id, existing employee Id, and also valid status by passing employee ID. Then you can see Assert.False(). it check the return value. if return value is false, test case will be pass and if it is true then test case will be failed. Also the same goes for Assert.True(). Like Assert.True() and Assert.False(), there are more assertions to do our test cases.

After writing the test cases, you can run all test cases as below image.

How to run all the tests

Then you can see test passes. Also, we can check test cases by pass invalid input. Then we can get failed test results.

So That’s it for today. I think you learned something new from my article.

See you again soon with another article !!

Happy Coding !!!

Undergraduate @ SUSL | Software Engineer