Lesson 12 of 93 · 6 min read · Updated
Core xUnit Assertions: Equal, Same, Null, Single, All, Collection and Equivalent
On this page
xUnit ships with a set of assertions for values, booleans, nulls, references and collections. Most of them could be replaced with Assert.True(...) and a boolean expression, and that is exactly the mistake to avoid. This lesson walks through the core assertions and the one rule that ties them together: pick the narrowest assertion that explains the failure best.
Technical Requirements
- .NET 11 SDK
- xunit 2.9.3 (with xunit.runner.visualstudio and Microsoft.NET.Test.Sdk)
The Rule: Choose the Narrowest Assertion
Consider a check that a list has one element. Assert.True(list.Count == 1) works, but when it fails, the message is only "Expected: True, Actual: False". You have to rerun the test in a debugger to learn what the list held. Assert.Single(list) checks the same thing, and when it fails, xUnit reports how many elements the collection had and prints them. A specific assertion turns a red test into a diagnosis.
The examples use Product from earlier lessons and a small Catalog class:
M02.XUnitBasics/Catalog.cs
Values, Booleans and Nulls
The first test raises a price by a percentage. The method rounds to two decimal places, so the result is predictable:
M02.XUnitBasics/Product.cs
Keep in mind that every xUnit assertion throws an exception when it fails. The test stops at the first failed assertion, and later assertions in the same test do not run. That is one more reason to order assertions from the most basic fact to the most detailed one.
M02.XUnitBasics.Tests/CoreAssertionTests.cs
Assert.Equal is the workhorse. It uses the type's equality, so for numbers and strings it compares values. The precision overload rounds both values to the given number of decimal places before comparing, which is how you compare computed decimal or double results without failing on tiny rounding differences.
Assert.True and Assert.False are for real boolean contracts. When you do use them on an expression, pass a message as the second argument (here "the product should have been in stock"). It is the only context the failure will have. If you find yourself writing Assert.True(x == y), use Assert.Equal(y, x) instead.
Find returns Product?, so a missing SKU is a normal result, not an error. Assert.Null and Assert.NotNull state that contract directly. Assert.NotNull also helps the compiler: after it, nullable analysis treats the value as not null.
Same vs. Equal: References and Values
M02.XUnitBasics.Tests/CoreAssertionTests.cs
Assert.Same checks reference identity: both variables point to the same object in memory. Assert.Equal checks equality as the type defines it. Use Same when identity is part of the contract, as it is here: the catalog must hand back the instance you stored, not a copy. Use Equal when only the value matters. Because Product is a class without custom equality, two products with identical data are not equal, and they are certainly not the same.
Collections
M02.XUnitBasics.Tests/CoreAssertionTests.cs
Contains has two forms: one takes an item, the other a predicate. Empty, NotEmpty and Single describe the size of a collection. Single is especially handy because it returns the element, so the next assertion can inspect it.
Assert.All and Assert.Collection look similar but answer different questions. All applies one rule to every element and reports every element that fails. It does not care about count or order. Collection takes one inspector per element, in order, and fails if the count differs. Choose Collection when order is part of the contract, as it is for InStock(), which promises results sorted by name. Choose All when every element must satisfy the same rule regardless of position.
Structural Comparison with Equivalent
M02.XUnitBasics.Tests/CoreAssertionTests.cs
Assert.Equivalent compares object graphs member by member, including nested arrays. Assert.Equal on the two anonymous objects would fail: the compiler-generated Equals of an anonymous type compares each property with default equality, and two different arrays are only equal by reference. Equivalent walks into the arrays and compares their content. It is useful for DTOs and results without custom equality. Use it with care: a broad structural comparison checks many members at once and can hide which one the test is really about.
Summary
- Pick the narrowest assertion that explains the failure.
Assert.Single(list)beatsAssert.True(list.Count == 1). Assert.Equalcompares values. Theprecisionoverload handles computed decimals and doubles.Assert.Samechecks reference identity,Assert.Equalchecks equality. UseSameonly when identity is part of the contract.- Use
Assert.Collectionwhen count and order matter, andAssert.Allwhen one rule applies to every element. Assert.Equivalentcompares object graphs structurally, without needing custom equality.