contracts package¶
What follows is an API reference detailing the various functions available in code-contracts.
contracts.assertion module¶
Module containing code assertions.
It complements the assertions already available in the TestCase
class.
-
contracts.assertion.
contains_one_element_of_class
(obj_cls, iterable_obj)[source]¶ Asserts that the specified iterable object contains one and only one element of the specified class.
Parameters: - obj_cls – the class of the object that is expected to be found in the iterable.
- iterable_obj –
Iterable
object.
Raises: AssertionError
if the callable does not raise an exception of the specified class.
-
contracts.assertion.
does_not_raise
(exception_cls, callable_obj, *args, **kwargs)[source]¶ Asserts that the specified callable object does not raise an exception of the specified class when called.
Parameters: - exception_cls – the class of the exception.
- callable_obj – the callable object (function or method).
- args – the positional arguments to pass to the callable.
- kwargs – the keyword arguments to pass to the callable.
Raises: AssertionError
if the callable raises an exception of the specified class.
-
contracts.assertion.
not_called_with
(mock_obj, *args, **kwargs)[source]¶ Asserts that the specified mock object was never called with a specific sequence of arguments.
Acts as a complementary function, because there is no
assert_not_called_with()
method available in theMock
class.Parameters: - mock_obj –
Mock
object. - args – the positional arguments to pass to the callable.
- kwargs – the keyword arguments to pass to the callable.
Raises: AssertionError
if the callable does not raise an exception of the specified class.- mock_obj –
-
contracts.assertion.
raises
(exception_cls, callable_obj, *args, **kwargs)[source]¶ Asserts that the specified callable object raises an exception of the specified class when called.
Can be used instead of
assertRaises()
for consistency across unit tests, especially whendoes_not_raise()
is also used.Parameters: - exception_cls – the class of the exception.
- callable_obj – the callable object (function or method).
- args – the positional arguments to pass to the callable.
- kwargs – the keyword arguments to pass to the callable.
Raises: AssertionError
if the callable does not raise an exception of the specified class.
-
contracts.assertion.
raises_with_msg
(exception_cls, callable_obj, expected_exception_msg, *args, **kwargs)[source]¶ Asserts that the specified callable object raises an exception of the specified type with the specified message when called.
Parameters: - exception_cls – the class of the exception.
- callable_obj – the callable object (function or method).
- expected_exception_msg – the expected substring in the exception’s message.
- args – the positional arguments to pass to the callable.
- kwargs – the keyword arguments to pass to the callable.
Raises: AssertionError
if the callable does not raise an exception of the specified class.
contracts.contract module¶
Module containing code contracts.
-
contracts.contract.
all_have_attribute
(value, attribute_name)[source]¶ Checks that all objects contained in value - be it a single object or an
Iterable
of objects - have the specified attribute.Parameters: - value – the value to check. It can be a single object, or an an
Iterable
of objects. - attribute_name – a string containing the name of the attribute to look for.
Raises: AttributeError
if one of the objects does not contain the specified attribute.- value – the value to check. It can be a single object, or an an
-
contracts.contract.
all_have_method
(value, method_name)[source]¶ Checks that all objects contained in value - be it a single object or an
Iterable
of objects - have the specified method.Parameters: - value – the value to check. It can be a single object, or an an
Iterable
of objects. - method_name – a string containing the name of the method to look for.
Raises: AttributeError
if one of the objects does not contain the specified method.- value – the value to check. It can be a single object, or an an
-
contracts.contract.
is_callable
(value)[source]¶ Checks that the specified value is a callable object (i.e. has a ‘__call__’ attribute).
Parameters: value – the value to check. Raises: TypeError
if the value is not a callable object.
-
contracts.contract.
is_equal
(value, expected_value, expression_str=None)[source]¶ Checks that the specified value is strictly equal to the expected value.
Parameters: - value – the value to check.
- expected_value – the expected value.
- expression_str – (optional) a string representing the evaluated boolean expression (e.g. ‘len(a) == 1’).
Raises: ValueError
if the values are not strictly equal.
-
contracts.contract.
is_equal_to_any
(value, expected_values, expression_str=None)[source]¶ Checks that the specified value is equal to at least one of the expected values.
Parameters: - value – the value to check.
- expected_values – an
Iterable
object containing the expected values. - expression_str – (optional) a string representing the evaluated boolean expression (e.g. ‘b > c’).
Raises: ValueError
if the value is not equal to any of the expected values.
-
contracts.contract.
is_false
(value, expression_str=None)[source]¶ Checks that the specified value is equal to False.
Parameters: - value – the value to check. It can be a standard variable’s value, or the result of an evaluated boolean expression (e.g. a or b > c).
- expression_str – (optional) a string representing the evaluated boolean expression (e.g. ‘b > c’).
Raises: TypeError
if the value is not False.
-
contracts.contract.
is_greater_than
(value, expected_value)[source]¶ Checks that the specified value is greater than the expected value.
Parameters: - value – the value to check.
- expected_value – the expected value.
Raises: ValueError
if the value is not greater than the expected value.
-
contracts.contract.
is_greater_than_or_equal
(value, expected_value)[source]¶ Checks that the specified value is greater than or strictly equal to the expected value.
Parameters: - value – the value to check.
- expected_value – the expected value.
Raises: ValueError
if the value is not greater than or strictly equal to the expected value.
-
contracts.contract.
is_instance
(value, cls)[source]¶ Checks that the specified value is an instance of the given class.
Parameters: - value – the value to check.
- cls – the expected class of the value.
Raises: TypeError
if the value is not an instance of the class.
-
contracts.contract.
is_not_empty
(value)[source]¶ Checks that the specified value is not empty.
Parameters: value – the value to check. To be considered empty, it must be equal to None, or equal to “” if it’s a string. If it’s an Iterable
object, its length must be equal to 0. Any other object type will never be considered empty.Raises: ValueError
if the value is considered empty.
-
contracts.contract.
is_not_none
(value)[source]¶ Checks that the specified value is not equal to None.
Parameters: value – the value to check. Raises: TypeError
if the value is equal to None.
-
contracts.contract.
is_true
(value, expression_str=None)[source]¶ Checks that the specified value is equal to True.
Parameters: - value – the value to check. It can be a standard variable’s value, or the result of an evaluated boolean expression (e.g. a or b > c).
- expression_str – (optional) a string representing the evaluated boolean expression (e.g. ‘b > c’).
Raises: TypeError
if the value is not True.