Integration Testing an Asp.Net Core Web API with xUnit.net

Summary

In this post I would like to show you how to go about adding Integration tests to your Asp.Net Core Web API project using Xunit.net.

Pre-requisites

Why I would want to add Integration testing to my Web API

Integration tests are a great way to test infrastructure connectivity of various components of a solution such as testing request/response to your Web API, against external systems such as databases file systems etc..

Why am I choosing to use xUnit.net as my test framework

ASP.NET Core uses it internally to test the product. So it will be well supported and improved throughout Asp.Net Core’s evolution.

Asp.Net Core makes it easy and efficient to perform integration testing due to its support of a test web host and and in-memory test server allowing us to make actual HttpClient requests without going over the wire so no firewall or network config issues to deal with as well as this these calls are fast whilst still working with routing, model binding & validation etc.

I will use an already created out of the box Asp.Net Core Web API which will be our ‘SUT’ or System Under Test and create our Test(xUnit.net) project which will be the project containing and executing our tests against our SUT using the test runner.

Lets’ go….

I am purposely neglecting clean typical solution folder structure, exception handling etc. in order to keep the post focused on the objective.

This is how the Solution currently stands

image

It just has an out of the box Web API project I called Test.API

Create the Test project

Right click the solution and select Add then New project

type xunit in the search field on the top right of the window, the results should be filtered and you should see  ‘xUnit Test Project(.Net Core)’

image

select it and name the project ‘IntegrationTests’

Lets go ahead and delete the class UnitTest1.cs file that gets created by default and create two new classes called:

‘TestScenarios’ and ‘TestScenarioBase’

TestScenarios is our class containing our tests and TestScenarioBase is our base class containing the code to create a server to fire up our API in order to call that we will leverage in our out TestScenarios class.

Within the base class make the class public and paste the below method in to the class

We will want to add the following Nuget packages :

  • Microsoft.AspNetCore
  • Microsoft.AspNetCore.TestHost
  • Microsft.AspNetCore.MVC

And we will need to add the folowing namespaces to the top of our class

  • using Microsoft.AspNetCore;
  • using Microsoft.AspNetCore.Hosting;
  • using Microsoft.AspNetCore.TestHost;
  • using Test.API;

The class should finally look like the below

image

In our TestScenarios class lets add the following code

Inherit from TestScenarioBase

Reference the namespaces:

  • using System.Threading.Tasks;
  • using Xunit;

Make the class public

The class should finally look like the below

image

In the code above we are calling our Get method in our Values Controller passing in an arbitrary value(5) to receive a response back to compare – by default our method simply returns “value”

Run the test using the test runner and we should receive a Successful test run.

That’s it!

In my MyReverie repository in GitHub I have implemented Integration Testing in to my Solution if you would like to see the code I referenced.