Search Results for

    Show / Hide Table of Contents

    EmptyString Constraint

    EmptyStringConstraint tests that a string has zero length. This is a specialized constraint created when Is.Empty is applied to a string value.

    Usage

    Is.Empty
    Is.Not.Empty
    

    Examples

    [Test]
    public void EmptyStringConstraint_Examples()
    {
        Assert.That("", Is.Empty);
        Assert.That(string.Empty, Is.Empty);
        Assert.That("Hello", Is.Not.Empty);
    
        // Common pattern: check for null or empty
        string? result = "";
        Assert.That(result, Is.Null.Or.Empty);
    
        string name = "Alice";
        Assert.That(name, Is.Not.Null.And.Not.Empty);
    }
    

    Notes

    1. Is.Empty is a polymorphic constraint that tests for empty strings, collections, or directories depending on the actual value type.
    2. For checking null-or-empty strings, you can combine constraints: Is.Null.Or.Empty.
    3. To test for whitespace-only strings, use WhiteSpaceConstraint.

    See Also

    • Empty Constraint - Polymorphic constraint for strings, collections, directories
    • WhiteSpace Constraint
    • Null Constraint
    • 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