Testing ASP.NET Core applications is a series of articles that will help you learn about writing automated tests at different levels for our ASP.NET Core application.

What we are going to cover is the testing process for model and controller classes, integration tests, and automated UI tests. To test our code, we are going to use the xUnit library and talk about the testing attributes and functionalities it provides for us. Furthermore, we are going to explain how to isolate classes being tested with mock objects and how to use the Moq library for that purpose.

Once we start writing integration tests, we are going to explain how to set up the in-memory database instead of using the real one and how to include anti-forgery tokens in our test code.

The last part of this series is going to cover automated UI testing with the Selenium tool.

But, Why Should We Test Our Code?

The primary reason is to check the quality of the software we are writing. By writing tests we can discover potential bugs in a development phase, which is a much better situation than finding them in a production phase. As a result, once our application is published, we won’t spend time fixing bugs, but developing new features, which is a much better scenario, don’t you agree?

What’s more, we develop a better understanding of the system by testing our code, thus providing better documentation as well.

Overall, all these reasons and many others as well lead to reduced costs of the system.

Background

For this tutorial, we recommend having:

We are going to separate this tutorial into several parts:

So, by the end of this series, you will have a good understanding of testing your MVC application at different levels.

We hope you will enjoy this tutorial and learn a lot.

Let’s get started.