Assertions

Assertions are used to assert conditions of your tests. With assertions the actual testing is realized.

Assert Statement

The easiest way to assert a statement is to use the assert statement.

def test_with_assert():
    assert True, "This is the assertions failure message"

Built-in Assertions

To write tests with finegrained assertions you can use the built-in assertions.

All specialized assertions can be used via the module htf.assertions or the assertions fixture.

def test_with_assertions_fixture(assertions):
    assertions.assert_true(True, "This is the assertions failure message")
    assertions.assert_almost_equal(1.00, 1.01, places=2)
    # etc.


def test_with_assertions_module():
    htf.assertions.assert_true(True, "This is the assertions failure message")
    htf.assertions.assert_almost_equal(1.00, 1.01, places=2)
htf.assertions.assert_almost_equal(a, b, places=None, delta=None, message=None)

Assert that a and b are almost equal. Useful to compare floating point numbers. Comparison is realized either using a maximum delta or the number of decimal places

Parameters
  • a (float) – number

  • b (float) – number

  • places=None (int) – the number of decimal places for comparison. If None 7 decimal places are used.

  • delta=None (float) – the maximum delta

  • message=None (str) – the message to prepend to the error message

Raises

AssertionError – if a and b are not almost equal

htf.assertions.assert_dict_equal(a, b, message=None)

Assert that a equals b and that both are of type dict.

Parameters
  • a – a dict

  • b – a dict

  • message=None (str) – the message to prepend to the error message

Raises

AssertionError – if a and b are not equal

htf.assertions.assert_equal(a, b, message=None)

Assert that a equals b. Tries to find a registered equality asserter and uses the base assert if no special assert is registered.

Parameters
  • a – an object

  • b – an object

  • message=None (str) – the message to prepend to the error message

Raises

AssertionError – if a and b are not equal

htf.assertions.assert_false(expression, message=None)

Assert that expression is False.

Parameters
  • expression (bool) – the boolean expression to be validated

  • message=None (str) – the message to prepend to the error message

Raises

AssertionError – if expression is not False

htf.assertions.assert_greater(a, b, message=None)

Assert that a is greater than b.

Parameters
  • a – a comparable object

  • b – a comparable object

  • message=None (str) – the message to prepend to the error message

Raises

AssertionError – if a is not greater than b

htf.assertions.assert_greater_equal(a, b, message=None)

Assert that a is greater or equal to b.

Parameters
  • a – a comparable object

  • b – a comparable object

  • message=None (str) – the message to prepend to the error message

Raises

AssertionError – if a is not greater or equal to b

htf.assertions.assert_in(member, container, message=None)

Assert that container contains member.

Parameters
  • member – the object that shall be included in container

  • container (iterable) – an iterable to search for member

  • message=None (str) – the message to prepend to the error message

Raises

AssertionError – if member is not included in container

htf.assertions.assert_is(a, b, message=None)

Assert that id(a) equals id(b).

Parameters
  • a – an object

  • b – another object

  • message=None (str) – the message to prepend to the error message

Raises

AssertionError – if id(a) does not equal id(b)

htf.assertions.assert_is_instance(obj, cls, message=None)

Assert that obj is an instance of cls.

Parameters
  • obj – an instance

  • cls – a class obj shall be an instance of

  • message=None (str) – the message to prepend to the error message

Raises

AssertionError – if obj is not an instance of cls

htf.assertions.assert_is_none(obj, message=None)

Assert that obj is None.

Parameters
  • obj – an object

  • message=None (str) – the message to prepend to the error message

Raises

AssertionError – if obj is not None

htf.assertions.assert_is_not(a, b, message=None)

Assert that id(a) does not equal id(b).

Parameters
  • a – an object

  • b – another object

  • message=None (str) – the message to prepend to the error message

Raises

AssertionError – if id(a) equals id(b)

htf.assertions.assert_is_not_instance(obj, cls, message=None)

Assert that obj is not an instance of cls.

Parameters
  • obj – an instance

  • cls – a class obj shall not be an instance of

  • message=None (str) – the message to prepend to the error message

Raises

AssertionError – if obj is an instance of cls

htf.assertions.assert_is_not_none(obj, message=None)

Assert that obj is not None.

Parameters
  • obj – an object

  • message=None (str) – the message to prepend to the error message

Raises

AssertionError – if obj is None

htf.assertions.assert_less(a, b, message=None)

Assert that a is less than b.

Parameters
  • a – a comparable object

  • b – a comparable object

  • message=None (str) – the message to prepend to the error message

Raises

AssertionError – if a is not less than b

htf.assertions.assert_less_equal(a, b, message=None)

Assert that a is less or equal to b.

Parameters
  • a – a comparable object

  • b – a comparable object

  • message=None (str) – the message to prepend to the error message

Raises

AssertionError – if a is not less or equal to b

htf.assertions.assert_list_equal(a, b, message=None)

Assert that a equals b and that both are of type list.

Parameters
  • a – a list

  • b – a list

  • message=None (str) – the message to prepend to the error message

Raises

AssertionError – if a and b are not equal or not lists

htf.assertions.assert_multi_line_equal(a, b, message=None)

Assert that a equals b and that both are of type str.

Parameters
  • a – a string

  • b – a string

  • message=None (str) – the message to prepend to the error message

Raises

AssertionError – if a and b are not equal or not strings

