Core API

BaseDescription

class hamcrest.core.base_description.BaseDescription

Bases: hamcrest.core.description.Description

Base class for all Description implementations.

append(string: str) → None

Append the string to the description.

append_description_of(value: Any) → hamcrest.core.description.Description

Appends description of given value to this description.

If the value implements describe_to, then it will be used.

Returns:self, for chaining
append_list(start: str, separator: str, end: str, list: Iterable[Any]) → hamcrest.core.description.Description

Appends a list of objects to the description.

Parameters:
  • start – String that will begin the list description.
  • separator – String that will separate each object in the description.
  • end – String that will end the list description.
  • list – List of objects to be described.
Returns:

self, for chaining

append_text(text: str) → hamcrest.core.description.Description

Appends some plain text to the description.

Returns:self, for chaining

BaseMatcher

class hamcrest.core.base_matcher.BaseMatcher

Bases: hamcrest.core.matcher.Matcher

Base class for all Matcher implementations.

Most implementations can just implement _matches, leaving the handling of any mismatch description to the matches method. But if it makes more sense to generate the mismatch description during the matching, override matches instead.

_matches(item: T) → bool
describe_match(item: T, match_description: hamcrest.core.description.Description) → None

Generates a description of why the matcher has accepted the item.

The description may be part of a larger description of why a matching failed, so it should be concise.

This method assumes that matches(item) is True, but will not check this.

Parameters:
  • item – The item that the Matcher has accepted.
  • match_description – The description to be built or appended to.
describe_mismatch(item: T, mismatch_description: hamcrest.core.description.Description) → None

Generates a description of why the matcher has not accepted the item.

The description will be part of a larger description of why a matching failed, so it should be concise.

This method assumes that matches(item) is False, but will not check this.

Parameters:
  • item – The item that the Matcher has rejected.
  • mismatch_description – The description to be built or appended to.
matches(item: T, mismatch_description: Optional[hamcrest.core.description.Description] = None) → bool

Evaluates the matcher for argument item.

If a mismatch is detected and argument mismatch_description is provided, it will generate a description of why the matcher has not accepted the item.

Parameters:
  • item – The object against which the matcher is evaluated.
  • mismatch_description
Returns:

True if item matches, otherwise False.

Description

class hamcrest.core.description.Description

Bases: object

A description of a Matcher.

A Matcher will describe itself to a description which can later be used for reporting.

append_description_of(value: Any) → hamcrest.core.description.Description

Appends description of given value to this description.

If the value implements describe_to, then it will be used.

Returns:self, for chaining
append_list(start: str, separator: str, end: str, list: Iterable[Any]) → hamcrest.core.description.Description

Appends a list of objects to the description.

Parameters:
  • start – String that will begin the list description.
  • separator – String that will separate each object in the description.
  • end – String that will end the list description.
  • list – List of objects to be described.
Returns:

self, for chaining

append_text(text: str) → hamcrest.core.description.Description

Appends some plain text to the description.

Returns:self, for chaining

Matcher

class hamcrest.core.matcher.Matcher

Bases: typing.Generic, hamcrest.core.selfdescribing.SelfDescribing

A matcher over acceptable values.

A matcher is able to describe itself to give feedback when it fails.

Matcher implementations should not directly implement this protocol. Instead, extend the BaseMatcher class, which will ensure that the Matcher API can grow to support new features and remain compatible with all Matcher implementations.

describe_match(item: T, match_description: hamcrest.core.description.Description) → None

Generates a description of why the matcher has accepted the item.

The description may be part of a larger description of why a matching failed, so it should be concise.

This method assumes that matches(item) is True, but will not check this.

Parameters:
  • item – The item that the Matcher has accepted.
  • match_description – The description to be built or appended to.
describe_mismatch(item: T, mismatch_description: hamcrest.core.description.Description) → None

Generates a description of why the matcher has not accepted the item.

The description will be part of a larger description of why a matching failed, so it should be concise.

This method assumes that matches(item) is False, but will not check this.

Parameters:
  • item – The item that the Matcher has rejected.
  • mismatch_description – The description to be built or appended to.
matches(item: T, mismatch_description: Optional[hamcrest.core.description.Description] = None) → bool

Evaluates the matcher for argument item.

If a mismatch is detected and argument mismatch_description is provided, it will generate a description of why the matcher has not accepted the item.

Parameters:
  • item – The object against which the matcher is evaluated.
  • mismatch_description
Returns:

True if item matches, otherwise False.

SelfDescribing

class hamcrest.core.selfdescribing.SelfDescribing

Bases: object

The ability of an object to describe itself.

describe_to(description: hamcrest.core.description.Description) → None

Generates a description of the object.

The description may be part of a description of a larger object of which this is just a component, so it should be worded appropriately.

Parameters:description – The description to be built or appended to.

SelfDescribingValue

class hamcrest.core.selfdescribingvalue.SelfDescribingValue(value: Any)

Bases: hamcrest.core.selfdescribing.SelfDescribing

Wrap any value in a SelfDescribingValue to satisfy the SelfDescribing interface.

Deprecated: No need for this class now that append_description_of handles any type of value.

describe_to(description: hamcrest.core.description.Description) → None

Generates a description of the value.

StringDescription

class hamcrest.core.string_description.StringDescription

Bases: hamcrest.core.base_description.BaseDescription

A Description that is stored as a string.

append(string: str) → None

Append the string to the description.

hamcrest.core.string_description.tostring(selfdescribing: hamcrest.core.selfdescribing.SelfDescribing) → str

Returns the description of a SelfDescribing object as a string.

Parameters:selfdescribing – The object to be described.
Returns:The description of the object.