Swap.Testing 0.4.1

There is a newer version of this package available.
See the version list below for details.
dotnet add package Swap.Testing --version 0.4.1
                    
NuGet\Install-Package Swap.Testing -Version 0.4.1
                    
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="Swap.Testing" Version="0.4.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Swap.Testing" Version="0.4.1" />
                    
Directory.Packages.props
<PackageReference Include="Swap.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 Swap.Testing --version 0.4.1
                    
#r "nuget: Swap.Testing, 0.4.1"
                    
#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.
#:package Swap.Testing@0.4.1
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Swap.Testing&version=0.4.1
                    
Install as a Cake Addin
#tool nuget:?package=Swap.Testing&version=0.4.1
                    
Install as a Cake Tool

Swap.Testing

NuGet

Swap.Testing provides small helpers for writing integration tests against HTMX-powered ASP.NET Core MVC applications.

It is designed to work alongside Swap.Htmx but does not require it.

Install

dotnet add package Swap.Testing

Key types

  • HtmxTestFixture<TProgram> – wraps WebApplicationFactory<TProgram> and gives you an HtmxTestClient<TProgram>.
  • HtmxTestClient<TProgram> – fluent client with HTMX-aware helpers (HtmxGetAsync, HtmxPostAsync, SubmitFormAsync, …).
  • HtmxTestResponse – wraps HttpResponseMessage and exposes HTML and HTMX assertions (elements, attributes, HX headers, snapshots, validation, etc.).
  • SnapshotManager – optional snapshot testing helper for HTML responses.

Basic usage

  1. Create a test project and reference your ASP.NET Core app project.
  2. Add Swap.Testing to the test project.
  3. Use HtmxTestFixture<TProgram> as an xUnit fixture:
public class TodosTests : IClassFixture<HtmxTestFixture<Program>>
{
    private readonly HtmxTestClient<Program> _client;

    public TodosTests(HtmxTestFixture<Program> fixture)
    {
        _client = fixture.Client;
    }

    [Fact]
    public async Task GetTodos_ReturnsPartialWithItems()
    {
        var response = await _client.HtmxGetAsync("/todos");

        await response
            .AssertSuccess()
            .AssertPartialViewAsync()
            .AssertElementCountAsync(".todo-item", expectedCount: 5);
    }
}

Form and redirect helpers

  • SubmitFormAsync – finds a <form> in a previous HTML response, reads hx-* or action/method, collects inputs, and submits as an HTMX request.
  • FollowHxRedirectAsync – follows an HX-Redirect header and returns the resulting HTMX response.

Snapshot testing

Use AssertMatchesSnapshotAsync to compare HTML output against a stored snapshot.

await response
    .AssertSuccess()
    .AssertMatchesSnapshotAsync("todo-list-partial");

The first run creates __snapshots__/todo-list-partial.html. Future runs compare normalized HTML. Set UPDATE_SNAPSHOTS=true to update snapshots.

More examples

See EXAMPLE_TESTS.cs in this folder for a longer example suite using most of the helpers.

Product Compatible and additional computed target framework versions.
.NET 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.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.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
0.5.0 33 12/10/2025
0.4.2 389 11/20/2025
0.4.1 379 11/20/2025
0.4.0 267 11/16/2025
0.3.0 193 11/3/2025
0.2.0-dev 118 11/1/2025
0.1.0 185 10/30/2025