Table of Contents

Class WebApplicationTestFactory

Namespace
Codebelt.Extensions.Xunit.Hosting.AspNetCore
Assembly
Codebelt.Extensions.Xunit.Hosting.AspNetCore.dll

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 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.

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

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

Creates and returns an IHostTest implementation.

public static IHostTest Create<TEntryPoint>(Action<IWebHostBuilder> webHostSetup = null, IWebApplicationFixture<TEntryPoint> hostFixture = null) where TEntryPoint : class

Parameters

webHostSetup Action<IWebHostBuilder>

The IWebHostBuilder which may be configured.

hostFixture IWebApplicationFixture<TEntryPoint>

An optional IWebApplicationFixture<TEntryPoint> implementation to use instead of the default BlockingManagedWebApplicationFixture<TEntryPoint> instance.

Returns

IHostTest

An instance of an IHostTest implementation.

Type Parameters

TEntryPoint

A type in the entry point assembly of the application.

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

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

public static Task<HttpResponseMessage> RunAsync<TEntryPoint>(Action<IWebHostBuilder> webHostSetup = null, Func<HttpClient, Task<HttpResponseMessage>> responseFactory = null, IWebApplicationFixture<TEntryPoint> hostFixture = null) where TEntryPoint : class

Parameters

webHostSetup Action<IWebHostBuilder>

The IWebHostBuilder which may be configured.

responseFactory Func<HttpClient, Task<HttpResponseMessage>>

The function delegate that creates a HttpResponseMessage from the HttpClient. Default is a GET request to the root URL ("/").

hostFixture IWebApplicationFixture<TEntryPoint>

An optional IWebApplicationFixture<TEntryPoint> implementation to use instead of the default BlockingManagedWebApplicationFixture<TEntryPoint> instance.

Returns

Task<HttpResponseMessage>

A Task that represents the asynchronous operation. The task result contains the HttpResponseMessage for the test server.

Type Parameters

TEntryPoint

A type in the entry point assembly of the application.

See Also