Nwwz.Mvc.Testing
1.0.0
dotnet add package Nwwz.Mvc.Testing --version 1.0.0
NuGet\Install-Package Nwwz.Mvc.Testing -Version 1.0.0
<PackageReference Include="Nwwz.Mvc.Testing" Version="1.0.0" />
<PackageVersion Include="Nwwz.Mvc.Testing" Version="1.0.0" />
<PackageReference Include="Nwwz.Mvc.Testing" />
paket add Nwwz.Mvc.Testing --version 1.0.0
#r "nuget: Nwwz.Mvc.Testing, 1.0.0"
#addin nuget:?package=Nwwz.Mvc.Testing&version=1.0.0
#tool nuget:?package=Nwwz.Mvc.Testing&version=1.0.0
Mvc.Testing
Alternative to Microsoft.AspNetCore.Mvc.Testing implementation to enable testing using actual endpoints.
This package provides an alternative WebApplicationFactory implementation. Instead using a TestServer it spins up Kestrel to expose listen to a port over the network. This allows a browser to access the application in a test scenario. Ideal to use in combination with Playwirght for instance.
While the same is possible using Microsoft.AspNetCore.Mvc.Testing. The method leaves you with a duplicate Host and will cause ConfigureWebHost
to be invoked twice. This project is a copy of Microsoft's WebApplicationFactory but it invokes Kerstel instead of the TestServer. The advantage is that you no longer need to worry about any side effect that might occur when ConfigureWebHost
is called twice.
Usage
- Add this package in you test project
dotnet add package Nwwz.Mvc.Testing
- Create a custom class that inherits from the
WebApplicationFactory
:
internal class CustomWebApplicationFactory : Nwwz.Mvc.Testing.WebApplicationFactory<Program>
{
protected override void ConfigureWebHost(IWebHostBuilder builder) =>
builder.ConfigureTestServices(services =>
{
// Add testservices here
});
}
- A simple (xUnit)test like below will start the app, start a browsers and visit the home page
public class Testing
{
[Fact]
public async Task Test()
{
var factory = new CustomWebApplicationFactory();
var playwright = await Microsoft.Playwright.Playwright.CreateAsync();
var browser = await playwright.Chromium.LaunchAsync(new BrowserTypeLaunchOptions
{
Headless = false
});
var page = await browser.NewPageAsync();
await page.GotoAsync(factory.ServerAddress);
}
}
- Starting the whole app and a browser for each test might be a bit much performance wise. Make sure that the app and browser are only started once and reused for each test. For example by leveraging xUnit's ICollectionFixture
Compatibility
The code is fully compatible with Microsoft.AspNetCore.Mvc.Testing. For example, CreateClient()
will still return a client that connects to the application under test.
The fact that is expose real endpoints comes with the need for a couple of extensions though.
Server address
The property ServerAddress
expose the endpoint as can be seen in the example above.
Toggle https
Toggle between http and https. The default is https.
To use http set UseHttps
to false.
internal class CustomWebApplicationFactory : Nwwz.Mvc.Testing.WebApplicationFactory<Program>
{
public CustomWebApplicationFactory()
{
// Optionally specify to use https or not, default is true
UseHttps = false;
}
protected override void ConfigureWebHost(IWebHostBuilder builder) =>
builder.ConfigureTestServices(services =>
{
// Add testservices here
});
}
Example
For a full example please see https://github.com/netwatwezoeken/full-integration-testing/tree/main
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | 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. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. |
-
net8.0
- Microsoft.AspNetCore.Mvc.Testing (>= 8.0.11)
-
net9.0
- Microsoft.AspNetCore.Mvc.Testing (>= 9.0.0)
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 | 185 | 12/28/2024 |