Text Matchers

Matchers that perform text comparisons.

class hamcrest.library.text.stringcontains.StringContains(substring)

Bases: SubstringMatcher

hamcrest.library.text.stringcontains.contains_string(substring)

Matches if object is a string containing a given string.

Parameters:
  • string – The string to search for.

  • substring (str) –

Return type:

Matcher[str]

This matcher first checks whether the evaluated object is a string. If so, it checks whether it contains string.

Example:

contains_string("def")

will match “abcdefg”.

class hamcrest.library.text.stringendswith.StringEndsWith(substring)

Bases: SubstringMatcher

hamcrest.library.text.stringendswith.ends_with(string)

Matches if object is a string ending with a given string.

Parameters:

string (str) – The string to search for.

Return type:

Matcher[str]

This matcher first checks whether the evaluated object is a string. If so, it checks if string matches the ending characters of the evaluated object.

Example:

ends_with("bar")

will match “foobar”.

class hamcrest.library.text.isequal_ignoring_case.IsEqualIgnoringCase(string)

Bases: BaseMatcher[str]

Parameters:

string (str) –

describe_to(description)

Generates a description of the object.

The description may be part of a description of a larger object of which this is just a component, so it should be worded appropriately.

Parameters:

description (Description) – The description to be built or appended to.

Return type:

None

hamcrest.library.text.isequal_ignoring_case.equal_to_ignoring_case(string)

Matches if object is a string equal to a given string, ignoring case differences.

Parameters:

string (str) – The string to compare against as the expected value.

Return type:

Matcher[str]

This matcher first checks whether the evaluated object is a string. If so, it compares it with string, ignoring differences of case.

Example:

equal_to_ignoring_case("hello world")

will match “heLLo WorlD”.

class hamcrest.library.text.isequal_ignoring_whitespace.IsEqualIgnoringWhiteSpace(string)

Bases: BaseMatcher[str]

describe_to(description)

Generates a description of the object.

The description may be part of a description of a larger object of which this is just a component, so it should be worded appropriately.

Parameters:

description (Description) – The description to be built or appended to.

Return type:

None

hamcrest.library.text.isequal_ignoring_whitespace.equal_to_ignoring_whitespace(string)

Matches if object is a string equal to a given string, ignoring differences in whitespace.

Parameters:

string (str) – The string to compare against as the expected value.

Return type:

Matcher[str]

This matcher first checks whether the evaluated object is a string. If so, it compares it with string, ignoring differences in runs of whitespace.

Example:

equal_to_ignoring_whitespace("hello world")

will match "hello   world".

class hamcrest.library.text.stringmatches.StringMatchesPattern(pattern)

Bases: BaseMatcher[str]

describe_to(description)

Generates a description of the object.

The description may be part of a description of a larger object of which this is just a component, so it should be worded appropriately.

Parameters:

description (Description) – The description to be built or appended to.

Return type:

None

hamcrest.library.text.stringmatches.matches_regexp(pattern)

Matches if object is a string containing a match for a given regular expression.

Parameters:

pattern (Union[str, Pattern[str]]) – The regular expression to search for.

Return type:

Matcher[str]

This matcher first checks whether the evaluated object is a string. If so, it checks if the regular expression pattern matches anywhere within the evaluated object.

class hamcrest.library.text.stringstartswith.StringStartsWith(substring)

Bases: SubstringMatcher

hamcrest.library.text.stringstartswith.starts_with(substring)

Matches if object is a string starting with a given string.

Parameters:
  • string – The string to search for.

  • substring (str) –

Return type:

Matcher[str]

This matcher first checks whether the evaluated object is a string. If so, it checks if string matches the beginning characters of the evaluated object.

Example:

starts_with("foo")

will match “foobar”.

class hamcrest.library.text.stringcontainsinorder.StringContainsInOrder(*substrings)

Bases: BaseMatcher[str]

describe_to(description)

Generates a description of the object.

The description may be part of a description of a larger object of which this is just a component, so it should be worded appropriately.

Parameters:

description (Description) – The description to be built or appended to.

Return type:

None

hamcrest.library.text.stringcontainsinorder.string_contains_in_order(*substrings)

Matches if object is a string containing a given list of substrings in relative order.

Parameters:
  • string1,... – A comma-separated list of strings.

  • substrings (str) –

Return type:

Matcher[str]

This matcher first checks whether the evaluated object is a string. If so, it checks whether it contains a given list of strings, in relative order to each other. The searches are performed starting from the beginning of the evaluated string.

Example:

string_contains_in_order("bc", "fg", "jkl")

will match “abcdefghijklm”.