Containment assertions¶
The contains family, shared by strings, collections, dicts, and bytes.
Containment assertions mixin.
contains ¶
Asserts that val contains the given item or items.
Checks if the collection contains the given item or items using in operator.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*items
|
object
|
the item or items expected to be contained |
()
|
Examples:
Usage:
assert_that('foo').contains('f')
assert_that('foo').contains('f', 'oo')
assert_that(['a', 'b']).contains('b', 'a')
assert_that((1, 2, 3)).contains(3, 2, 1)
assert_that({'a': 1, 'b': 2}).contains('b', 'a') # checks keys
assert_that({'a', 'b'}).contains('b', 'a')
assert_that([1, 2, 3]).is_type_of(list).contains(1, 2).does_not_contain(4, 5)
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 item or items |
TypeError
|
if val is not a container or iterable |
Note
Where one item is not found, the failure says what is nearest to it: the closest row among the elements, and on a line under the message why it is not there, where one fact about it says so.
Tip
Use the contains_key() alias when working with
dict-like objects to be self-documenting.
See Also
contains_ignoring_case() -
for case-insensitive string contains
Source code in assertpy/contains.py
400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 | |
does_not_contain ¶
Asserts that val does not contain the given item or items.
Checks if the collection excludes the given item or items using in operator.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*items
|
object
|
the item or items expected to be excluded |
()
|
Examples:
Usage:
assert_that('foo').does_not_contain('x')
assert_that(['a', 'b']).does_not_contain('x', 'y')
assert_that((1, 2, 3)).does_not_contain(4, 5)
assert_that({'a': 1, 'b': 2}).does_not_contain('x', 'y') # checks keys
assert_that({'a', 'b'}).does_not_contain('x', 'y')
assert_that([1, 2, 3]).is_type_of(list).contains(1, 2).does_not_contain(4, 5)
Returns:
| Name | Type | Description |
|---|---|---|
AssertionBuilder |
Self
|
returns this instance to chain to the next assertion |
Raises:
| Type | Description |
|---|---|
AssertionError
|
if val does contain the item or items |
TypeError
|
if val is not a container or iterable |
Note
Accepts a Matcher for any item, the same as
contains(): the assertion then fails when any
item of val matches it.
Tip
Use the does_not_contain_key() alias when working with
dict-like objects to be self-documenting.
Source code in assertpy/contains.py
contains_only ¶
Asserts that val contains only the given item or items.
Checks if the collection contains only the given item or items using in operator.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*items
|
object
|
the only item or items expected to be contained |
()
|
Examples:
Usage:
assert_that('foo').contains_only('f', 'o')
assert_that(['a', 'a', 'b']).contains_only('a', 'b')
assert_that((1, 1, 2)).contains_only(1, 2)
assert_that({'a': 1, 'a': 2, 'b': 3}).contains_only('a', 'b')
assert_that({'a', 'a', 'b'}).contains_only('a', 'b')
Returns:
| Name | Type | Description |
|---|---|---|
AssertionBuilder |
Self
|
returns this instance to chain to the next assertion |
Raises:
| Type | Description |
|---|---|
AssertionError
|
if val contains anything not item or items |
Note
Where one item is not found, the failure says what is nearest to it: the closest row among the elements nobody asked for, and on a line under the message why it is not there, where one fact about it says so.
Source code in assertpy/contains.py
contains_sequence ¶
Asserts that val contains the given ordered sequence of items.
Checks if the collection contains the given sequence of items using in operator.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*items
|
object
|
the sequence of items expected to be contained |
()
|
Examples:
Usage:
assert_that('foo').contains_sequence('f', 'o')
assert_that('foo').contains_sequence('o', 'o')
assert_that(['a', 'b', 'c']).contains_sequence('b', 'c')
assert_that((1, 2, 3)).contains_sequence(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 contains the given sequence of items |
Source code in assertpy/contains.py
contains_duplicates ¶
Asserts that val is iterable and does contain duplicates.
Examples:
Usage:
assert_that('foo').contains_duplicates()
assert_that(['a', 'a', 'b']).contains_duplicates()
assert_that((1, 1, 2)).contains_duplicates()
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 duplicates |
Source code in assertpy/contains.py
does_not_contain_duplicates ¶
Asserts that val is iterable and does not contain any duplicates.
Examples:
Usage:
assert_that('fox').does_not_contain_duplicates()
assert_that(['a', 'b', 'c']).does_not_contain_duplicates()
assert_that((1, 2, 3)).does_not_contain_duplicates()
Returns:
| Name | Type | Description |
|---|---|---|
AssertionBuilder |
Self
|
returns this instance to chain to the next assertion |
Raises:
| Type | Description |
|---|---|
AssertionError
|
if val does contain duplicates |
Source code in assertpy/contains.py
is_empty ¶
Asserts that val is empty.
Examples:
Usage:
assert_that('').is_empty()
assert_that([]).is_empty()
assert_that(()).is_empty()
assert_that({}).is_empty()
assert_that(set()).is_empty()
Returns:
| Name | Type | Description |
|---|---|---|
AssertionBuilder |
Self
|
returns this instance to chain to the next assertion |
Raises:
| Type | Description |
|---|---|
AssertionError
|
if val is not empty |
Source code in assertpy/contains.py
is_not_empty ¶
Asserts that val is not empty.
Examples:
Usage:
assert_that('foo').is_not_empty()
assert_that(['a', 'b']).is_not_empty()
assert_that((1, 2, 3)).is_not_empty()
assert_that({'a': 1, 'b': 2}).is_not_empty()
assert_that({'a', 'b'}).is_not_empty()
Returns:
| Name | Type | Description |
|---|---|---|
AssertionBuilder |
Self
|
returns this instance to chain to the next assertion |
Raises:
| Type | Description |
|---|---|
AssertionError
|
if val is empty |
Source code in assertpy/contains.py
contains_exactly ¶
Asserts that val contains exactly the given items in the given order.
Unlike contains_only() (which ignores
order) and contains_sequence()
(which allows extra items), this method requires exact count, items, and order.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*items
|
object
|
the items expected, in exact order |
()
|
Examples:
Usage:
assert_that([1, 2, 3]).contains_exactly(1, 2, 3)
assert_that(['a', 'b']).contains_exactly('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 exactly the given items in order |
Source code in assertpy/contains.py
contains_exactly_in_any_order ¶
Asserts that val contains exactly the given items, in any order.
Like contains_exactly() but ignoring
order: val and the given items must be equal as multisets, so duplicates count (each item
must occur exactly as many times as given). Unlike
contains_only() (which checks membership
both ways and ignores counts), an extra duplicate or a missing one fails.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*items
|
object
|
the items expected, in any order |
()
|
Examples:
Usage:
assert_that([3, 1, 2]).contains_exactly_in_any_order(1, 2, 3)
assert_that(['b', 'a', 'b']).contains_exactly_in_any_order('a', 'b', 'b')
assert_that([1, 2, 2]).contains_exactly_in_any_order(1, 2) # fails (extra 2)
assert_that([1, 2]).contains_exactly_in_any_order(1, 2, 2) # fails (missing 2)
Returns:
| Name | Type | Description |
|---|---|---|
AssertionBuilder |
Self
|
returns this instance to chain to the next assertion |
Raises:
| Type | Description |
|---|---|
AssertionError
|
if val does not contain exactly the given items in any order |
TypeError
|
if val is not iterable |
ValueError
|
if no items are given |
Source code in assertpy/contains.py
contains_in_order ¶
Asserts that val contains the given items in the given order (as a subsequence).
Items must appear in the given order but do not need to be contiguous.
Unlike contains_sequence() which
requires contiguous items.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*items
|
object
|
the items expected, in order (but not necessarily contiguous) |
()
|
Examples:
Usage:
assert_that([1, 5, 2, 8, 3]).contains_in_order(1, 2, 3)
assert_that(['a', 'x', 'b', 'y', 'c']).contains_in_order('a', 'b', 'c')
Returns:
| Name | Type | Description |
|---|---|---|
AssertionBuilder |
Self
|
returns this instance to chain to the next assertion |
Raises:
| Type | Description |
|---|---|
AssertionError
|
if val does not contain items in the given order |
Source code in assertpy/contains.py
contains_only_once ¶
Asserts that val contains each given item exactly once.
Each given item must appear in val with a count of exactly one: an item absent from val is reported as missing, an item occurring more than once is reported as duplicated.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*items
|
object
|
the items each expected to occur exactly once |
()
|
Examples:
Usage:
assert_that([1, 2, 3]).contains_only_once(1, 3)
assert_that('foo').contains_only_once('f')
assert_that([1, 2, 2, 3]).contains_only_once(2) # fails (occurs twice)
assert_that([1, 2, 3]).contains_only_once(4) # fails (missing)
Returns:
| Name | Type | Description |
|---|---|---|
AssertionBuilder |
Self
|
returns this instance to chain to the next assertion |
Raises:
| Type | Description |
|---|---|
AssertionError
|
if any given item is missing from val or occurs more than once |
TypeError
|
if val is not iterable |
ValueError
|
if no items are given |
Source code in assertpy/contains.py
is_in ¶
Asserts that val is equal to one of the given items.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*items
|
object
|
the items expected to contain val |
()
|
Examples:
Usage:
assert_that('foo').is_in('foo', 'bar', 'baz')
assert_that(1).is_in(0, 1, 2, 3)
Returns:
| Name | Type | Description |
|---|---|---|
AssertionBuilder |
Self
|
returns this instance to chain to the next assertion |
Raises:
| Type | Description |
|---|---|
AssertionError
|
if val is not in the given items |
Note
The failure says what is nearest to val: the closest row among the given items, and on a line under the message why val is in none of them, where one fact about it says so.
Source code in assertpy/contains.py
is_not_in ¶
Asserts that val is not equal to one of the given items.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*items
|
object
|
the items expected to exclude val |
()
|
Examples:
Usage:
assert_that('foo').is_not_in('bar', 'baz', 'box')
assert_that(1).is_not_in(-1, -2, -3)
Returns:
| Name | Type | Description |
|---|---|---|
AssertionBuilder |
Self
|
returns this instance to chain to the next assertion |
Raises:
| Type | Description |
|---|---|
AssertionError
|
if val is in the given items |