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
                    
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="Nwwz.Mvc.Testing" Version="1.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Nwwz.Mvc.Testing" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="Nwwz.Mvc.Testing" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Nwwz.Mvc.Testing --version 1.0.0
                    
#r "nuget: Nwwz.Mvc.Testing, 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.
#addin nuget:?package=Nwwz.Mvc.Testing&version=1.0.0
                    
Install Nwwz.Mvc.Testing as a Cake Addin
#tool nuget:?package=Nwwz.Mvc.Testing&version=1.0.0
                    
Install Nwwz.Mvc.Testing as a Cake Tool

Mvc.Testing

Alternative to Microsoft.AspNetCore.Mvc.Testing implementation to enable testing using actual endpoints.

Source on Github

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

  1. Add this package in you test project

dotnet add package Nwwz.Mvc.Testing

  1. 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
            });
}
  1. 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);
    }
}
  1. 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 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. 
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 185 12/28/2024