Text Matchers

Matchers that perform text comparisons.

contains_string

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

Matches if object is a string containing a given string.

Parameters:string – The string to search for.

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

ends_with

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

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

Parameters:string – The string to search for.

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

equal_to_ignoring_case

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 – The string to compare against as the expected value.

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

equal_to_ignoring_whitespace

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 – The string to compare against as the expected value.

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

matches_regexp

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

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

Parameters:pattern – The regular expression to search for.

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.

starts_with

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.

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

string_contains_in_order

hamcrest.library.text.stringcontainsinorder.string_contains_in_order(string1[, string2[, ...]])

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

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

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