What are the different constraints used with NUnit assert?

Can anyone help me with different constraints used with NUnit assert with example?

Hi Brett,

Here are the different constraints:

  1. Equal Constraints

    a. EqualTo:

    Is.EqualTo( object expected )

    b. NotEqualTo:

    Is.Not.EqualTo( object expected )

  2. Comparison Constraints

    a. GreaterThan:

    Is.GreaterThan( object expected )

    b. GreaterThanOrEqualTo:

    Is.GreaterThanOrEqualTo( object expected )

    c. LessThan:

    Is.LessThan( object expected )

    d. LessThanOrEqualTo:

    Is.LessThanOrEqualTo( object expected )

    e. InRange:

    Is.InRange( object from, object to )

  3. String Constraints

    a. String Equal or Not Equal:

    Is.EqualTo( object expected )

    Is.Not.EqualTo( object expected )

    b. String Equal with IgnoreCase:

    IgnoreCase { get; }

    c. DoesContain:

    IgnoreCase { get; }

    d. Empty:

    Assert.That(arg1, Is.Empty);

    e. StartWith and EndWith:

    Assert.That(str1, Does.StartWith(“str2”));

    Assert.That(str1, Does.Not.StartWith(“str2”));

    Assert.That(str1, Does.EndWith(“str2”));

    Assert.That(str1, Does.Not.EndWith(“str2”));

    f. Regex (Does.Match):

    Assert.That(result, Does.Match(“regex expression”));

    Assert.That(result, Does.Not.Match(“regex expression”));

  4. Condition Constraints

    a. Empty

    Assert.That(arg1, Is.Empty);

    Assert.That(arg1, Is.Not.Empty);

    b. Null

    Is.Null(object expected);

    c. Boolean:

    Assert.That(arg1, Is.True);

  5. Collection Constraints

    int[] arr = new int[] {6, 7, 8};

    a. Greater than:

    Assert.That(arr, Is.All.GreaterThan(4));

    b. Less Than:

    Assert.That(arr, Is.All.LessThan(20));

    c. Not Null:

    Assert.That(arr, Is.All.Not.Null);

    d. Instance Of:

    Assert.That(arr, Is.All.InstanceOf());

    e. Exactly X Items

    Assert.That(arr, Has.Exactly(3).Items);

    f. Unique Items:

    Assert.That(arr, Is.Unique);

    g. Empty:

    Assert.That(arr, Is.Not.Empty);

    Assert.That(arr, Is.Empty);

    h. Contains

    Assert.That(array, Contains.Item(6));