Assert.AreEqual
Assert.AreEqual tests whether the two arguments are equal.
Note
From version 4.5.0, using C# 14, you don't need to use the ClassicAssert class, nor the NUnit.Framework.Legacy namespace, but can use the former Assert class. This applies for many of the asserts, but a few still require the use of the ClassicAssert class. These will be fixed in upcoming releases. In the list below, and in the examples, the 4.5.0 syntax will be used. If you use an earlier 4.x version, replace Assert with ClassicAssert and include the NUnit.Framework.Legacy namespace.
Assert.AreEqual(double expected, double actual, double tolerance);
Assert.AreEqual(double expected, double actual, double tolerance,
string message, params object[] params);
Assert.AreEqual(object expected, object actual);
Assert.AreEqual(object expected, object actual,
string message, params object[] params);
Comparing Numerics of Different Types
The method overloads that compare two objects make special provision so that numeric values of different types compare as expected. This assert succeeds:
[Test]
public void AreEqual_Examples()
{
// Comparing numerics of different types
Assert.AreEqual(5, 5.0);
// Floating point special values
Assert.AreEqual(double.PositiveInfinity, double.PositiveInfinity);
Assert.AreEqual(double.NegativeInfinity, double.NegativeInfinity);
Assert.AreEqual(double.NaN, double.NaN);
// Basic equality
Assert.AreEqual("expected", "expected");
Assert.AreEqual(42, 42);
// With tolerance for floating point
Assert.AreEqual(2.1 + 1.2, 3.3, 0.001);
}
Comparing Floating Point Values
Values of type float and double are compared using an additional argument that indicates a tolerance within which they will be considered as equal.
Special values are handled so that the following Asserts succeed:
[Test]
public void AreEqual_Examples()
{
// Comparing numerics of different types
Assert.AreEqual(5, 5.0);
// Floating point special values
Assert.AreEqual(double.PositiveInfinity, double.PositiveInfinity);
Assert.AreEqual(double.NegativeInfinity, double.NegativeInfinity);
Assert.AreEqual(double.NaN, double.NaN);
// Basic equality
Assert.AreEqual("expected", "expected");
Assert.AreEqual(42, 42);
// With tolerance for floating point
Assert.AreEqual(2.1 + 1.2, 3.3, 0.001);
}
Comparing Arrays and Collections
NUnit is able to compare single-dimensioned arrays, multi-dimensioned arrays, nested arrays (arrays of arrays) and collections. Two arrays or collections are considered equal if they have the same dimensions and if each pair of corresponding elements is equal.
NUnit 3.0 adds the ability to compare generic collections and dictionaries.