Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASPNetCore Web API integration tests using Test Host or WebApplicationFactory

I am writing Integration Test for an ASPNetCore web API project. While reading about it I came across two terminologies, first is

Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory

and second is

Microsoft.AspNetCore.TestHost

References:

  1. https://docs.microsoft.com/en-us/aspnet/core/test/integration-tests?view=aspnetcore-2.1

  2. https://koukia.ca/integration-testing-in-asp-net-core-2-0-51d14ede3968

I am a little confused, why and which kind of setup I should use to test Web API controllers?

like image 528
kiran Avatar asked Oct 15 '18 09:10

kiran


1 Answers

I am following an older approach (TestHost)

because I had my startup class in another assembly (internal) and I had been exposing IWebHostBuilder to my API project. So, in that case, I could not able to use WebApplicationFactory which takes Startup.cs class. And as I have an IWebHostBuilder reference I could directly use TestServer(IWebHostBuilder) to create the test client.

like image 152
kiran Avatar answered Sep 18 '22 17:09

kiran