Deprecated: Methods with the same name as their class will not be constructors in a future version of PHP; MenuItem has a deprecated constructor in /home/charl422/nunitsoftware.com/nunitv2/php/build_menu.php on line 34
NUnit - StringConstraints
NUnit 2.6.5 Legacy Documentation. View NUnit 3 Documentation

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

  1. ContainsSubstring and Contains may appear only in the body of a constraint expression.
  2. 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

  1. 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

  1. 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

  1. Matches may appear only in the body of a constraint expression.