Decorator Matchers

Matchers that decorate other matchers for better expression.

described_as

hamcrest.core.core.described_as.described_as(description, matcher[, value1[, ...]])

Adds custom failure description to a given matcher.

Parameters:
  • description – Overrides the matcher’s description.
  • matcher – The matcher to satisfy.
  • value1,... – Optional comma-separated list of substitution values.

The description may contain substitution placeholders %0, %1, etc. These will be replaced by any values that follow the matcher.

is_

hamcrest.core.core.is_.is_(x)

Decorates another matcher, or provides shortcuts to the frequently used is(equal_to(x)) and is(instance_of(x)).

Parameters:x – The matcher to satisfy, or a type for instance_of matching, or an expected value for equal_to matching.

This matcher compares the evaluated object to the given matcher.

Note

PyHamcrest’s is_ matcher is unrelated to Python’s is operator. The matcher for object identity is same_instance.

If the x argument is a matcher, its behavior is retained, but the test may be more expressive. For example:

assert_that(value, less_than(5))
assert_that(value, is_(less_than(5)))

If the x argument is a type, it is wrapped in an instance_of matcher. This makes the following statements equivalent:

assert_that(cheese, instance_of(Cheddar))
assert_that(cheese, is_(instance_of(Cheddar)))
assert_that(cheese, is_(Cheddar))

Otherwise, if the x argument is not a matcher, it is wrapped in an equal_to matcher. This makes the following statements equivalent:

assert_that(cheese, equal_to(smelly))
assert_that(cheese, is_(equal_to(smelly)))
assert_that(cheese, is_(smelly))

Choose the style that makes your expression most readable. This will vary depending on context.