2 Comments

If you want to Assert that a result string in a NUnit test is not null or empty, use:

Assert.That(string.IsNullOrEmpty(result), Is.False, "Result string must not be null or empty");

2 Replies to “C# – Assert that a string is not null or empty with NUnit”

  1. Regarding Mark’s comment, that should be
    Assert.That(result, Is.Not.Null.And.Not.Empty);

    Assert.That(“”, Is.Not.Null.Or.Empty); passes because the empty string is not null, or is empty. In fact, because Is.Not.Null passes, the Or.Empty isn’t even hit.

    !string.IsNullOrEmpty is equivalent to !(s == null || s == “”), which is equivalent to s != null && s != “”
    Is.Not.Null.Or.Empty is equivalent to !(s == null) || s == “”, which is equivalent to s != null || s == “”

    This can also be used for collections.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Related Posts