Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.Net Core 3 Worker Integration Tests

For integration testing Asp.Net Core application we have WebApplicationFactory. But how to test Worker (HostedService) that is not web application? I don't want to copy the whole DI configuration in SUT like in this question. Is it possible to use original configuration and override some of dependencies like we do for web application in WebApplicationFactory.WithWebHostBuilder?

like image 485
Artur Avatar asked Mar 22 '20 06:03

Artur


People also ask

What is WebApplicationFactory?

WebApplicationFactory<TEntryPoint>() Creates an instance of WebApplicationFactory<TEntryPoint>. This factory can be used to create a TestServer instance using the MVC application defined by TEntryPoint and one or more HttpClient instances used to send HttpRequestMessage to the TestServer.

What is Microsoft ASP.NET Core TestHost?

ASP.NET Core includes a library Microsoft. AspNetCore. TestHost which contains an in-memory web host. This lets you send HTTP requests to your server without the latency or hassle of sending requests over the network.

What is integration test in ASP NET Core?

Thank you. Integration tests ensure that an app's components function correctly at a level that includes the app's supporting infrastructure, such as the database, file system, and network. ASP.NET Core supports integration tests using a unit test framework with a test web host and an in-memory test server.

What is the unit test framework for integration tests?

ASP.NET Core supports integration tests using a unit test framework with a test web host and an in-memory test server. This topic assumes a basic understanding of unit tests. If unfamiliar with test concepts, see the Unit Testing in .NET Core and .NET Standard topic and its linked content.

What should be the scope of integration testing?

Always scope your integration testing. They should be narrow and should verify integration with downstream and upstream both. Test the only permutation of the Test scenarios. For more details please visit the below article as we deep dive into a few additional scenarios including API Contract Testing.

Do I need to override createhostbuilder in Integration Test Project?

If you happen to create a service from scratch using .NET Core 3.1 WebAPI template then for the Integration test project, you don’t need to override CreateHostBuilder. This I found as per the design update in the .NET Core 3.1 version.


1 Answers

I wasn't able to find any solution for Worker services integration testing hence I made adoption of WebApplicationFactory. The source code and usage example can be found here: https://github.com/gao-artur/WorkerService.Testing

like image 184
Artur Avatar answered Oct 12 '22 23:10

Artur