Object Matchers¶
Matchers that inspect objects.
equal_to¶
-
class
hamcrest.core.core.isequal.
IsEqual
(equals: Any)¶ Bases:
hamcrest.core.base_matcher.BaseMatcher
-
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.
-
-
hamcrest.core.core.isequal.
equal_to
(obj: Any) → hamcrest.core.matcher.Matcher[typing.Any][Any]¶ Matches if object is equal to a given object.
Parameters: obj – The object to compare against as the expected value. This matcher compares the evaluated object to
obj
for equality.
has_length¶
-
class
hamcrest.library.object.haslength.
HasLength
(len_matcher: hamcrest.core.matcher.Matcher[int][int])¶ Bases:
hamcrest.core.base_matcher.BaseMatcher
-
describe_mismatch
(item: collections.abc.Sized, 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)
isFalse
, but will not check this.Parameters: - item – The item that the
Matcher
has rejected. - mismatch_description – The description to be built or appended to.
- item – The item that the
-
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.
-
-
hamcrest.library.object.haslength.
has_length
(match: Union[int, hamcrest.core.matcher.Matcher[int][int]]) → hamcrest.core.matcher.Matcher[collections.abc.Sized][collections.abc.Sized]¶ Matches if
len(item)
satisfies a given matcher.Parameters: match – The matcher to satisfy, or an expected value for equal_to
matching.This matcher invokes the
len
function on the evaluated object to get its length, passing the result to a given matcher for evaluation.If the
match
argument is not a matcher, it is implicitly wrapped in anequal_to
matcher to check for :equality.Examples:
has_length(greater_than(6)) has_length(5)
has_string¶
-
class
hamcrest.library.object.hasstring.
HasString
(str_matcher: hamcrest.core.matcher.Matcher[str][str])¶ Bases:
hamcrest.core.base_matcher.BaseMatcher
-
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.
-
-
hamcrest.library.object.hasstring.
has_string
(match) → hamcrest.core.matcher.Matcher[object][object]¶ Matches if
str(item)
satisfies a given matcher.Parameters: match – The matcher to satisfy, or an expected value for equal_to
matching.This matcher invokes the
str
function on the evaluated object to get its length, passing the result to a given matcher for evaluation. If thematch
argument is not a matcher, it is implicitly wrapped in anequal_to
matcher to check for equality.Examples:
has_string(starts_with('foo')) has_string('bar')
has_properties/has_property¶
-
class
hamcrest.library.object.hasproperty.
IsObjectWithProperty
(property_name: str, value_matcher: hamcrest.core.matcher.Matcher[~V][V])¶ Bases:
hamcrest.core.base_matcher.BaseMatcher
-
describe_mismatch
(item: object, 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)
isFalse
, but will not check this.Parameters: - item – The item that the
Matcher
has rejected. - mismatch_description – The description to be built or appended to.
- item – The item that the
-
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.
-
-
hamcrest.library.object.hasproperty.
has_properties
(*keys_valuematchers, **kv_args)¶ Matches if an object has properties satisfying all of a dictionary of string property names and corresponding value matchers.
Parameters: matcher_dict – A dictionary mapping keys to associated value matchers, or to expected values for equal_to
matching.Note that the keys must be actual keys, not matchers. Any value argument that is not a matcher is implicitly wrapped in an
equal_to
matcher to check for equality.Examples:
has_properties({'foo':equal_to(1), 'bar':equal_to(2)}) has_properties({'foo':1, 'bar':2})
has_properties
also accepts a list of keyword arguments:-
hamcrest.library.object.hasproperty.
has_properties
(keyword1=value_matcher1[, keyword2=value_matcher2[, ...]])
Parameters: - keyword1 – A keyword to look up.
- valueMatcher1 – The matcher to satisfy for the value, or an expected
value for
equal_to
matching.
Examples:
has_properties(foo=equal_to(1), bar=equal_to(2)) has_properties(foo=1, bar=2)
Finally,
has_properties
also accepts a list of alternating keys and their value matchers:-
hamcrest.library.object.hasproperty.
has_properties
(key1, value_matcher1[, ...])
Parameters: - key1 – A key (not a matcher) to look up.
- valueMatcher1 – The matcher to satisfy for the value, or an expected
value for
equal_to
matching.
Examples:
has_properties('foo', equal_to(1), 'bar', equal_to(2)) has_properties('foo', 1, 'bar', 2)
-
-
hamcrest.library.object.hasproperty.
has_property
(name: str, match: Union[None, hamcrest.core.matcher.Matcher[~V][V], V] = None) → hamcrest.core.matcher.Matcher[object][object]¶ Matches if object has a property with a given name whose value satisfies a given matcher.
Parameters: - name – The name of the property.
- match – Optional matcher to satisfy.
This matcher determines if the evaluated object has a property with a given name. If no such property is found,
has_property
is not satisfied.If the property is found, its value is passed to a given matcher for evaluation. If the
match
argument is not a matcher, it is implicitly wrapped in anequal_to
matcher to check for equality.If the
match
argument is not provided, theanything
matcher is used so thathas_property
is satisfied if a matching property is found.Examples:
has_property('name', starts_with('J')) has_property('name', 'Jon') has_property('name')
instance_of¶
-
class
hamcrest.core.core.isinstanceof.
IsInstanceOf
(expected_type: Type[CT_co])¶ Bases:
hamcrest.core.base_matcher.BaseMatcher
-
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.
-
-
hamcrest.core.core.isinstanceof.
instance_of
(atype: Type[CT_co]) → hamcrest.core.matcher.Matcher[object][object]¶ Matches if object is an instance of, or inherits from, a given type.
Parameters: atype – The type to compare against as the expected type. This matcher checks whether the evaluated object is an instance of
atype
or an instance of any class that inherits fromatype
.Example:
instance_of(str)
none, not_none¶
-
class
hamcrest.core.core.isnone.
IsNone
¶ Bases:
hamcrest.core.base_matcher.BaseMatcher
-
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.
-
-
hamcrest.core.core.isnone.
none
() → hamcrest.core.matcher.Matcher[typing.Union[typing.Any, NoneType]][Optional[Any]]¶ Matches if object is
None
.
-
hamcrest.core.core.isnone.
not_none
() → hamcrest.core.matcher.Matcher[typing.Union[typing.Any, NoneType]][Optional[Any]]¶ Matches if object is not
None
.
same_instance¶
-
class
hamcrest.core.core.issame.
IsSame
(obj: T)¶ Bases:
hamcrest.core.base_matcher.BaseMatcher
-
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)
isFalse
, but will not check this.Parameters: - item – The item that the
Matcher
has rejected. - mismatch_description – The description to be built or appended to.
- item – The item that the
-
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.
-
-
hamcrest.core.core.issame.
same_instance
(obj: T) → hamcrest.core.matcher.Matcher[~T][T]¶ Matches if evaluated object is the same instance as a given object.
Parameters: obj – The object to compare against as the expected value. This matcher invokes the
is
identity operator to determine if the evaluated object is the the same object asobj
.
calling, raises¶
-
class
hamcrest.core.core.raises.
Raises
(expected: Type[Exception], pattern: Optional[str] = None, matching: Optional[hamcrest.core.matcher.Matcher] = None)¶ Bases:
hamcrest.core.base_matcher.BaseMatcher
-
describe_match
(item, 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)
isTrue
, but will not check this.Parameters: - item – The item that the
Matcher
has accepted. - match_description – The description to be built or appended to.
- item – The item that the
-
describe_mismatch
(item, 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)
isFalse
, but will not check this.Parameters: - item – The item that the
Matcher
has rejected. - mismatch_description – The description to be built or appended to.
- item – The item that the
-
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.
-
-
hamcrest.core.core.raises.
calling
(func: Callable[[...], Any]) → hamcrest.core.core.raises.DeferredCallable¶ Wrapper for function call that delays the actual execution so that
raises
matcher can catch any thrown exception.Parameters: func – The function or method to be called The arguments can be provided with a call to the
with_args
function on the returned object:calling(my_method).with_args(arguments, and_='keywords')
-
hamcrest.core.core.raises.
raises
(exception: Type[Exception], pattern=None, matching=None) → hamcrest.core.matcher.Matcher[typing.Callable[..., typing.Any]][Callable[[...], Any]]¶ Matches if the called function raised the expected exception.
Parameters: - exception – The class of the expected exception
- pattern – Optional regular expression to match exception message.
- matching – Optional Hamcrest matchers to apply to the exception.
Expects the actual to be wrapped by using
calling
, or a callable taking no arguments. Optional argument pattern should be a string containing a regular expression. If provided, the string representation of the actual exception - e.g.str(actual)
- must match pattern.Examples:
assert_that(calling(int).with_args('q'), raises(TypeError)) assert_that(calling(parse, broken_input), raises(ValueError)) assert_that( calling(valid_user, bad_json), raises(HTTPError, matching=has_properties(status_code=500) )