.NET Core Apps Integration Testing

By | May 26, 2019

Integration tests evaluate an application on a broader level than unit tests. Unit tests are used to test isolated software components, such as individual class methods. Integration tests confirm all layers are properly working when fully processing a request. Basically, Integration tests are used to test the app’s infrastructure including databases and file systems which can make them time consuming when run and requires lot of set up code.

Before advent of .NET Core, writing integration tests for .NET environment is bit of challenge. But, with the release of ASP.NET Core, whole end-to-end MVC and Web API testing has become simpler. A test host in provided which helps runs in-memory while providing a HTTP client to test the application

Having this test host available means, we can write tests that look and execute quickly like unit tests but exercise almost all layers of our code without the need for external components

Key guidelines for integration tests:

  1. Limit integration tests to the most important infrastructure scenarios.
    If a behavior can be tested using unit test then choose the unit test.
  2. Preferably, build integration tests to target in-memory database. Despite the fact, there is an upfront cost in dealing with data seeding and setup, consider it as a long term investment toward improving code base integrity and maintainability.
  3. Integration tests when hooked with in-memory database provides an idempotent/deterministic effect for every test session
    avoiding hassle of data cleanup when connected to real database. Of course, there is cost associated with set up and tear down events for every test case.
  4. Limit integration tests interaction with real database for read operations to avoid data loss in database environments.

EmployeeController Integration Tests

Given below is our psuedo EmployeeController which provides CRUD methods for employees for which we will write integration tests to verify EmployeeController HTTP methods are working as expected.


Integration test project dependencies

To write integration tests in ASP.NET Core for EmployeeController, we should first define tests in a test project. This test project will have a reference to target EmployeeAPI ASP.NET Core project. Test project also needs specific Nuget packages to create and run integration tests which are shown below.

As shown here, Integration test project has reference to EmployeeApi. You can also required nuget packages to build and run .NET Core integration tests

EmployeeApi.IntegrationTests.csproj packages

Microsoft.AspNetCore.Mvc.Testing package dependency

The key dependency to implement integration tests in ASP.NET Core is
Microsoft.AspNetCore.Mvc.Testing package because it gives us TestServer class, crucial for building in-memory test server which in turn forms basis for creating API integration tests and executing them. Another class WebApplicationFactory also provides necessary bootstrapping to run integration tests against target API project.

Customized WebApplicationFactory class

However, for our project, we had to define custom WebApplicationFactory called EmployeeControllerTestFactory using IWebHostBuilder. The IWebHostBuilder gives opportunity to customize services which in our case to inject light-weight in-memory database seeded with mock data rather than using real database for integration tests and we can also setup separate appSettings file just for testing scenarios. On the other hand, we can either leverage Startup class from host EmployeeAPIproject for creating entry point for TestServer or create a separate Startup class for testing use case too. For our demo, all we need is custom WebApplicationFactory as shown below and rest of infrastructure can be leveraged from underlying framework.


EmployeeController integration tests

After defining custom WebControllerFactory class EmployeeControllerTestFactory, we proceed to implement EmployeeController test class using EmployeeControllerTestFactory. The factory class bootstraps the host EmployeeController and exposes HttpClient to build and run tests. Given below set of integration tests targeting EmployeeController GET, POST, PUT and DELETE methods


You can download full source code using below link