Nickogl.AspNetCore.IntegrationTesting 1.0.0

dotnet add package Nickogl.AspNetCore.IntegrationTesting --version 1.0.0                
NuGet\Install-Package Nickogl.AspNetCore.IntegrationTesting -Version 1.0.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Nickogl.AspNetCore.IntegrationTesting" Version="1.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Nickogl.AspNetCore.IntegrationTesting --version 1.0.0                
#r "nuget: Nickogl.AspNetCore.IntegrationTesting, 1.0.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Nickogl.AspNetCore.IntegrationTesting as a Cake Addin
#addin nuget:?package=Nickogl.AspNetCore.IntegrationTesting&version=1.0.0

// Install Nickogl.AspNetCore.IntegrationTesting as a Cake Tool
#tool nuget:?package=Nickogl.AspNetCore.IntegrationTesting&version=1.0.0                

ASP.NET Core Integration Testing

This library hosts ASP.NET Core web applications in the test process, making tests and the application under test easy to debug and allowing each of the tests to configure the web application differently.

Unlike WebApplicationFactory from the Microsoft.AspNetCore.Mvc.Testing package, this library hosts the web applications with Kestrel, allowing one to make real HTTP and websocket requests against the application. It is thus ideal for orchestrating integration tests of various components.

Features

  • Stop and restart application, which can be useful for testing persistence features, for example
  • Hook into each stage of the entry point
    • Before WebApplicationBuilder.Build():
      • Configure options
      • Configure services
      • Everything else supported by IWebHostBuilder
    • After WebApplicationBuilder.Build() and before WebApplication.Run():
      • Add middleware
      • Add endpoints
      • Everything else supported by IApplicationBuilder

Sample

Full code can be found here.

Configure options

using var app = new WebApplicationTestHost<Program>();
app.ConfigureOptions<SampleOptions>(options => options.Text = "bar");
using var httpClient = new HttpClient() { BaseAddress = app.BaseAddress };

var response = await httpClient.GetAsync("/text");

Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
Assert.AreEqual("bar", await response.Content.ReadAsStringAsync());

Stop and restart application

using var textFile = new TemporaryFile();
using var app = new WebApplicationTestHost<Program>();
app.ConfigureOptions<SampleOptions>(options => options.PersistedTextFilePath = textFile.Path);
using var httpClient = new HttpClient() { BaseAddress = app.BaseAddress };

await httpClient.PostAsync("/text", new StringContent("bar"));
await app.StopAsync();
await app.StartAsync();
var response = await httpClient.GetAsync("/text");

Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
Assert.AreEqual("bar", await response.Content.ReadAsStringAsync());

Configure services

using var app = new WebApplicationTestHost<Program>();
app.ConfigureServices(services =>
{
    services.RemoveAll<ISampleService>();
    services.AddSingleton<ISampleService, SampleServiceMock>();
});
using var httpClient = new HttpClient() { BaseAddress = app.BaseAddress };

var response = await httpClient.GetAsync("/sample");

Assert.AreEqual("mocked", await response.Content.ReadAsStringAsync());

Add middleware

int observedRequests = 0;
using var app = new WebApplicationTestHost<Program>();
app.ConfigureApplication(app =>
{
    app.Use((next) =>
    {
        return async httpContext =>
        {
            await next(httpContext);
            Interlocked.Increment(ref observedRequests);
        };
    });
});
using var httpClient = new HttpClient() { BaseAddress = app.BaseAddress };

await httpClient.GetAsync("/text");

Assert.AreEqual(1, observedRequests);
Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 is compatible. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.0 62 11/17/2024