Legacy Documentation. View NUnit 3 Documentation

Comparison Constraints (NUnit 2.4 / 2.5)

Comparison constraints are able to test whether one value is greater or less than another. Comparison constraints work on numeric values, as well as other objects that implement the IComparable interface or - beginning with NUnit 2.5 - IComparable<T>.

Beginning with NUnit 2.5, you may supply your own comparison algorithm through the Using modifier.

GreaterThanConstraint

Action

Tests that one value is greater than another.

Constructor

GreaterThanConstraint(object expected)

Syntax

Is.GreaterThan(object expected)
Is.Positive // Equivalent to Is.GreaterThan(0)

Modifiers

...Using(IComparer comparer)
...Using(IComparer<T> comparer)
...Using(Comparison<T> comparer)

Examples of Use

Assert.That(7, Is.GreaterThan(3));
Assert.That(myOwnObject,
    Is.GreaterThan(theExpected).Using(myComparer));
Assert.That(42, Is.Positive);

GreaterThanOrEqualConstraint

Action

Tests that one value is greater than or equal to another.

Constructor

GreaterThanOrEqualConstraint(object expected)

Syntax

Is.GreaterThanOrEqualTo(object expected)
Is.AtLeast(object expected)

Modifiers

...Using(IComparer comparer)
...Using(IComparer<T> comparer)
...Using(Comparison<T> comparer)

Examples of Use

Assert.That(7, Is.GreaterThanOrEqualTo(3));
Assert.That(7, Is.AtLeast(3));
Assert.That(7, Is.GreaterThanOrEqualTo(7));
Assert.That(7, Is.AtLeast(7));
Assert.That(myOwnObject,
    Is.GreaterThanOrEqualTo(theExpected).Using(myComparer));

LessThanConstraint

Action

Tests that one value is less than another.

Constructor

LessThanConstraint(object expected)

Syntax

Is.LessThan(object expected)
Is.Negative // Equivalent to Is.LessThan(0)

Modifiers

...Using(IComparer comparer)
...Using(IComparer<T> comparer)
...Using(Comparison<T> comparer)

Examples of Use

Assert.That(3, Is.LessThan(7));
Assert.That(myOwnObject,
    Is.LessThan(theExpected).Using(myComparer));
Assert.That(-5, Is.Negative);

LessThanOrEqualConstraint

Action

Tests that one value is less than or equal to another.

Constructor

LessThanOrEqualConstraint(object expected)

Syntax

Is.LessThanOrEqualTo(object expected)
Is.AtMost(object expected)

Modifiers

...Using(IComparer comparer)
...Using(IComparer<T> comparer)
...Using(Comparison<T> comparer)

Examples of Use

Assert.That(3, Is.LessThanOrEqualTo(7));
Assert.That(3, Is.AtMost(7));
Assert.That(3, Is.LessThanOrEqualTo(3));
Assert.That(3, Is.AtMost(3));
Assert.That(myOwnObject,
    Is.LessThanOrEqualTo(theExpected).Using(myComparer));

RangeConstraint

Action

Constructor

RangeConstraint(IComparable from, IComparable to)

Syntax

Is.InRange(IComparable from, IComparable to)

Modifiers

...Using(IComparer comparer)
...Using(IComparer<T> comparer)
...Using(Comparison<T> comparer)

Examples of Use

int[] iarray = new int[] { 1, 2, 3 }

Assert.That( 42, Is.InRange(1, 100) );
Assert.That( iarray, Is.All.InRange(1, 3) );
Assert.That(myOwnObject,
    Is.InRange(lowExpected, highExpected).Using(myComparer));