htf.assertions.assert_not_almost_equal(a, b, places=None, delta=None, message=None)

Assert that a and b are not almost equal. Useful to compare floating point numbers. Comparison is realized either using a maximum delta or the number of decimal places

Parameters
  • a (float) – number

  • b (float) – number

  • places=None (int) – the number of decimal places for comparison. If None 7 decimal places are used.

  • delta=None (float) – the maximum delta

  • message=None (str) – the message to prepend to the error message

Raises

AssertionError – if a and b are almost equal

htf.assertions.assert_not_equal(a, b, message=None)

Assert that a does not equal b.

Parameters
  • a – an object

  • b – an object

  • message=None (str) – the message to prepend to the error message

Raises

AssertionError – if a and b are equal

htf.assertions.assert_not_in(member, container, message=None)

Assert that container does not contain member.

Parameters
  • member – the object that shall not be included in container

  • container (iterable) – an iterable to search for member

  • message=None (str) – the message to prepend to the error message

Raises

AssertionError – if member is included in container

htf.assertions.assert_not_is_instance(obj, cls, message=None)

Assert that obj is not an instance of cls.

Parameters
  • obj – an instance

  • cls – a class obj shall not be an instance of

  • message=None (str) – the message to prepend to the error message

Raises

AssertionError – if obj is an instance of cls

htf.assertions.assert_not_regex(text, unexpected_regex, message=None)

Assert that text does not match regular expression unexpected_regex.

Parameters
  • text – a text to be matched

  • unexpected_regex – a regular expression to be used

  • message=None (str) – the message to prepend to the error message

Raises

AssertionError – if text matches regular expression unexpected_regex

htf.assertions.assert_raises(expected_exception, callable_object=None, *args, **kwargs)

Assert that expected_exception is raised. Can be used as a context manager or with a callable object that is run with *args and **kwargs.

Parameters
  • expected_exception (type) – the expected exception that shall be raised

  • callable_object=None (callable) – the callable object if not used as a context manager

  • *args – variable positional arguments passed to callable_object

  • **kwargs – variable keyword arguments passed to callable_object

Returns

a context manager if callable_object is None else None.

Raises

AssertionError – if expected_exception is not raised

htf.assertions.assert_raises_regex(expected_exception, expected_regex, callable_object=None, *args, **kwargs)

Assert that expected_exception is raised. Can be used as a context manager or with a callable object that is run with *args and **kwargs.

Parameters
  • expected_exception (type) – the expected exception that shall be raised

  • expected_regex (regular expression) – the regular expression that shall match the stringified representation of the exception

  • callable_object=None (callable) – the callable object if not used as a context manager

  • *args – variable positional arguments passed to callable_object

  • **kwargs – variable keyword arguments passed to callable_object

Returns

a context manager if callable_object is None else None.

Raises

AssertionError – if expected_exception is not raised or does not match expected_regex

htf.assertions.assert_regex(text, expected_regex, message=None)

Assert that text matches regular expression expected_regex.

Parameters
  • text – a text to be matched

  • expected_regex – a regular expression to be used

  • message=None (str) – the message to prepend to the error message

Raises

AssertionError – if text does not match regular expression expected_regex

htf.assertions.assert_sequence_equal(a, b, message=None, sequence_type=None)

Assert that a equals b and that both are sequences.

Parameters
  • a – a sequence

  • b – a sequence

  • sequence_type=None (type) – the sequence type to be used (if not None)

  • message=None (str) – the message to prepend to the error message

Raises

AssertionError – if a and b are not equal or don’t match the sequence_type

htf.assertions.assert_set_equal(a, b, message=None)

Assert that a equals b and that both are of type set.

Parameters
  • a – a set

  • b – a set

  • message=None (str) – the message to prepend to the error message

Raises

AssertionError – if a and b are not equal or not sets

htf.assertions.assert_str_equal(a, b, message=None)

Assert that a equals b and that both are of type str.

Parameters
  • a – a string

  • b – a string

  • message=None (str) – the message to prepend to the error message

Raises

AssertionError – if a and b are not equal or not strings

htf.assertions.assert_true(expression, message=None)

Assert that expression is True.

Parameters
  • expression (bool) – the boolean expression to be validated

  • message=None (str) – the message to prepend to the error message

Raises

AssertionError – if expression is not True

htf.assertions.assert_tuple_equal(a, b, message=None)

Assert that a equals b and that both are of type tuple.

Parameters
  • a – a list

  • b – a list

  • message=None (str) – the message to prepend to the error message

Raises

AssertionError – if a and b are not equal or not tuples

htf.assertions.get_equality_asserter(type_)

Return the registered equality asserter for type_.

Parameters

type (type) – a type

Returns

the registered equality asserter for type

Raises

KeyError – if type_ has no registered equality asserter

htf.assertions.set_diff_threshold(threshold)

Set the diff threshold to threshold:

Parameters

threshold (int) – the maximum number of characters to use difflib

htf.assertions.set_equality_asserter(type_, asserter)

Register a new type equality asserter used in assert_equal and assert_not_equal.

Parameters
  • type (type) – the type the checker is used for

  • asserter (callable) – callable equality asserter object that supports parameters a, b and message

htf.assertions.set_repr_length(length)

Set the maximum representation length to length

Parameters

length (int) – the maximum representation length

For legacy tests the htf.TestCase offers wrappers for all built-in assertions and also non-pep8 wrappers to support legacy tests.