Kotest Collection Matchers

Introduces rich matchers available for Collection, Iterable, and Array types.

Also see inspectors, which are useful for testing several elements in a collection.

Collections
collection.shouldBeEmpty() Checks that the collection has zero elements.
collection.shouldBeUnique() Checks that all elements in the collection are distinct by natural equality.
collection.shouldBeUnique(comparator) Checks that all elements are unique using the given comparator.
collection.shouldContain(element) Checks that the collection contains the specified element.
collection.shouldContainAll(e1, e2, ..., en) Checks that the collection contains all listed elements, regardless of order.
collection.shouldContainDuplicates() Checks that the collection contains at least one duplicated element.
collection.shouldContainExactly() Checks that the collection contains only the given values in order and no other values.
collection.shouldContainExactlyInAnyOrder() Checks that the collection contains exactly the given values in any order and nothing else.
collection.shouldContainAllInAnyOrder() Checks that the collection contains all given values in any order and no other values.
collection.shouldContainNoNulls() Checks that the collection has no null elements, or is empty.
collection.shouldContainNull() Checks that the collection contains at least one null element.
collection.shouldContainOnlyNulls() Checks that the collection contains only null elements, or is empty.
collection.shouldContainAllIgnoringFields() Checks that all listed elements are in the collection while ignoring one or more fields.
collection.shouldHaveSingleElement(element) Checks that the collection contains exactly one element and that it is the specified element.
collection.shouldHaveSingleElement { block } Checks that the collection contains a single element matching the given predicate.
collection.shouldHaveSize(length) Checks that the collection has exactly the given length.
collection.shouldBeSingleton() Checks that the collection contains exactly one element.
collection.shouldBeSingleton { block } Checks that the collection contains exactly one element, then runs the block with that element.
collection.shouldHaveLowerBound(element) Checks that the given element is less than or equal to every element in the collection. Works only for elements that implement Comparable.
collection.shouldHaveUpperBound(element) Checks that the given element is greater than or equal to every element in the collection. Works only for elements that implement Comparable.
collection.shouldBeSmallerThan(col) Checks that the collection is smaller than another collection.
collection.shouldBeLargerThan(col) Checks that the collection is larger than another collection.
collection.shouldBeSameSizeAs(col) Checks that the collection has the same size as another collection.
collection.shouldHaveAtLeastSize(n) Checks that the collection size is at least n.
collection.shouldHaveAtMostSize(n) Checks that the collection size is at most n.
list.shouldBeSorted() Checks that the list is sorted.
list.shouldBeSortedBy { transform } Checks that the list is sorted by the values after applying the transform.
list.shouldContainInOrder(other) Checks that this list contains the given list in order. Other elements may appear before or after it.
list.shouldExistInOrder({ element }, ...) Checks that the list contains elements matching the predicates in order. Other elements may appear around or between matching elements.
list.shouldHaveElementAt(index, element) Checks that this list contains the given element at the given position.
list.shouldStartWith(lst) Checks that this list starts with the elements of the given list in order.
list.shouldEndWith(lst) Checks that this list ends with the elements of the given list in order.
iterable.shouldMatchEach(assertions) Iterates this list and the assertions, and checks that each element passes the associated assertion. Fails if the collection sizes differ.
iterable.shouldMatchInOrder(assertions) Checks that this iterable has a contiguous subsequence matching the assertions in order.
iterable.shouldMatchInOrderSubset(assertions) Checks that this iterable has a subsequence matching the assertions in order, with gaps allowed.
value.shouldBeOneOf(collection) Checks that the specific instance is contained in the collection.
collection.shouldContainAnyOf(collection) Checks that the collection contains at least one element from the given collection.
value.shouldBeIn(collection) Checks that the object is contained in the collection by value, not by reference.

References