6
votes

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?

1
I have same question, have you figured it out? - kvuong
@kvuong Yes, 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 IWebHostBuilder reference I could directly use TestServer(IWebHostBuilder) to create the test client. Hope it will help you. - kiran
I believe, the former mate is the replacement approach to the latter. This is according to the latest blogs from MightySoft. - IbrarMumtaz

1 Answers

0
votes

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.