NUnit.Comparisons 2.0.0

dotnet add package NUnit.Comparisons --version 2.0.0
                    
NuGet\Install-Package NUnit.Comparisons -Version 2.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="NUnit.Comparisons" Version="2.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="NUnit.Comparisons" Version="2.0.0" />
                    
Directory.Packages.props
<PackageReference Include="NUnit.Comparisons" />
                    
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 NUnit.Comparisons --version 2.0.0
                    
#r "nuget: NUnit.Comparisons, 2.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 NUnit.Comparisons@2.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=NUnit.Comparisons&version=2.0.0
                    
Install as a Cake Addin
#tool nuget:?package=NUnit.Comparisons&version=2.0.0
                    
Install as a Cake Tool

NUnit.Comparisons

NUnit constraint extensions that produce detailed nested diff output when complex objects don't match — collections, XML documents, and custom type pairs.

Where NUnit's built-in constraints give a pass/fail for complex objects, this library gives you the full path to the mismatch:

XDocument {} should have matched the expected value, but did not.
    property Root XElement {letter} should have matched the expected value, but did not.
      method Nodes Collections did not match
        XElement {salutation} should have matched the expected value, but did not.
          method Nodes Collections did not match
            XText {} should have matched the expected value, but did not.
              property Value   String lengths are both 12. Strings differ at index 1.
                Expected: "Expected Text"
                But was:  "Actual Text"

The library is also not limited to comparing objects of the same type, which is useful when comparing a legacy API against a replacement that must behave identically.

Requirements

  • .NET 10 or later
  • NUnit 4.x

Getting started

Reference the projects you need in your test project:

  • NUnit.Comparisons — core constraint framework
  • NUnit.Comparisons.Xml — XML extension (compares XDocument/XElement against XmlDocument/XmlElement)

In your test setup, register the assembly containing your constraints with the factory:

[OneTimeSetUp]
public void Setup()
{
    CompareConstraintFactory.AddAssembly(typeof(XElementConstraint).Assembly);
}

Then use Is.ComparableTo in assertions:

Assert.That(actualXDocument, Is.ComparableTo(expectedXmlDocument));

Adding a new comparable type pair

Create a class that extends CompareConstraint<TActual, TExpected>:

public class XAttributeConstraint : CompareConstraint<XAttribute, XmlAttribute>
{
    protected override void AddCustomConstraints()
    {
        Add(Has.Property("Name").Property("LocalName").EqualTo(Expected!.Name));
        Add(Has.Property("Name").Property("NamespaceName").EqualTo(Expected.NamespaceURI));
        Add(Has.Property("Value").EqualTo(Expected.Value));
    }

    public override string GetActualName(XAttribute actual) => actual.Name.ToString();
    public override string GetExpectedName(XmlAttribute expected) => expected.Name;
}

AddCustomConstraints declares what "equal" means for this type pair using the same constraint DSL you would use in Assert.That. GetActualName and GetExpectedName return a short display name for each object used in failure messages (return null for types with no meaningful identity).

Register the assembly containing your constraint in test setup (see above). The factory discovers all CompareConstraint subclasses automatically — no attribute decoration required.

See src/NUnit.Comparisons.Xml for a complete example with eight constraint types covering the full XML object model.

Architecture and extension points

See CLAUDE.md for the full architecture, the constraint hierarchy, the MEF discovery mechanism, the DelegatingConstraintResult pattern, and a recipe for adding new type pairs.

Build and test

dotnet build NUnit.Comparisons.slnx
dotnet test NUnit.Comparisons.slnx

License

Apache License 2.0 — see LICENSE.

Product 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.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on NUnit.Comparisons:

Package Downloads
NUnit.Comparisons.Xml

XML extension for NUnit.Comparisons. Compares XDocument/XElement/XAttribute (LINQ to XML) against XmlDocument/XmlElement/XmlAttribute (classic XML DOM) with detailed nested diff output showing exactly which node differed and how.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
2.0.0 186 6/19/2026
1.0.1 2,620 11/19/2012