Table of Contents

WebApplicationTestFactory Class

Definition

Namespace
Codebelt.Extensions.Xunit.Hosting.AspNetCore
Assemblies
Codebelt.Extensions.Xunit.Hosting.AspNetCore.dll
Source
src/Codebelt.Extensions.Xunit.Hosting.AspNetCore/WebApplicationTestFactory.cs

Provides a set of static methods for ASP.NET Core testing that bootstraps an existing application entry point.

public static class WebApplicationTestFactory
Inheritance
WebApplicationTestFactory

Examples

The test project references a minimal ASP.NET Core application with a /health endpoint. WebApplicationTestFactory uses ManagedWebApplicationFixture<TEntryPoint> by default, runs that real entry point on TestServer, applies a test-only service override through IWebHostBuilder, and gives the test an owned host from which it creates an HTTP client. Pass an explicit IWebApplicationFixture<TEntryPoint> when the test needs a different lifecycle.

using System.Threading.Tasks;
using Codebelt.Extensions.Xunit.Hosting.AspNetCore;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.TestHost;
using Microsoft.Extensions.DependencyInjection;
using Xunit;

namespace CatalogApi.Tests;

public sealed class WebApplicationTestFactoryExample
{
    [Fact]
    public async Task Create_UsesTheApplicationPipelineAndTestOverrides()
    {
        using var application = WebApplicationTestFactory.Create<CatalogProgram>(builder =>
        {
            builder.ConfigureServices(services =>
                services.AddSingleton(new CatalogStatus("test-ready")));
        });
        using var client = application.Host.GetTestClient();

        var body = await client.GetStringAsync("/health").ConfigureAwait(false);

        Assert.Equal("test-ready", body);
    }
}

public sealed record CatalogStatus(string Value);

public sealed class CatalogProgram
{
    public static void Main(string[] args)
    {
        var builder = WebApplication.CreateBuilder(args);
        builder.Services.AddSingleton(new CatalogStatus("ready"));

        var app = builder.Build();
        app.MapGet("/health", (CatalogStatus status) => status.Value);
        app.Run();
    }
}

Methods

Name Description
Create<TEntryPoint>(Action<IWebHostBuilder>, IWebApplicationFixture<TEntryPoint>)

Creates and returns an IHostTest implementation.

RunAsync<TEntryPoint>(Action<IWebHostBuilder>, Func<HttpClient, Task<HttpResponseMessage>>, IWebApplicationFixture<TEntryPoint>)

Runs an ASP.NET Core application and returns an HttpResponseMessage for the test server.

See Also