nti.testing.matchers#

Hamcrest matchers for testing.

class nti.testing.matchers.TypeCheckedDict(key_class=<class 'object'>, val_class=<class 'object'>, notify=None)[source]#

Bases: dict

A dictionary that ensures keys and values are of the required type when set

nti.testing.matchers.aq_inContextOf(parent)[source]#

Matches if the data is in the acquisition context of the given object.

nti.testing.matchers.has_attr(name: str, match: None | Matcher[V] | V = None) Matcher[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 an equal_to() matcher to check for equality.

If the match argument is not provided, the anything() matcher is used so that has_property is satisfied if a matching property is found.

Examples:

has_property('name', starts_with('J'))
has_property('name', 'Jon')
has_property('name')
nti.testing.matchers.has_length(match: int | Matcher[int]) Matcher[Sized][source]#

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 an equal_to() matcher to check for :equality.

Examples:

has_length(greater_than(6))
has_length(5)
nti.testing.matchers.implements(iface)[source]#

Matches if the object implements (is a factory for) the given interface.

nti.testing.matchers.is_false()[source]#

Matches an object with a false boolean value.

nti.testing.matchers.is_true()[source]#

Matches an object with a true boolean value.

nti.testing.matchers.not_validated_by(field, invalid=<class 'zope.interface.exceptions.Invalid'>)[source]#

Matches if the data is NOT validated by the given IField.

Parameters:

invalid (exception) – The types of exceptions that are considered invalid. Anything other than this is allowed to be raised.

Changed in version 2.0.1: Add invalid and change it from Exception to zope.interface.interfaces.Invalid

nti.testing.matchers.provides(iface)[source]#

Matches an object that provides the given interface.

nti.testing.matchers.validated_by(field, invalid=<class 'zope.interface.exceptions.Invalid'>)[source]#

Matches if the data is validated by the given IField.

Parameters:

invalid (exception) – The types of exceptions that are considered invalid. Anything other than this is allowed to be raised.

Changed in version 2.0.1: Add invalid and change it from Exception to zope.interface.interfaces.Invalid

nti.testing.matchers.validly_provides(*ifaces)[source]#

Matches if the object verifiably and validly provides the given schema (interface(s)).

Verification is done with zope.interface and verifiably_provides(), while validation is done with zope.schema.getValidationErrors().

nti.testing.matchers.verifiably_provides(*ifaces)[source]#

Matches if the object verifiably provides the correct interface(s), as defined by zope.interface.verify.verifyObject(). This means having the right attributes and methods with the right signatures.

Note

This does not test schema compliance. For that (stricter) test, see validly_provides().