ASP.NET Core web server for writing and running tests. This package was built from the source code at https://github.com/dotnet/dotnet/tree/87bc0b04e21d786669142109a5128c95618b75ed
$ dotnet add package Microsoft.AspNetCore.TestHostMicrosoft.AspNetCore.TestHost provides an ASP.NET Core web server for testing middleware in isolation.
To use Microsoft.AspNetCore.TestHost, follow these steps:
dotnet add package Microsoft.AspNetCore.TestHost
To set up the TestServer, configure it in your test project. Here's an example:
[Fact]
public async Task MiddlewareTest_ReturnsNotFoundForRequest()
{
// Build and start a host that uses TestServer
using var host = await new HostBuilder()
.ConfigureWebHost(builder =>
{
builder.UseTestServer()
.ConfigureServices(services =>
{
// Add any required services that the middleware uses
services.AddMyServices();
})
.Configure(app =>
{
// Configure the processing pipeline to use the middleware
// for the test
app.UseMiddleware<MyMiddleware>();
});
})
.StartAsync();
var response = await host.GetTestClient().GetAsync("/");
Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
}
The main types provided by this package are:
TestServer: An IServer implementation for executing testsTestServerOptions: Provides options for configuring a TestServerFor additional documentation and examples, refer to the official documentation for testing middleware in ASP.NET Core.
Microsoft.AspNetCore.TestHost is released as open-source under the MIT license. Bug reports and contributions are welcome at .