Integration Tests in WebAPI + .NET Core 2.0

Avishek Patra
3 min readFeb 19, 2018

Introduction

Integration testing in WebAPI what does it mean?

It means you’ll be writing test cases which will call your API’s in real-time and you’ll be validating the response. So you’ll not be checking the Unit functionality but the end to end functionality.

So let’s create a web API project in .Net Core, I hope that people are already familiar with that, however, I’ll add a few screenshots which will help to understand the newbies

Open Visual Studio 2017/VS Code.

If you’re using VS 2017 then navigate to File->New-> Project. You’ll receive the following window

If you’re not getting the same, then click on the web in the left panel then you’ll be getting the above, select the ASP.NET Core Web Application. Once you click on “OK” you’ll be getting another dialogue like this

Select the WebAPI option and click “Ok”, this will create a WebAPI project in your visual studio.

Now let’s add a xUnit project. Again click on the File->New->Project

Navigate to the following option in the opened dialog

If you’re using VS code then either open console or .net CLI then type the following two commands to create the web API and xunit test project(make sure you’ve moved to your working directory before running the commands)

This will create a WebAPI project with a Controller File (ValuesController, under controllers folder) and a xUnit Project with a Test file.

Now when we do integration test we need a host server which will actually host the web API services during this process. So we have to install a NuGet package for this

Package manager
Install-Package Microsoft.AspNetCore.TestHost -Version 2.0.1

.Net CLI
dotnet add package Microsoft.AspNetCore.TestHost --version 2.0.1
This NuGet package will install the required dependencies to write workable tests

Now let’s rename the UnitTest1.cs file to something more relevant, I have made it ValuesControllerTests.cs since this file will contain the integration tests for ValuesController.cs which is the default controller comes with WebAPI.

Let’s modify the controller like this

private readonly TestServer _testServer;
private readonly HttpClient _testClient;
public ValuesControllerTests()
{
//Initializing the test environment
_testServer = new TestServer(new WebHostBuilder()
.UseStartup());
//Test client creation
_testClient = _testServer.CreateClient();
}

So here TestSever will help us to create a test server which will host WebAPI, HttpClient will be the agent which will call the WebAPI.
_testServer = new TestServer(new WebHostBuilder().UseStartup());
If you give a closer look to this line then you can see TestServer constructor takes a parameter, we are initializing the WebHostBuilder class which will host the to be tested project, Again calling UseStartup method and passing Startup class of our web API project. This will tell WebHostBuilder to host our web API in the test environment.

Now we are good to write our first test
Let us write something like this

[Fact]
public async Task TestGetRequestAsync()
{
var response = await _testClient.GetAsync("/api/values/");
response.EnsureSuccessStatusCode();

var result = await response.Content.ReadAsStringAsync();

Assert.Equal(“[\”value1\”,\”value2\”]”, result);
}

So we are calling AP, and then we are Asserting on the response. Lets Press Ctrl+R,T this will execute the test case and will be displayed like this

You can find the codes related to this article in the below repository.
Click Here
I hope you have liked the article, Please share your opinions in the comments section below.

Originally published at http://coderera.wordpress.com on February 19, 2018.

--

--