Overview
Codebelt.Extensions.Xunit.Hosting.AspNetCore extends Codebelt.Extensions.Xunit.Hosting with an ASP.NET Core request pipeline backed by Microsoft.AspNetCore.TestHost. It lets a test configure services and an IApplicationBuilder, host them on an in-memory TestServer, and exercise the result either through an HttpClient or by building and invoking the pipeline directly.
The package keeps the same two styles as the hosting package. Base classes (WebHostTest<T> and MinimalWebHostTest<T>) follow the class-fixture model and require a ConfigureApplication override, while factories (WebHostTestFactory and MinimalWebHostTestFactory) create and run a web host inline. It also supplies a fake IHttpContextAccessor so components that depend on the current HTTP context resolve in isolation.
Key APIs
WebHostTest<T> is the abstract base class for ASP.NET Core tests, where T is an IWebHostFixture. In addition to the ConfigureServices override inherited from HostTest<T>, it adds the abstract ConfigureApplication(IApplicationBuilder app) for building the request pipeline and exposes the configured pipeline through the Application property. MinimalWebHostTest<T> is the minimal-hosting equivalent built on MinimalHostTest and an IWebMinimalHostFixture.
WebHostTestFactory.Create returns a disposable IWebHostTest from optional service, pipeline, and host delegates, while WebHostTestFactory.RunAsync goes one step further: it creates the host, obtains an HttpClient from the TestServer, and returns the resulting HttpResponseMessage. RunWithHostBuilderContextAsync exposes the HostBuilderContext to the service and pipeline callbacks, and MinimalWebHostTestFactory mirrors all of these for the minimal hosting model.
ManagedWebHostFixture is the default IWebHostFixture. It configures a TestServer with PreserveExecutionContext, the Development environment, JSON and environment-variable configuration, console, debug, and event-source logging, then runs the host. BlockingManagedWebHostFixture starts the host synchronously so startup exceptions surface to the caller, and SelfManagedWebHostFixture replaces the runner with a no-op; ManagedWebMinimalHostFixture and its self-managed variant provide the same behavior over WebApplication.CreateBuilder.
HttpClientExtensions.ToHttpResponseMessageAsync extends HttpClient with a responseFactory delegate that defaults to a GET request against the root URL, which is the mechanism RunAsync uses to produce its HttpResponseMessage. ServiceCollectionExtensions.AddFakeHttpContextAccessor registers FakeHttpContextAccessor, a unit-test implementation of IHttpContextAccessor whose HttpContext is pre-populated with request and response features; the web host fixtures add it automatically.
HostBuilderApplicationExtensions.ToHostBuilder converts an IHostApplicationBuilder to an IHostBuilder when it is a WebApplicationBuilder, throwing ArgumentException otherwise, which bridges the minimal-hosting callback to host-builder configuration. IWebHostTest, IWebHostFixture, and IPipelineTest are the abstractions that expose the running host, the configurable pipeline callback, and the built Application.
Basic usage
using System.Net;
using System.Threading.Tasks;
using Codebelt.Extensions.Xunit;
using Codebelt.Extensions.Xunit.Hosting.AspNetCore;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Xunit;
namespace HealthEndpointTests;
public class HealthEndpointTest : Test
{
public HealthEndpointTest(ITestOutputHelper output) : base(output)
{
}
[Fact]
public async Task Get_ShouldReturnHealthyBody()
{
using var response = await WebHostTestFactory.RunAsync(
pipelineSetup: app => app.Run(context => context.Response.WriteAsync("Healthy")));
var body = await response.Content.ReadAsStringAsync();
TestOutput.WriteLine($"{(int)response.StatusCode}: {body}");
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.Equal("Healthy", body);
}
}
Use this pattern when you want to verify how a middleware or terminal handler responds to a real HTTP request without standing up a full web project. It matters because WebHostTestFactory.RunAsync hosts the pipeline on an in-memory TestServer and returns the actual HttpResponseMessage, so the test asserts on the same status code and body a client would receive.
Installation
dotnet add package Codebelt.Extensions.Xunit.Hosting.AspNetCore
Usage guidance
Choose this package when the system under test is an ASP.NET Core pipeline component such as middleware, a filter, or a controller, and you want a TestServer and HttpClient without a separate web host project. If your test only needs Microsoft Dependency Injection or a generic IHost with no HTTP pipeline, stay with Codebelt.Extensions.Xunit.Hosting; for plain unit tests with no host at all, use Codebelt.Extensions.Xunit.