Asp.Net Core Web Api Integration testing using EntityFrameworkCore LocalDb and XUnit2

Compared to unit testing, Integration testing gives us a better return on test investment.

Nowadays, we have a lot of tools to set it up easily, and it can go faster and gives us more confidence with code we have changing knowing we have not breaking existing functionalities.

It can help us to catch regression errors, before we deploy to a test environment, and fewer bugs in new features, resulting Higher velocity and higher quality solution

Unit testing test behaviour of a class and drive its implementation, run in memory and mocks the Data Access Laywer while Integration testing test the entire system and focus on interactions between many classes, uses the real database and test the Data Access Laywer and its value is to catch regressions. So Integration testing may catch errors that passes on  unit testing

In my previous tutorial , Asp.Net Core Web Api Integration testing using InMemory EntityFrameworkCore Sqlite and XUnit2 I used SqlLite InMemory

Note : InMemory is not a relational database. It is designed to be a general purpose database for testing, and is not designed to mimic a relational database.  So it will allow us to save data that would violate referential integrity constraints in a relational database.

In this tutorial, I will talk about Asp.Net Web Api Core Integration testing using EntityFrameworkCore LocalDB and Xunit2

So lets go ahead and implement some integration tests for the solution discussed on this tutorial Token Based Authentication using Asp.net Web Api Core

Lets start and create a XUnit Test Project using Visual Studio 2017

XunitTestProject

Add reference to the project TokenAuthWebApiCore.Server  that represent our system under test

XunitTestProject XunitTestProject2

INTEGRATION TESTING USING LOCAL DB

Lets install some dependencies

 

Lets create a Generic TestFixture  class, here we are going to create a Test Server to  target our API  Server http://localhost:58834

So we build a host by creating an instance of WebHostBuilder.  And  our tests classes will inherits from IClassFixture<TestFixture<OurTestStartup>>

Next, open Startup.cs file of project TokenAuthWebApiCore.Server  and add 2 virtual methods SetUpDataBase and EnsureDatabaseCreated. So this 2 methods will be overriden in our test StartUp class to use a test database like  SqlServer Local DB or any other database system

Here we are using LocalDB, so lets create a TestStartupLocalDb class that inherits from Startup

Next , overrides  SetUpDataBase and Configure LocalDB, so we will use LocalDB instead of the production database

Next , overrides   EnsureDatabaseCreated to ensures that the database for the context exists. Here I will destroy and recreate the database

Here is complete code of TestStartupLocalDb  class

Next, create our first Test class AuthControllerRegisterUserTest and inherits it from IClassFixture<TestFixture<TestStartupLocalDb >>

 

Next, lets order tests methods , because to be able to get a existing user, we must register it first.   So create a PriorityOrderer class that implement ITestCaseOrderer  and  TestPriorityAttribute class and implement them as follow

TestPriority

TestPriority2 TestPriority3

Next, create our first Test class AuthControllerTokenTestand inherits it from IClassFixture<TestFixture<TestStartupLocalDb >>  and implement it

Decorate test methods  with TestPriority(1)  and TestPriority(2). So  the method decorated with TestPriority(2) will be executed  after the method decorated with TestPriority(1)  that is  UserRegistration Test

 

Create  an AssemblyInfo.cs file  and add the line  [assembly: CollectionBehavior(DisableTestParallelization = true)]  to disable parallelism execution.

Thank you for reading

sample code is available here  Asp.Net-Web-Api-Core-Integration-testing-using-EntityFrameworkCore-LocalDB-and-Xunit2

 

Gora LEYE

I’m a .NET Architect and Technical Expert skills located in Paris (FRANCE). The purpose of this blog is mainly to post general .NET tips and tricks,

Leave a Reply