String Constraints (NUnit 2.4)
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 has been deprecated since NUnit 2.5.1 and was removed in NUnit 3.0.
SubstringConstraint
Action
Tests for a substring.
Constructor
SubstringConstraint(string expected)
Syntax
Does.Contain(string expected) Contains.Substring(string expected) ContainsSubstring(string expected) Contains(string expected) [Obsolete] Is.StringContaining(string expected) [Obsolete] Text.Contains(string expected) [Obsolete] Text.DoesNotContain(string expected)
Modifiers
...IgnoreCase
Examples of Use
string phrase = "Make your tests fail before passing!" Assert.That( phrase, Does.Contain( "tests fail" ) ); Assert.That( phrase, Contains.Substring( "tests fail" ) ); Assert.That( phrase, Does.Not.Contain( "tests pass" ) ); Assert.That( phrase, Does.Contain( "make" ).IgnoreCase );
Notes
- ContainsSubstring and Contains may appear only in the body of a constraint expression.
- Contains is not actually a string constraint but is converted to one when a string is being tested.
StartsWithConstraint
Action
Tests for an initial string.
Constructor
StartsWithConstraint(string expected)
Syntax
Does.StartWith(string expected) StartsWith(string expected) [Obsolete] Is.StringStarting(string expected) [Obsolete] Text.StartsWith(string expected) [Obsolete] Text.DoesNotStartWith(string expected)
Modifiers
...IgnoreCase
Examples of Use
string phrase = "Make your tests fail before passing!" Assert.That( phrase, Does.StartWith( "Make" ) ); Assert.That( phrase, Does.Not.StartWith( "Break" ) ); Assert.That( phrase, Has.Length.GreaterThan(10) .And.Not.StartsWith( "Break" ) );
Notes
- StartsWith may appear only in the body of a constraint expression.
EndsWithConstraint
Action
Tests for an ending string.
Constructor
EndsWithConstraint(string expected)
Syntax
Does.EndWith(string expected) EndsWith(string expected) [Obsolete] Is.StringEnding(string expected) [Obsolete] Text.EndsWith(string expected) [Obsolete] Text.DoesNotEndWith(string expected)
Modifiers
...IgnoreCase
Examples of Use
string phrase = "Make your tests fail before passing!" Assert.That( phrase, Does.EndWith( "!" ) ); Assert.That( phrase, Does.EndWith( "PASSING!" ).IgnoreCase );
Notes
- EndsWith may appear only in the body of a constraint expression.
RegexConstraint
Action
Tests that a pattern is matched.
Constructor
RegexConstraint(string pattern)
Syntax
Does.Match(string pattern) Matches(string pattern) [Obsolete] Is.StringMatching(string pattern) [Obsolete] Text.Matches(string pattern) [Obsolete] Text.DoesNotMatch(string pattern)
Modifiers
...IgnoreCase
Examples of Use
string phrase = "Make your tests fail before passing!" Assert.That( phrase, Does.Match( "Make.*tests.*pass" ) ); Assert.That( phrase, Does.Not.Match( "your.*passing.*tests" ) ); Assert.That( phrase, Has.Length.GreaterThan(10) .And.Not.Matches( "your.*passing.*tests" ) );
Notes
- Matches may appear only in the body of a constraint expression.