Overview
Codebelt.Extensions.Xunit.App is a productivity meta-package. It ships no assembly of its own and instead references the three standalone libraries in the family so a test project can add a single package and reach the whole toolbelt: Codebelt.Extensions.Xunit, Codebelt.Extensions.Xunit.Hosting, and Codebelt.Extensions.Xunit.Hosting.AspNetCore.
Use it when a single test assembly mixes plain unit tests, dependency-injection tests, and ASP.NET Core tests and you would rather not track each standalone package separately.
Key APIs
This package exposes no public APIs of its own. It is a reference aggregate, so the types you use come from the referenced packages: Test, InMemoryTestStore<T>, and Test.Match from Codebelt.Extensions.Xunit; HostTest<T>, HostTestFactory, and AddXunitTestLogging from Codebelt.Extensions.Xunit.Hosting; and WebHostTest<T>, WebHostTestFactory, and AddFakeHttpContextAccessor from Codebelt.Extensions.Xunit.Hosting.AspNetCore.
Basic usage
Codebelt.Extensions.Xunit
using Codebelt.Extensions.Xunit;
using Xunit;
namespace ReceiptTests;
public class ReceiptFormatterTest : Test
{
public ReceiptFormatterTest(ITestOutputHelper output) : base(output)
{
}
[Fact]
public void Format_ShouldMatchTemplate_IgnoringOrderId()
{
var receipt = Format(orderId: 4096, total: 75);
TestOutput.WriteLine(receipt);
Assert.True(Test.Match("Order * billed 75 USD", receipt));
}
private static string Format(int orderId, int total) => $"Order {orderId} billed {total} USD";
}
Codebelt.Extensions.Xunit.Hosting
using Codebelt.Extensions.Xunit;
using Codebelt.Extensions.Xunit.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Xunit;
namespace PricingTests;
public class PricingServiceTest : HostTest<ManagedHostFixture>
{
private readonly PricingService _pricing;
public PricingServiceTest(ManagedHostFixture hostFixture, ITestOutputHelper output) : base(hostFixture, output)
{
_pricing = Host.Services.GetRequiredService<PricingService>();
}
[Fact]
public void Net_ShouldRemoveVat()
{
var net = _pricing.Net(125m, 0.25m);
TestOutput.WriteLine($"Net price: {net}");
Assert.Equal(100m, net);
}
public override void ConfigureServices(IServiceCollection services)
{
services.AddSingleton<PricingService>();
}
public sealed class PricingService
{
public decimal Net(decimal gross, decimal vatRate) => gross / (1 + vatRate);
}
}
Codebelt.Extensions.Xunit.Hosting.AspNetCore
using System.Linq;
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 ApiTests;
public class CorrelationMiddlewareTest : Test
{
public CorrelationMiddlewareTest(ITestOutputHelper output) : base(output)
{
}
[Fact]
public async Task Invoke_ShouldStampCorrelationHeader()
{
using var response = await WebHostTestFactory.RunAsync(
pipelineSetup: app =>
{
app.Use(async (context, next) =>
{
context.Response.Headers["X-Correlation-Id"] = "test-correlation";
await next(context);
});
app.Run(context => context.Response.WriteAsync("done"));
});
var correlation = response.Headers.GetValues("X-Correlation-Id").Single();
TestOutput.WriteLine(correlation);
Assert.Equal("test-correlation", correlation);
}
}
Each example demonstrates an API owned by one of the referenced packages, not by this package. Codebelt.Extensions.Xunit.App provides only the single package reference; the Test, HostTest<T>, and WebHostTestFactory APIs are implemented and documented by the standalone packages it pulls in.
Installation
dotnet add package Codebelt.Extensions.Xunit.App
Usage guidance
Add this package when one test project benefits from the full family and you want a single dependency to maintain. Choose a standalone package such as Codebelt.Extensions.Xunit instead when a project only needs plain unit testing, since pulling in the aggregate would also reference the hosting and ASP.NET Core libraries that project never uses.