Legacy Documentation. View NUnit 3 Documentation

Equal Constraint (NUnit 2.4)

An EqualConstraint is used to test whether an actual value is equal to the expected value supplied in its constructor.

Syntax HelperConstructorOperation
Is.EqualTo( object )EqualConstraint( null )tests that two objects are equal

Notes

  1. Numerics of different types compare successfully if their values are equal.

  2. Values of type float and double are normally compared using an additional argument that indicates a tolerance within which they will be considered as equal. The Within modifier is used for this purpose. Beginning with NUnit 2.4.2, a tolerance may be specified for other numeric types as well.

  3. Beginning with NUnit 2.4.4, float and double comparisons for which no tolerance is specified use a default, use the value of GlobalSettings.DefaultFloatingPointTolerance. If this is not set, a tolerance of 0.0d is used.

  4. Beginning with NUnit 2.4, equality comparisons of DateTime values may use a TimeSpan as a tolerannce.

  5. Unlike normal floating point arithmetic, floating NaN comparisons succeed if both values are NaN. See the note on Equality Asserts.

  6. You may use this constraint to compare arrays with the same number of dimensions, two collections or an single-dimensioned array and collection. If you want to treat the arrays being compared as simple collections, use the AsCollection modifier, which causes the comparison to be made element by element, without regard for the rank or dimensions of the array. Jagged arrays (arrays of arrays) are compared recursively and must match in shape exactly for the comparison to succeed.

  7. In order to assert that two strings are equal ignoring case, use the IgnoreCase modifier. It may also be used when comparing arrays or collections of strings.

  8. When an equality test between two strings fails, the relevant portion of of both strings is displayed in the error message, clipping the strings to fit the length of the line as needed. Beginning with 2.4.4, this behavior may be modified by use of the NoClip modifier on the constraint. In addition, the maximum line length may be modified for all tests by setting the value of TextMessageWriter.MaximumLineLength in the appropriate level of setup.

Examples of Use

Assert.That(2 + 2, Is.EqualTo(4));
Assert.That(2 + 2 == 4);
Assert.That(2 + 2, Is.Not.EqualTo(5));
Assert.That(2 + 2 != 5);
Assert.That( 5.0, Is.EqualTo( 5 );
Assert.That( 2.1 + 1.2, Is.EqualTo( 3.3 ).Within( .0005 );
Assert.That( double.PositiveInfinity, Is.EqualTo( double.PositiveInfinity ) );
Assert.That( double.NegativeInfinity, Is.EqualTo( double.NegativeInfinity ) );
Assert.That( double.NaN, Is.EqualTo( double.NaN ) );

int[] i3 = new int[] { 1, 2, 3 };
double[] d3 = new double[] { 1.0, 2.0, 3.0 };
int[] iunequal = new int[] { 1, 3, 2 };
Assert.That(i3, Is.EqualTo(d3));
Assert.That(i3, Is.Not.EqualTo(iunequal));

int array2x2 = new int[,] { { 1, 2 } { 3, 4 } };
int array4 = new int[] { 1, 2, 3, 4 };
Assert.That( array2x2, Is.EqualTo( array4 ) ); // Fails
Assert.That( array2x2, Is.EqualTo( array4 ).AsCollection ); // Succeeds

Assert.That( "Hello!", Is.EqualTo( "HELLO!" ).IgnoreCase );

string[] expected = new string[] { "Hello", World" };
string[] actual = new string[] { "HELLO", "world" };
Assert.That( actual, Is.EqualTo( expected ).IgnoreCase;

// Using inheritance
Expect( i3, EqualTo( d3 ) );
Expect( i3, Not.EqualTo( iunequal ) );