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 is deprecated beginning with NUnit 2.5.1 and will be removed in NUnit 3.0.

SubstringConstraint

Action

Tests for a substring.

Constructor

SubstringConstraint(string expected)

Syntax

Is.StringContaining(string expected)
Contains.Substring(string expected)
ContainsSubstring(string expected)
Contains(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, Is.StringContaining( "tests fail" ) );
Assert.That( phrase, Contains.Substring( "tests fail" ) );
Assert.That( phrase, Is.Not.StringContaining( "tests pass" ) );
Assert.That( phrase, Is.StringContaining( "make" ).IgnoreCase );
Expect (phrase, Contains.Substring( "make" ).IgnoreCase );

Notes

  1. ContainsSubstring and Contains may appear only in the body of a constraint expression or when the inherited syntax is used.
  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

Is.StringStarting(string expected)
StartsWith(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, 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" ) );

Notes

  1. StartsWith may appear only in the body of a constraint expression or when the inherited syntax is used.

EndsWithConstraint

Action

Tests for an ending string.

Constructor

EndsWithConstraint(string expected)

Syntax

Is.StringEnding(string expected)
EndsWith(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, Is.StringEnding( "!" ) );
Assert.That( phrase, Is.StringEnding( "PASSING!" ).IgnoreCase );
Expect( phrase, EndsWith( "!" ) );

Notes

  1. EndsWith may appear only in the body of a constraint expression or when the inherited syntax is used.

RegexConstraint

Action

Tests that a pattern is matched.

Constructor

RegexConstraint(string pattern)

Syntax

Is.StringMatching(string pattern)
Matches(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, 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" ) );

Notes

  1. Matches may appear only in the body of a constraint expression or when the inherited syntax is used.