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
andb
are almost equal. Useful to compare floating point numbers. Comparison is realized either using a maximumdelta
or the number of decimalplaces
- 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
andb
are not almost equal
- htf.assertions.assert_dict_equal(a: Dict, b: Dict, message: str | None = None) None
Assert that
a
equalsb
and that both are of typedict
.- Parameters:
a – a dict
b – a dict
message – the message to prepend to the error message
- Raises:
AssertionError – if
a
andb
are not equal
- htf.assertions.assert_equal(a: Any, b: Any, message: str | None = None) None
Assert that
a
equalsb
. 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
andb
are not equal
- htf.assertions.assert_false(expression: bool, message: str | None = None) None
Assert that
expression
isFalse
.- Parameters:
expression – the boolean expression to be validated
message – the message to prepend to the error message
- Raises:
AssertionError – if
expression
is notFalse
- htf.assertions.assert_greater(a: Any, b: Any, message: str | None = None) None
Assert that
a
is greater thanb
.- 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 thanb
- htf.assertions.assert_greater_equal(a: Any, b: Any, message: str | None = None) None
Assert that
a
is 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
a
is not greater or equal tob
- htf.assertions.assert_in(member: Any, container: Iterable, message: str | None = None) None
Assert that
container
containsmember
.- 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 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
obj
is an instance ofcls
.- Parameters:
obj – an instance
cls – a class
obj
shall be an instance ofmessage – the message to prepend to the error message
- Raises:
AssertionError – if
obj
is not an instance ofcls
- htf.assertions.assert_is_none(obj: Any, message: str | None = None) None
Assert that
obj
isNone
.- Parameters:
obj – an object
message – the message to prepend to the error message
- Raises:
AssertionError – if
obj
is 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
obj
is not an instance ofcls
.- Parameters:
obj – an instance
cls – a class
obj
shall not be an instance ofmessage – the message to prepend to the error message
- Raises:
AssertionError – if
obj
is an instance ofcls
- htf.assertions.assert_is_not_none(obj: Any, message: str | None = None) None
Assert that
obj
is notNone
.- Parameters:
obj – an object
message – the message to prepend to the error message
- Raises:
AssertionError – if
obj
isNone
- htf.assertions.assert_less(a: Any, b: Any, message: str | None = None) None
Assert that
a
is less thanb
.- 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 thanb
- htf.assertions.assert_less_equal(a: Any, b: Any, message: str | None = None) None
Assert that
a
is 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
a
is not less or equal tob
- htf.assertions.assert_list_equal(a: List, b: List, message: str | None = None) None
Assert that
a
equalsb
and that both are of typelist
.- Parameters:
a – a list
b – a list
message – the message to prepend to the error message
- Raises:
AssertionError – if
a
andb
are not equal or not lists
- htf.assertions.assert_multi_line_equal(a: str, b: str, message: str | None = None) None
Assert that
a
equalsb
and that both are of typestr
.- Parameters:
a – a string
b – a string
message – the message to prepend to the error message
- Raises:
AssertionError – if
a
andb
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
andb
are not almost equal. Useful to compare floating point numbers. Comparison is realized either using a maximumdelta
or the number of decimalplaces
- 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
andb
are almost equal
- htf.assertions.assert_not_equal(a: Any, b: Any, message: str | None = None) None
Assert that
a
does not equalb
.- Parameters:
a – an object
b – an object
message – the message to prepend to the error message
- Raises:
AssertionError – if
a
andb
are equal
- htf.assertions.assert_not_in(member: Any, container: Iterable, message: str | None = None) None
Assert that
container
does not containmember
.- 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 incontainer
- htf.assertions.assert_not_is_instance(obj: Any, cls: Type, message: str | None = None) None
Assert that
obj
is not an instance ofcls
.- Parameters:
obj – an instance
cls – a class
obj
shall not be an instance ofmessage – the message to prepend to the error message
- Raises:
AssertionError – if
obj
is an instance ofcls
- htf.assertions.assert_not_regex(text: str, unexpected_regex: str, message: str | None = None) None
Assert that
text
does 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
text
matches 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_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
isNone
elseNone
.- 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
isNone
elseNone
.- Raises:
AssertionError – if
expected_exception
is not raised or does not matchexpected_regex
- htf.assertions.assert_regex(text: str, expected_regex: str, message: str | None = None) None
Assert that
text
matches 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
text
does 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
a
equalsb
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
andb
are not equal or don’t match thesequence_type
- htf.assertions.assert_set_equal(a: Set, b: Set, message: str | None = None) None
Assert that
a
equalsb
and that both are of typeset
.- Parameters:
a – a set
b – a set
message – the message to prepend to the error message
- Raises:
AssertionError – if
a
andb
are not equal or not sets
- htf.assertions.assert_str_equal(a: str, b: str, message: str | None = None) None
Assert that
a
equalsb
and that both are of typestr
.- Parameters:
a – a string
b – a string
message – the message to prepend to the error message
- Raises:
AssertionError – if
a
andb
are not equal or not strings
- htf.assertions.assert_true(expression: bool, message: str | None = None) None
Assert that
expression
isTrue
.- Parameters:
expression – the boolean expression to be validated
message – the message to prepend to the error message
- Raises:
AssertionError – if
expression
is notTrue
- htf.assertions.assert_tuple_equal(a: Tuple, b: Tuple, message: str | None = None) None
Assert that
a
equalsb
and that both are of typetuple
.- Parameters:
a – a list
b – a list
message – the message to prepend to the error message
- Raises:
AssertionError – if
a
andb
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
andassert_not_equal
.- Parameters:
type – the type the checker is used for
asserter – callable equality asserter object that supports parameters
a
,b
andmessage
- 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.