xUnit.Asserts.Compare
4.0.0
dotnet add package xUnit.Asserts.Compare --version 4.0.0
NuGet\Install-Package xUnit.Asserts.Compare -Version 4.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="xUnit.Asserts.Compare" Version="4.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="xUnit.Asserts.Compare" Version="4.0.0" />
<PackageReference Include="xUnit.Asserts.Compare" />
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 xUnit.Asserts.Compare --version 4.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: xUnit.Asserts.Compare, 4.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.
#:package xUnit.Asserts.Compare@4.0.0
#: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=xUnit.Asserts.Compare&version=4.0.0
#tool nuget:?package=xUnit.Asserts.Compare&version=4.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
xUnit.Asserts.Compare
Deep object comparison assertions for xUnit.net v3, powered by CompareNETObjects.
Compare whole object graphs in one line, without overriding Equals. When the objects differ, the failure message lists every member that differs:
DeepAssert.Equal(expected, actual);
Assert.Equal() Failure: Deep comparison found 2 differences
- Types [String,String], Item Expected.Name != Actual.Name, Values (Jacob,James)
- Types [String,String], Item Expected.ShippingAddress.City != Actual.ShippingAddress.City, Values (Hanoi,Paris)
Expected: Customer { CreatedAt = 0001-01-01T00:00:00.0000000, Email = "jacob@example.com", Id = 00000000-0000-0000-0000-000000000000, Lines = [], Name = "Jacob", ··· }
Actual: Customer { CreatedAt = 0001-01-01T00:00:00.0000000, Email = "jacob@example.com", Id = 00000000-0000-0000-0000-000000000000, Lines = [], Name = "James", ··· }
Install
dotnet add package xUnit.Asserts.Compare
| Package version | xUnit.net | .NET |
|---|---|---|
| 4.x | v3 (xunit.v3 4.0+) |
.NET 10 |
| 2.9.x | v2 (xunit 2.9) |
.NET 8 / .NET 9 |
Still on xUnit.net v2? Keep using xUnit.Asserts.Compare 2.9.2. See the migration guide when you upgrade.
Quick start
using Xunit.Asserts.Compare;
// Deep equality
DeepAssert.Equal(expected, actual);
DeepAssert.NotEqual(expected, actual);
// Ignore members: by expression (refactoring-safe) or by name / wildcard
DeepAssert.Equal(expected, actual, o => o.Ignore(c => c.Id, c => c.CreatedAt));
DeepAssert.Equal(expected, actual, "Id", "CreatedAt");
DeepAssert.Equal(expected, actual, o => o.Ignore("*At"));
// Collections in any order, or paired by key
DeepAssert.Equal(expectedLines, actualLines, o => o.IgnoreCollectionOrder());
DeepAssert.Equal(expectedLines, actualLines, o => o.MatchCollectionItemsBy<OrderLine>(l => l.Sku));
// Tolerances
DeepAssert.Equal(expected, actual, o => o
.WithDoubleTolerance(1e-9)
.WithDecimalTolerance(0.01m)
.WithDateTimeTolerance(TimeSpan.FromSeconds(1)));
// Different types: check a DTO or API response against an anonymous object
DeepAssert.Equivalent(new { Name = "Jacob", City = "Hanoi" }, dto);
// Deep search in a collection
DeepAssert.Contains(new OrderLine { Sku = "PEN", Quantity = 10 }, lines);
DeepAssert.DoesNotContain(cancelledLine, lines);
Features
| Need | API |
|---|---|
| Compare two object graphs | DeepAssert.Equal, DeepAssert.NotEqual |
| Compare objects of different types | DeepAssert.Equivalent |
| Find an item by deep equality | DeepAssert.Contains, DeepAssert.DoesNotContain |
| Skip members | Ignore(x => x.Member), Ignore("Name", "Type.Member", "*Suffix"), IgnoreType<T>() |
| Compare only some members | CompareOnly(x => x.Member), CompareOnly("Name") |
| Unordered collections | IgnoreCollectionOrder(), MatchCollectionItemsBy<TItem>(x => x.Key) |
| Numeric and time tolerances | WithDoubleTolerance, WithDecimalTolerance, WithDateTimeTolerance |
| Relaxed strings | IgnoreCase(), IgnoreLeadingTrailingWhitespace(), TreatNullAndEmptyStringAsEqual() |
| Custom equality for a type | WithComparer<TValue>((a, b) => ...) |
| Private members | IncludePrivateMembers() |
| Longer or shorter failure reports | MaxDifferences(n) (default 100) |
| Anything else CompareNETObjects supports | Configure(config => ...) |
Documentation
- Getting started: installation, first assertions, reading failure messages
- Use cases: recipes for common testing situations
- API reference: every assertion and option
- Migrating from 2.x to 4.x
- Changelog
Building from source
Requires the .NET 10 SDK.
dotnet build
dotnet test
Repository layout:
src/Xunit.Asserts.Compare/ the library (the NuGet package)
tests/Xunit.Asserts.Compare.Tests/ unit tests; UseCases/ holds a runnable version of every recipe in docs/use-cases.md
docs/ documentation
Directory.Build.props settings shared by all projects
Directory.Packages.props NuGet package versions (central package management)
global.json .NET SDK version and test runner
License
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net10.0 is compatible. 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.
-
net10.0
- CompareNETObjects (>= 4.84.0)
- xunit.v3.assert (>= 4.0.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories (1)
Showing the top 1 popular GitHub repositories that depend on xUnit.Asserts.Compare:
| Repository | Stars |
|---|---|
|
asc-lab/dotnet-cqrs-intro
Examples of implementation CQRS with Event Sourcing - evolutionary approach
|