String constraints perform tests that are specific to strings. Attempting to test a non-string value with a string constraint is an error and gives an exception.
The Text prefix is deprecated beginning with NUnit 2.5.1 and will be removed in NUnit 3.0.
Tests for a substring.
SubstringConstraint(string expected)
Is.StringContaining(string expected) ContainsSubstring(string expected) Contains(string expected) [Obsolete] Text.Contains(string expected) [Obsolete] Text.DoesNotContain(string expected)
...IgnoreCase
string phrase = "Make your tests fail before passing!" Assert.That( phrase, Is.StringContaining( "tests fail" ) ); Assert.That( phrase, Is.Not.StringContaining( "tests pass" ) ); Assert.That( phrase, Is.StringContaining( "make" ).IgnoreCase ); Expect (phrase, Contains.Substring( "make" ).IgnoreCase );
Tests for an initial string.
StartsWithConstraint(string expected)
Is.StringStarting(string expected) StartsWith(string expected) [Obsolete] Text.StartsWith(string expected) [Obsolete] Text.DoesNotStartWith(string expected)
...IgnoreCase
string phrase = "Make your tests fail before passing!" Assert.That( phrase, Is.StringStarting( "Make" ) ); Assert.That( phrase, Is.Not.StringStarting( "Break" ) ); Assert.That( phrase, Has.Length.GreaterThan(10) .And.Not.StartsWith( "Break" ) ); Expect( phrase, StartsWith( "Make" ) );
Tests for an ending string.
EndsWithConstraint(string expected)
Is.StringEnding(string expected) EndsWith(string expected) [Obsolete] Text.EndsWith(string expected) [Obsolete] Text.DoesNotEndWith(string expected)
...IgnoreCase
string phrase = "Make your tests fail before passing!" Assert.That( phrase, Is.StringEnding( "!" ) ); Assert.That( phrase, Is.StringEnding( "PASSING!" ).IgnoreCase ); Expect( phrase, EndsWith( "!" ) );
Tests that a pattern is matched.
RegexConstraint(string pattern)
Is.StringMatching(string pattern) Matches(string pattern) [Obsolete] Text.Matches(string pattern) [Obsolete] Text.DoesNotMatch(string pattern)
...IgnoreCase
string phrase = "Make your tests fail before passing!" Assert.That( phrase, Is.StringMatching( "Make.*tests.*pass" ) ); Assert.That( phrase, Is.Not.StringMatching( "your.*passing.*tests" ) ); Assert.That( phrase, Has.Length.GreaterThan(10) .And.Not.Matches( "your.*passing.*tests" ) ); Expect( phrase, Matches( "Make.*pass" ) );