Swap.Testing
0.4.1
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
<PackageReference Include="Swap.Testing" Version="0.4.1" />
<PackageVersion Include="Swap.Testing" Version="0.4.1" />
<PackageReference Include="Swap.Testing" />
paket add Swap.Testing --version 0.4.1
#r "nuget: Swap.Testing, 0.4.1"
#:package Swap.Testing@0.4.1
#addin nuget:?package=Swap.Testing&version=0.4.1
#tool nuget:?package=Swap.Testing&version=0.4.1
Swap.Testing
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>– wrapsWebApplicationFactory<TProgram>and gives you anHtmxTestClient<TProgram>.HtmxTestClient<TProgram>– fluent client with HTMX-aware helpers (HtmxGetAsync,HtmxPostAsync,SubmitFormAsync, …).HtmxTestResponse– wrapsHttpResponseMessageand exposes HTML and HTMX assertions (elements, attributes, HX headers, snapshots, validation, etc.).SnapshotManager– optional snapshot testing helper for HTML responses.
Basic usage
- Create a test project and reference your ASP.NET Core app project.
- Add Swap.Testing to the test project.
- 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, readshx-*oraction/method, collects inputs, and submits as an HTMX request.FollowHxRedirectAsync– follows anHX-Redirectheader 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 | Versions 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. |
-
net9.0
- AngleSharp (>= 1.1.2)
- Microsoft.AspNetCore.Mvc.Testing (>= 9.0.10)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.