ASP.NET Core 2+ best practices and practical tools for testing, part 1


When developing an application, two big questions are coming:

  • How can we know and verify if an implementation remains compliant with the expected operating rule, preventing regressions in case of evolution of the program ?
  • How can we know and verify if the behavior of each software layer between them ?

The answers are definitely: Make unit and integration tests!

But how can we achieve that?

This article will describe the best practices and the practical tools that can be used to make efficient unit and integration tests.

Philosophy and good practices of unit tests

  • The class tested (SUT) in complete isolation
    • Some exceptions are allowed (mock files)
  • The test must be easy to write, if not, refactor!
  • The test must run quickly
    • Some exceptions are allowed especially when your business logic has complex computation
  • The test must easy to understand
    • Respect coding conventions
    • Normalize tests name to understand what is does
    • Don’t hesitate to use nested classes to group your tests on the same SUT
    • Identify clearly each step (Arrange, Act, Assert)
  • Test only one behavior
    • But testing one behavior doesn’t one Assert!
  • Don’t try to test at 100% your code
    • It’s unefficient
    • It’s very expensive

Philosophy and good practices of integration tests

  • Have to run them after unit tests
    • It’s better to test first if each component works by itself before checking that it integrates with others
  • Ideally make an end-to-end test instead of testing a service and a repository
    • It’s more efficient and there is less code to write

Practical tools for testing

xUnit is a test execution tool, the most used and supported by the community:

NSubstitute is a mocking library, very popular and very easy to use:

FluentAssertion is an assertion library, popular. Makes easier to understand the intent of your tests:

AutoFixture is a practical variables setup tool, minimize the time spent to arrange a test. Gain in readability and maintainability:

ExpectedObjects Allows to compare objects by their value (not their reference). No need anymore to test each property. Gain in readability and maintainability:

JsonSerializer, very usefull in certain cases to generate data:

TestServer is a test web host and an in-memory test server. It’s an easy way to make end-to-end tests:

FakeJwt is simply the Microsoft.AspNetCore.Authentication.JwtBearer assembly rewritten with fake validation. Originally rewritten by “GestionSystemesTelecom” company here:, I had to modify it to make it compatible with ASP.NET Core 2.2. It can be downloaded here: It allows to use fake JWT for integration tests, no need to use a real JWT configuration to run your integration tests. *

* Some examples of use have already been written by Dominique Saint-Amand here:

In the part 2 I will provide other examples.

Written by


Anthony is a specialist in Web technologies (14 years of experience), in particular Microsoft .NET and learns the Cloud Azure platform. He has received twice the Microsoft MVP award and he is also certified Microsoft MCSD and Azure Fundamentals.
%d bloggers like this: