String assertions¶
Assertions available when the value under test is a str. Call them on the builder returned by
assert_that("...").
String assertions mixin.
is_equal_to_ignoring_case ¶
Asserts that val is a string and is case-insensitive equal to other.
Checks actual is equal to expected using the == operator and str.lower().
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
str
|
the expected value |
required |
Examples:
Usage:
assert_that('foo').is_equal_to_ignoring_case('FOO')
assert_that('FOO').is_equal_to_ignoring_case('foo')
assert_that('fOo').is_equal_to_ignoring_case('FoO')
Returns:
| Name | Type | Description |
|---|---|---|
AssertionBuilder |
Self
|
returns this instance to chain to the next assertion |
Raises:
| Type | Description |
|---|---|
AssertionError
|
if actual is not case-insensitive equal to expected |
Source code in assertpy2/string.py
contains_ignoring_case ¶
Asserts that val is string and contains the given item or items.
Walks val and checks for item or items using the == operator and str.lower().
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*items
|
str
|
the item or items expected to be contained |
()
|
Examples:
Usage:
assert_that('foo').contains_ignoring_case('F', 'oO')
assert_that(['a', 'B']).contains_ignoring_case('A', 'b')
assert_that({'a': 1, 'B': 2}).contains_ignoring_case('A', 'b')
assert_that({'a', 'B'}).contains_ignoring_case('A', 'b')
Returns:
| Name | Type | Description |
|---|---|---|
AssertionBuilder |
Self
|
returns this instance to chain to the next assertion |
Raises:
| Type | Description |
|---|---|
AssertionError
|
if val does not contain the case-insensitive item or items |
Source code in assertpy2/string.py
starts_with ¶
Asserts that val is string or iterable and starts with prefix.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prefix
|
str
|
the prefix |
required |
Examples:
Usage:
assert_that('foo').starts_with('fo')
assert_that(['a', 'b', 'c']).starts_with('a')
assert_that((1, 2, 3)).starts_with(1)
assert_that(((1, 2), (3, 4), (5, 6))).starts_with((1, 2))
Returns:
| Name | Type | Description |
|---|---|---|
AssertionBuilder |
Self
|
returns this instance to chain to the next assertion |
Raises:
| Type | Description |
|---|---|
AssertionError
|
if val does not start with prefix |
Source code in assertpy2/string.py
ends_with ¶
Asserts that val is string or iterable and ends with suffix.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
suffix
|
str
|
the suffix |
required |
Examples:
Usage:
assert_that('foo').ends_with('oo')
assert_that(['a', 'b', 'c']).ends_with('c')
assert_that((1, 2, 3)).ends_with(3)
assert_that(((1, 2), (3, 4), (5, 6))).ends_with((5, 6))
Returns:
| Name | Type | Description |
|---|---|---|
AssertionBuilder |
Self
|
returns this instance to chain to the next assertion |
Raises:
| Type | Description |
|---|---|
AssertionError
|
if val does not end with suffix |
Source code in assertpy2/string.py
matches ¶
Asserts that val is string and matches the given regex pattern.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pattern
|
str
|
the regular expression pattern, as raw string (aka prefixed with |
required |
Examples:
Usage:
assert_that('foo').matches(r'\w')
assert_that('123-456-7890').matches(r'\d{3}-\d{3}-\d{4}')
Match is partial unless anchored, so these assertion pass:
assert_that('foo').matches(r'\w')
assert_that('foo').matches(r'oo')
assert_that('foo').matches(r'\w{2}')
To match the entire string, just use an anchored regex pattern where ^ and $
match the start and end of line and \A and \Z match the start and end of string:
assert_that('foo').matches(r'^\w{3}$')
assert_that('foo').matches(r'\A\w{3}\Z')
And regex flags, such as re.MULTILINE and re.DOTALL, can only be applied via
inline modifiers, such as (?m) and (?s):
s = '''bar
foo
baz'''
# using multiline (?m)
assert_that(s).matches(r'(?m)^foo$')
# using dotall (?s)
assert_that(s).matches(r'(?s)b(.*)z')
Returns:
| Name | Type | Description |
|---|---|---|
AssertionBuilder |
Self
|
returns this instance to chain to the next assertion |
Raises:
| Type | Description |
|---|---|
AssertionError
|
if val does not match pattern |
Tip
Regular expressions are tricky. Be sure to use raw strings (aka prefixed with r).
Also, note that the matches() assertion passes
for partial matches (as does the
underlying re.match method). So, if you need to match the entire string, you must
include anchors in the regex pattern.
Source code in assertpy2/string.py
does_not_match ¶
Asserts that val is string and does not match the given regex pattern.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pattern
|
str
|
the regular expression pattern, as raw string (aka prefixed with |
required |
Examples:
Usage:
assert_that('foo').does_not_match(r'\d+')
assert_that('123').does_not_match(r'\w+')
Returns:
| Name | Type | Description |
|---|---|---|
AssertionBuilder |
Self
|
returns this instance to chain to the next assertion |
Raises:
| Type | Description |
|---|---|
AssertionError
|
if val does match pattern |
See Also
matches() - for more about regex patterns
Source code in assertpy2/string.py
is_alpha ¶
Asserts that val is non-empty string and all characters are alphabetic (using str.isalpha()).
Examples:
Usage:
assert_that('foo').is_alpha()
Returns:
| Name | Type | Description |
|---|---|---|
AssertionBuilder |
Self
|
returns this instance to chain to the next assertion |
Raises:
| Type | Description |
|---|---|
AssertionError
|
if val is not alphabetic |
Source code in assertpy2/string.py
is_digit ¶
Asserts that val is non-empty string and all characters are digits (using str.isdigit()).
Examples:
Usage:
assert_that('1234567890').is_digit()
Returns:
| Name | Type | Description |
|---|---|---|
AssertionBuilder |
Self
|
returns this instance to chain to the next assertion |
Raises:
| Type | Description |
|---|---|
AssertionError
|
if val is not digits |
Source code in assertpy2/string.py
is_lower ¶
Asserts that val is non-empty string and all characters are lowercase (using str.lower()).
Examples:
Usage:
assert_that('foo').is_lower()
Returns:
| Name | Type | Description |
|---|---|---|
AssertionBuilder |
Self
|
returns this instance to chain to the next assertion |
Raises:
| Type | Description |
|---|---|
AssertionError
|
if val is not lowercase |
Source code in assertpy2/string.py
is_upper ¶
Asserts that val is non-empty string and all characters are uppercase (using str.upper()).
Examples:
Usage:
assert_that('FOO').is_upper()
Returns:
| Name | Type | Description |
|---|---|---|
AssertionBuilder |
Self
|
returns this instance to chain to the next assertion |
Raises:
| Type | Description |
|---|---|
AssertionError
|
if val is not uppercase |
Source code in assertpy2/string.py
is_alphanumeric ¶
Asserts that val is non-empty string and all characters are alphanumeric (using str.isalnum()).
Examples:
Usage:
assert_that('abc123').is_alphanumeric()
Returns:
| Name | Type | Description |
|---|---|---|
AssertionBuilder |
Self
|
returns this instance to chain to the next assertion |
Raises:
| Type | Description |
|---|---|
AssertionError
|
if val is not alphanumeric |
Source code in assertpy2/string.py
is_whitespace ¶
Asserts that val is non-empty string and all characters are whitespace (using str.isspace()).
Examples:
Usage:
assert_that(' ').is_whitespace()
assert_that('\t\n').is_whitespace()
Returns:
| Name | Type | Description |
|---|---|---|
AssertionBuilder |
Self
|
returns this instance to chain to the next assertion |
Raises:
| Type | Description |
|---|---|
AssertionError
|
if val is not whitespace |
Source code in assertpy2/string.py
contains_any_of ¶
Asserts that val is a string and contains at least one of the given items.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*items
|
str
|
the items, at least one of which is expected to be contained |
()
|
Examples:
Usage:
assert_that('foobar').contains_any_of('foo', 'xxx')
assert_that('foobar').contains_any_of('xxx', 'bar')
Returns:
| Name | Type | Description |
|---|---|---|
AssertionBuilder |
Self
|
returns this instance to chain to the next assertion |
Raises:
| Type | Description |
|---|---|
AssertionError
|
if val does not contain any of the items |
Source code in assertpy2/string.py
contains_none_of ¶
Asserts that val is a string and contains none of the given items.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*items
|
str
|
the items, none of which should be contained |
()
|
Examples:
Usage:
assert_that('foobar').contains_none_of('xxx', 'yyy')
Returns:
| Name | Type | Description |
|---|---|---|
AssertionBuilder |
Self
|
returns this instance to chain to the next assertion |
Raises:
| Type | Description |
|---|---|
AssertionError
|
if val does contain any of the items |
Source code in assertpy2/string.py
is_unicode ¶
Asserts that val is a str.
Retained for assertpy compatibility: every str is unicode on Python 3, so this is
effectively an isinstance(val, str) check.
Examples:
Usage:
assert_that('foo').is_unicode()
Returns:
| Name | Type | Description |
|---|---|---|
AssertionBuilder |
Self
|
returns this instance to chain to the next assertion |
Raises:
| Type | Description |
|---|---|
AssertionError
|
if val is not a |
Source code in assertpy2/string.py
extracting_group ¶
Search val for pattern and return a new builder whose val is the captured group.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pattern
|
str
|
the regular expression pattern (must contain at least one group) |
required |
group
|
int | str
|
the group index (int) or name (str) to extract. Defaults to |
0
|
Examples:
Usage with positional groups:
assert_that("status=200 path=/api").extracting_group(r"status=(\d+)", 1).is_equal_to("200")
Usage with named groups:
assert_that("2024-01-15 ERROR").extracting_group(
r"(?P<level>\w+)$", "level"
).is_equal_to("ERROR")
Returns:
| Name | Type | Description |
|---|---|---|
AssertionBuilder |
Self
|
a new builder whose val is the extracted group string |
Raises:
| Type | Description |
|---|---|
TypeError
|
if val is not a string or pattern is not a string |
ValueError
|
if pattern is empty |
AssertionError
|
if the pattern does not match val or the group does not exist |
Source code in assertpy2/string.py
matches_with_groups ¶
Search val for pattern and return a new builder whose val is the tuple of all groups.
If the pattern contains named groups, the builder val is a dict
of {name: value} for all named groups. Otherwise it is the
tuple returned by Match.groups().
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pattern
|
str
|
the regular expression pattern with one or more groups |
required |
Examples:
Positional groups:
assert_that("2024-01-15 ERROR").matches_with_groups(
r"(\d{4}-\d{2}-\d{2}) (\w+)"
).is_length(2)
Named groups:
assert_that("status=200").matches_with_groups(
r"(?P<key>\w+)=(?P<val>\w+)"
).contains_key("key").contains_key("val")
Returns:
| Name | Type | Description |
|---|---|---|
AssertionBuilder |
Self
|
a new builder whose val is the groups tuple or groupdict |
Raises:
| Type | Description |
|---|---|
TypeError
|
if val is not a string or pattern is not a string |
ValueError
|
if pattern is empty |
AssertionError
|
if the pattern does not match val |