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
Is.Emptyis a polymorphic constraint that tests for empty strings, collections, or directories depending on the actual value type.- For checking null-or-empty strings, you can combine constraints:
Is.Null.Or.Empty. - To test for whitespace-only strings, use WhiteSpaceConstraint.
See Also
- Empty Constraint - Polymorphic constraint for strings, collections, directories
- WhiteSpace Constraint
- Null Constraint