In this lesson, you will learn how to bootstrap an in-memory instance of your web host to perform end-to-end integration testing.
Up until now, we have used mocks to isolate components. While mocks are fast, they don't verify if your routing, middleware, or configuration actually function correctly when wired together. WebApplicationFactory<TEntryPoint> bridges this gap by booting your actual application inside a test process, using an In-Memory test server rather than a real network port.
How it works
The WebApplicationFactory class creates an in-memory TestServer. To use it, you create a test class that inherits from IClassFixture<WebApplicationFactory<TEntryPoint>>. The TEntryPoint is the Program class of your API or Web application. Once bootstrapped, the factory provides an HttpClient that intercepts requests directly into the application's middleware pipeline, bypassing the network stack entirely.
Concrete Example
Here is how you bootstrap the host and send an end-to-end request:
public class WeatherApiTests : IClassFixture<WebApplicationFactory<Program>>
{
private readonly WebApplicationFactory<Program> _factory;
public WeatherApiTests(WebApplicationFactory<Program> factory)
{
_factory = factory;
}
[Fact]
public async Task Get_EndpointsReturnSuccess()
{
// The factory handles the hosting lifecycle
var client = _factory.CreateClient();
// The request hits your actual routing and controllers
var response = await client.GetAsync("/weatherforecast");
response.EnsureSuccessStatusCode();
}
}
Why use this?
By using WebApplicationFactory, your tests exercise the real Program.cs configuration, including your dependency injection container, database contexts (which you might swap for an In-Memory provider), and authentication handlers. Because the host is spun up in-memory, the tests remain fast while providing high-fidelity feedback about whether your application starts and handles requests correctly.
It is the definitive way to move from "testing components in isolation" to "verifying the integrity of the entire web application."
Key Takeaway: WebApplicationFactory bootstraps your real application pipeline in-memory, allowing you to run end-to-end integration tests without the overhead of network sockets or external deployment.