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: float, b: float, places: int | None = None, delta: float | None = None, message: str | None = None) 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 – number

  • b – number

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

  • delta=None – the maximum delta

  • message – the message to prepend to the error message

Raises:

AssertionError – if a and b are not almost equal

htf.assertions.assert_dict_equal(a: Dict, b: Dict, message: str | None = None) None

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

Parameters:
  • a – a dict

  • b – a dict

  • message – the message to prepend to the error message

Raises:

AssertionError – if a and b are not equal

htf.assertions.assert_equal(a: Any, b: Any, message: str | None = None) 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 – the message to prepend to the error message

Raises:

AssertionError – if a and b are not equal

htf.assertions.assert_false(expression: bool, message: str | None = None) None

Assert that expression is False.

Parameters:
  • expression – the boolean expression to be validated

  • message – the message to prepend to the error message

Raises:

AssertionError – if expression is not False

htf.assertions.assert_greater(a: Any, b: Any, message: str | None = None) None

Assert that a is greater than b.

Parameters:
  • a – a comparable object

  • b – a comparable object

  • message – the message to prepend to the error message

Raises:

AssertionError – if a is not greater than b

htf.assertions.assert_greater_equal(a: Any, b: Any, message: str | None = None) None

Assert that a is greater or equal to b.

Parameters:
  • a – a comparable object

  • b – a comparable object

  • message – the message to prepend to the error message

Raises:

AssertionError – if a is not greater or equal to b

htf.assertions.assert_in(member: Any, container: Iterable, message: str | None = None) None

Assert that container contains member.

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

  • container – an iterable to search for member

  • message – the message to prepend to the error message

Raises:

AssertionError – if member is not included in container

htf.assertions.assert_is(a: Any, b: Any, message: str | None = None) None

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

Parameters:
  • a – an object

  • b – another object

  • message – the message to prepend to the error message

Raises:

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

htf.assertions.assert_is_instance(obj: Any, cls: Type, message: str | None = None) None

Assert that obj is an instance of cls.

Parameters:
  • obj – an instance

  • cls – a class obj shall be an instance of

  • message – the message to prepend to the error message

Raises:

AssertionError – if obj is not an instance of cls

htf.assertions.assert_is_none(obj: Any, message: str | None = None) None

Assert that obj is None.

Parameters:
  • obj – an object

  • message – the message to prepend to the error message

Raises:

AssertionError – if obj is not None

htf.assertions.assert_is_not(a: Any, b: Any, message: str | None = None) None

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

Parameters:
  • a – an object

  • b – another object

  • message – the message to prepend to the error message

Raises:

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

htf.assertions.assert_is_not_instance(obj: Any, cls: Type, message: str | None = None) 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 – the message to prepend to the error message

Raises:

AssertionError – if obj is an instance of cls

htf.assertions.assert_is_not_none(obj: Any, message: str | None = None) None

Assert that obj is not None.

Parameters:
  • obj – an object

  • message – the message to prepend to the error message

Raises:

AssertionError – if obj is None

htf.assertions.assert_less(a: Any, b: Any, message: str | None = None) None

Assert that a is less than b.

Parameters:
  • a – a comparable object

  • b – a comparable object

  • message – the message to prepend to the error message

Raises:

AssertionError – if a is not less than b

htf.assertions.assert_less_equal(a: Any, b: Any, message: str | None = None) None

Assert that a is less or equal to b.

Parameters:
  • a – a comparable object

  • b – a comparable object

  • message – 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: List, b: List, message: str | None = None) None

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

Parameters:
  • a – a list

  • b – a list

  • message – 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: str, b: str, message: str | None = None) None

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

Parameters:
  • a – a string

  • b – a string

  • message – 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: float, b: float, places: int | None = None, delta: float | None = None, message: str | None = None) 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 – number

  • b – number

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

  • delta=None – the maximum delta

  • message – the message to prepend to the error message

Raises:

AssertionError – if a and b are almost equal

htf.assertions.assert_not_equal(a: Any, b: Any, message: str | None = None) None

Assert that a does not equal b.

Parameters:
  • a – an object

  • b – an object

  • message – the message to prepend to the error message

Raises:

AssertionError – if a and b are equal

htf.assertions.assert_not_in(member: Any, container: Iterable, message: str | None = None) None

Assert that container does not contain member.

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

  • container – an iterable to search for member

  • message – the message to prepend to the error message

Raises:

AssertionError – if member is included in container

htf.assertions.assert_not_is_instance(obj: Any, cls: Type, message: str | None = None) 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 – the message to prepend to the error message

Raises:

AssertionError – if obj is an instance of cls

htf.assertions.assert_not_regex(text: str, unexpected_regex: str, message: str | None = None) 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 – the message to prepend to the error message

Raises:

AssertionError – if text matches regular expression unexpected_regex

htf.assertions.assert_raises(expected_exception: Type[BaseException], callable_object: Any | None = None, *args: Any, **kwargs: Any) _AssertRaisesContext | None

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 – the expected exception that shall be raised

  • callable_object=None – 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: Type[BaseException], expected_regex: str, callable_object: Any | None = None, *args: Any, **kwargs: Any) _AssertRaisesContext | None

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 – the expected exception that shall be raised

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

  • callable_object=None – 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: str, expected_regex: str, message: str | None = None) 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 – 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: Sequence, b: Sequence, message: str | None = None, sequence_type: Type | None = None) None

Assert that a equals b and that both are sequences.

Parameters:
  • a – a sequence

  • b – a sequence

  • message – the message to prepend to the error message

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

Raises:

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

htf.assertions.assert_set_equal(a: Set, b: Set, message: str | None = None) None

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

Parameters:
  • a – a set

  • b – a set

  • message – 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: str, b: str, message: str | None = None) None

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

Parameters:
  • a – a string

  • b – a string

  • message – 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: bool, message: str | None = None) None

Assert that expression is True.

Parameters:
  • expression – the boolean expression to be validated

  • message – the message to prepend to the error message

Raises:

AssertionError – if expression is not True

htf.assertions.assert_tuple_equal(a: Tuple, b: Tuple, message: str | None = None) None

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

Parameters:
  • a – a list

  • b – a list

  • message – 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_: Type) Callable[[...], None]

Return the registered equality asserter for type_.

Parameters:

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: int) None

Set the diff threshold to threshold:

Parameters:

threshold – the maximum number of characters to use difflib

htf.assertions.set_equality_asserter(type_: Type, asserter: Callable[[...], None]) None

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

Parameters:
  • type – the type the checker is used for

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

htf.assertions.set_repr_length(length: int) None

Set the maximum representation length to length

Parameters:

length – 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.