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
aandbare almost equal. Useful to compare floating point numbers. Comparison is realized either using a maximumdeltaor the number of decimalplaces- Parameters:
a – number
b – number
places=None – the number of decimal places for comparison. If
None7decimal places are used.delta=None – the maximum delta
message – the message to prepend to the error message
- Raises:
AssertionError – if
aandbare not almost equal
- htf.assertions.assert_dict_equal(a: Dict, b: Dict, message: str | None = None) None
Assert that
aequalsband that both are of typedict.- Parameters:
a – a dict
b – a dict
message – the message to prepend to the error message
- Raises:
AssertionError – if
aandbare not equal
- htf.assertions.assert_equal(a: Any, b: Any, message: str | None = None) None
Assert that
aequalsb. 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
aandbare not equal
- htf.assertions.assert_false(expression: bool, message: str | None = None) None
Assert that
expressionisFalse.- Parameters:
expression – the boolean expression to be validated
message – the message to prepend to the error message
- Raises:
AssertionError – if
expressionis notFalse
- htf.assertions.assert_greater(a: Any, b: Any, message: str | None = None) None
Assert that
ais greater thanb.- Parameters:
a – a comparable object
b – a comparable object
message – the message to prepend to the error message
- Raises:
AssertionError – if
ais not greater thanb
- htf.assertions.assert_greater_equal(a: Any, b: Any, message: str | None = None) None
Assert that
ais greater or equal tob.- Parameters:
a – a comparable object
b – a comparable object
message – the message to prepend to the error message
- Raises:
AssertionError – if
ais not greater or equal tob
- htf.assertions.assert_in(member: Any, container: Iterable, message: str | None = None) None
Assert that
containercontainsmember.- Parameters:
member – the object that shall be included in
containercontainer – an iterable to search for
membermessage – the message to prepend to the error message
- Raises:
AssertionError – if
memberis not included incontainer
- htf.assertions.assert_is(a: Any, b: Any, message: str | None = None) None
Assert that
id(a)equalsid(b).- Parameters:
a – an object
b – another object
message – the message to prepend to the error message
- Raises:
AssertionError – if
id(a)does not equalid(b)
- htf.assertions.assert_is_instance(obj: Any, cls: Type, message: str | None = None) None
Assert that
objis an instance ofcls.- Parameters:
obj – an instance
cls – a class
objshall be an instance ofmessage – the message to prepend to the error message
- Raises:
AssertionError – if
objis not an instance ofcls
- htf.assertions.assert_is_none(obj: Any, message: str | None = None) None
Assert that
objisNone.- Parameters:
obj – an object
message – the message to prepend to the error message
- Raises:
AssertionError – if
objis notNone
- htf.assertions.assert_is_not(a: Any, b: Any, message: str | None = None) None
Assert that
id(a)does not equalid(b).- Parameters:
a – an object
b – another object
message – the message to prepend to the error message
- Raises:
AssertionError – if
id(a)equalsid(b)
- htf.assertions.assert_is_not_instance(obj: Any, cls: Type, message: str | None = None) None
Assert that
objis not an instance ofcls.- Parameters:
obj – an instance
cls – a class
objshall not be an instance ofmessage – the message to prepend to the error message
- Raises:
AssertionError – if
objis an instance ofcls
- htf.assertions.assert_is_not_none(obj: Any, message: str | None = None) None
Assert that
objis notNone.- Parameters:
obj – an object
message – the message to prepend to the error message
- Raises:
AssertionError – if
objisNone
- htf.assertions.assert_less(a: Any, b: Any, message: str | None = None) None
Assert that
ais less thanb.- Parameters:
a – a comparable object
b – a comparable object
message – the message to prepend to the error message
- Raises:
AssertionError – if
ais not less thanb
- htf.assertions.assert_less_equal(a: Any, b: Any, message: str | None = None) None
Assert that
ais less or equal tob.- Parameters:
a – a comparable object
b – a comparable object
message – the message to prepend to the error message
- Raises:
AssertionError – if
ais not less or equal tob
- htf.assertions.assert_list_equal(a: List, b: List, message: str | None = None) None
Assert that
aequalsband that both are of typelist.- Parameters:
a – a list
b – a list
message – the message to prepend to the error message
- Raises:
AssertionError – if
aandbare not equal or not lists
- htf.assertions.assert_multi_line_equal(a: str, b: str, message: str | None = None) None
Assert that
aequalsband that both are of typestr.- Parameters:
a – a string
b – a string
message – the message to prepend to the error message
- Raises:
AssertionError – if
aandbare 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
aandbare not almost equal. Useful to compare floating point numbers. Comparison is realized either using a maximumdeltaor the number of decimalplaces- Parameters:
a – number
b – number
places=None – the number of decimal places for comparison. If
None7decimal places are used.delta=None – the maximum delta
message – the message to prepend to the error message
- Raises:
AssertionError – if
aandbare almost equal
- htf.assertions.assert_not_equal(a: Any, b: Any, message: str | None = None) None
Assert that
adoes not equalb.- Parameters:
a – an object
b – an object
message – the message to prepend to the error message
- Raises:
AssertionError – if
aandbare equal
- htf.assertions.assert_not_in(member: Any, container: Iterable, message: str | None = None) None
Assert that
containerdoes not containmember.- Parameters:
member – the object that shall not be included in
containercontainer – an iterable to search for
membermessage – the message to prepend to the error message
- Raises:
AssertionError – if
memberis included incontainer
- htf.assertions.assert_not_is_instance(obj: Any, cls: Type, message: str | None = None) None
Assert that
objis not an instance ofcls.- Parameters:
obj – an instance
cls – a class
objshall not be an instance ofmessage – the message to prepend to the error message
- Raises:
AssertionError – if
objis an instance ofcls
- htf.assertions.assert_not_regex(text: str, unexpected_regex: str, message: str | None = None) None
Assert that
textdoes not match regular expressionunexpected_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
textmatches regular expressionunexpected_regex
- htf.assertions.assert_raises(expected_exception: Type[BaseException], callable_object: Any | None = None, *args: Any, **kwargs: Any) _AssertRaisesContext | None
Assert that
expected_exceptionis raised. Can be used as a context manager or with a callable object that is run with*argsand**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_objectisNoneelseNone.- Raises:
AssertionError – if
expected_exceptionis 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_exceptionis raised. Can be used as a context manager or with a callable object that is run with*argsand**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_objectisNoneelseNone.- Raises:
AssertionError – if
expected_exceptionis not raised or does not matchexpected_regex
- htf.assertions.assert_regex(text: str, expected_regex: str, message: str | None = None) None
Assert that
textmatches regular expressionexpected_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
textdoes not match regular expressionexpected_regex
- htf.assertions.assert_sequence_equal(a: Sequence, b: Sequence, message: str | None = None, sequence_type: Type | None = None) None
Assert that
aequalsband 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
aandbare not equal or don’t match thesequence_type
- htf.assertions.assert_set_equal(a: Set, b: Set, message: str | None = None) None
Assert that
aequalsband that both are of typeset.- Parameters:
a – a set
b – a set
message – the message to prepend to the error message
- Raises:
AssertionError – if
aandbare not equal or not sets
- htf.assertions.assert_str_equal(a: str, b: str, message: str | None = None) None
Assert that
aequalsband that both are of typestr.- Parameters:
a – a string
b – a string
message – the message to prepend to the error message
- Raises:
AssertionError – if
aandbare not equal or not strings
- htf.assertions.assert_true(expression: bool, message: str | None = None) None
Assert that
expressionisTrue.- Parameters:
expression – the boolean expression to be validated
message – the message to prepend to the error message
- Raises:
AssertionError – if
expressionis notTrue
- htf.assertions.assert_tuple_equal(a: Tuple, b: Tuple, message: str | None = None) None
Assert that
aequalsband that both are of typetuple.- Parameters:
a – a list
b – a list
message – the message to prepend to the error message
- Raises:
AssertionError – if
aandbare 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_equalandassert_not_equal.- Parameters:
type – the type the checker is used for
asserter – callable equality asserter object that supports parameters
a,bandmessage
- 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.