Search Results for

    Show / Hide Table of Contents

    Not Constraint

    NotConstraint negates another constraint. It succeeds when the inner constraint fails, and fails when the inner constraint succeeds.

    Usage

    Is.Not.<constraint>
    Does.Not.<constraint>
    Has.No.<constraint>
    

    Examples

    [Test]
    public void NotConstraint_Examples()
    {
        // Basic negation
        Assert.That(42, Is.Not.EqualTo(0));
        Assert.That("Hello", Is.Not.Null);
        Assert.That("Hello", Is.Not.Empty);
    
        // String negation
        Assert.That("Hello World", Does.Not.Contain("Goodbye"));
        Assert.That("test.txt", Does.Not.EndWith(".pdf"));
    
        // Collection negation
        Assert.That(new[] { 1, 2, 3 }, Has.No.Member(0));
        Assert.That(new[] { 1, 2, 3 }, Is.Not.Empty);
        Assert.That(new[] { 1, 2, 3 }, Has.None.Negative);
    
        // Type negation
        Assert.That("Hello", Is.Not.InstanceOf<int>());
    
        // Combined with And/Or
        object obj = 42;
        Assert.That(obj, Is.Not.Null.And.InstanceOf<int>());
        Assert.That("test", Is.Not.Null.And.Not.Empty);
    }
    

    Notes

    1. Is.Not, Does.Not, and Has.No are all ways to negate constraints.
    2. Has.No is specifically for collection constraints (equivalent to Has.None).
    3. Negation applies to the immediately following constraint.

    See Also

    • And Constraint - Combine constraints
    • Or Constraint - Alternative constraints
    • Edit this page
    In this article
    Back to top Generated by DocFX | Copyright (c) 2018- The NUnit Project - Licensed under CC BY-NC-SA 4.0