Assertions¶
Definition of the Asserter. Provides also a default instance that
can be easily used importing asserter.
- class Asserter¶
This class provides the implementaion of the assertion methods of the feedback helpers.
Each assertion can output either a positive or a negative feedback, depending on whether the assertion passes or fails. A default feedback maker is defined in the configuration.
You have two ways of overriding the feedback:
Provide a custom piece of feedback for each assertion using
positive_feedbackornegative_feedbackkeyword arguments;Define custom feedback makers in the
Configwhen instantiating any implementation ofAsserter.
Existence Assertions
Asserts whether a variable exists in the given set of variables.
Asserts whether a certain key exists in the given dictionary.
Asserts whether a certain key does not exist in the given dictionary.
Asserts that only certain keys are exist in the given dictionary.
Assrets that the provided module exists in a certain set of modules.
Asserts that the provided file exists in the file system.
Asserts that a given attribute exists in a certain object.
Comparison Assertions
Asserts that the two values are equal.
Asserts that the two values are not equal.
Asserts that the two floating point values are equal.
Asserts that the value is of the provided type.
Asserts that the value is not
None.Asserts that the provided sequence is of a certain length.
Asserts that the a certain string contains the provided value.
Asserts that the value is
True.Asserts that the value is
False.Other Methods
Assertion that always fails.
Writes to the configured output a message giving the user feedback that all the assertions have passed and he successfully passed the tests.
You should call this method at the end of your test routine to make sure file handles are correctly closed.
- finalize()¶
You should call this method at the end of your test routine to make sure file handles are correctly closed.
- Return type:
None
- variable_exists(varname, variables, *, positive_feedback=None, negative_feedback=None)¶
Asserts whether a variable exists in the given set of variables.
- Parameters:
varname (
str) – The name of the variable that should exist.variables (
Dict[str,Any]) – The set of variables. Usually, you want this to beglobals().positive_feedback (
Union[str,NoFeedback,None]) – Overrides the feedback the user receives if the assertion passes.negative_feedback (
Union[str,NoFeedback,None]) – Overrides the feedback the user receives if the assertion fails.
- Return type:
None
- is_set(key, dictionary, *, positive_feedback=None, negative_feedback=None)¶
Asserts whether a certain key exists in the given dictionary.
It currently only works with string-keys.
- Parameters:
key (
str) – The key that should exist in the dictionary.dictionary (
Dict[str,Any]) – The dictionary that should contain the provided key.positive_feedback (
Union[str,NoFeedback,None]) – Overrides the feedback the user receives if the assertion passes.negative_feedback (
Union[str,NoFeedback,None]) – Overrides the feedback the user receives if the assertion fails.
- Return type:
None
- is_not_set(key, dictionary, *, positive_feedback=None, negative_feedback=None)¶
Asserts whether a certain key does not exist in the given dictionary.
It currently only works with string-keys.
- Parameters:
key (
str) – The key that should not exist in th dictionary.dictionary (
Dict[str,Any]) – The dictionary that should not contain the provided key.positive_feedback (
Union[str,NoFeedback,None]) – Overrides the feedback the user receives if the assertion passes.negative_feedback (
Union[str,NoFeedback,None]) – Overrides the feedback the user receives if the assertion fails.
- Return type:
None
- only_defined_names(keys, dictionary, *, positive_feedback=None, negative_feedback=None)¶
Asserts that only certain keys are exist in the given dictionary.
It currently only works with string-keys.
Other than the keys provided, a set of keys is added to make sure that this works as expected also with
globals().- Parameters:
keys (
List[str]) – The list of the only keys that should exist in the dictionary.dictionary (
Dict[str,Any]) – The dictionary that should only contain the provided keys.positive_feedback (
Union[str,NoFeedback,None]) – Overrides the feedback the user receives if the assertion passes.negative_feedback (
Union[str,NoFeedback,None]) – Overrides the feedback the user receives if the assertion fails.
- Return type:
None
- not_equals(val, not_expected, *, positive_feedback=None, negative_feedback=None)¶
Asserts that the two values are not equal.
- Parameters:
val (
TypeVar(_T)) – The value to check.not_expected (
TypeVar(_T)) – The value thatvalshould not be equal to.positive_feedback (
Union[str,NoFeedback,None]) – Overrides the feedback the user receives if the assertion passes.negative_feedback (
Union[str,NoFeedback,None]) – Overrides the feedback the user receives if the assertion fails.
- Return type:
None
- equals(val, expected, *, positive_feedback=None, negative_feedback=None)¶
Asserts that the two values are equal.
- Parameters:
val (
TypeVar(_T)) – The value to check.expected (
TypeVar(_T)) – The expected valuevalshould be equal to.positive_feedback (
Union[str,NoFeedback,None]) – Overrides the feedback the user receives if the assertion passes.negative_feedback (
Union[str,NoFeedback,None]) – Overrides the feedback the user receives if the assertion fails.
- Return type:
None
- equals_float(val, expected, *, positive_feedback=None, negative_feedback=None)¶
Asserts that the two floating point values are equal.
It uses
math.isclosewith the default relative and absolute tolerances.- Parameters:
val (
float) – The value to check.expected (
float) – The expected valuevalshould be equal to.positive_feedback (
Union[str,NoFeedback,None]) – Overrides the feedback the user receives if the assertion passes.negative_feedback (
Union[str,NoFeedback,None]) – Overrides the feedback the user receives if the assertion fails.
- Return type:
None
- is_of_type(val, expected, *, positive_feedback=None, negative_feedback=None)¶
Asserts that the value is of the provided type.
- Parameters:
val (
Any) – The value to check.expected (
Type) – The typevalshould be.positive_feedback (
Union[str,NoFeedback,None]) – Overrides the feedback the user receives if the assertion passes.negative_feedback (
Union[str,NoFeedback,None]) – Overrides the feedback the user receives if the assertion fails.
- Return type:
None
- not_none(val, *, positive_feedback=None, negative_feedback=None)¶
Asserts that the value is not
None.- Parameters:
- Return type:
None
- has_length(val, expected_length, *, positive_feedback=None, negative_feedback=None)¶
Asserts that the provided sequence is of a certain length.
- Parameters:
val (
Sequence) – The sequence to check.expected_length (
int) – The lengthvalshould have.positive_feedback (
Union[str,NoFeedback,None]) – Overrides the feedback the user receives if the assertion passes.negative_feedback (
Union[str,NoFeedback,None]) – Overrides the feedback the user receives if the assertion fails.
- Return type:
None
- string_contains(val, expected, *, positive_feedback=None, negative_feedback=None)¶
Asserts that the a certain string contains the provided value.
- Parameters:
val (
str) – The value that should exist in the string.expected (
str) – The string that should containval.positive_feedback (
Union[str,NoFeedback,None]) – Overrides the feedback the user receives if the assertion passes.negative_feedback (
Union[str,NoFeedback,None]) – Overrides the feedback the user receives if the assertion fails.
- Return type:
None
- is_imported(module_name, modules, *, positive_feedback=None, negative_feedback=None)¶
Assrets that the provided module exists in a certain set of modules.
Usually, you want
modulesto besys.modules.- Parameters:
module_name (
str) – The module that should be imported.modules (
Dict[str,ModuleType]) – The dictionary of imported modules.positive_feedback (
Union[str,NoFeedback,None]) – Overrides the feedback the user receives if the assertion passes.negative_feedback (
Union[str,NoFeedback,None]) – Overrides the feedback the user receives if the assertion fails.
- Return type:
None
- file_exists(file_path, *, positive_feedback=None, negative_feedback=None)¶
Asserts that the provided file exists in the file system.
- Parameters:
- Return type:
None
- has_attr(attr, obj, *, positive_feedback=None, negative_feedback=None)¶
Asserts that a given attribute exists in a certain object.
- Parameters:
attr (
str) – The attribute that should exist in the object.obj (
object) – The object that should have the given attribute.positive_feedback (
Union[str,NoFeedback,None]) – Overrides the feedback the user receives if the assertion passes.negative_feedback (
Union[str,NoFeedback,None]) – Overrides the feedback the user receives if the assertion fails.
- Return type:
None
- is_true(val, *, positive_feedback=None, negative_feedback=None)¶
Asserts that the value is
True.
- is_false(val, *, positive_feedback=None, negative_feedback=None)¶
Asserts that the value is
False.
- never(feedback)¶
Assertion that always fails. Should be used in branches of execution that should not be called.
- Parameters:
feedback (
Union[str,NoFeedback]) – The feedback to provide the user in case this assertion is called.- Return type:
None
- emit_success(*, feedback=None)¶
Writes to the configured output a message giving the user feedback that all the assertions have passed and he successfully passed the tests.
The asserter is finalized and the execution terminated.
- asserter = <cg_feedback_helpers._impl.assertions.Asserter object>¶
Default asserter exported for comodity. It will use all the default config.
- pytest_asserter = <cg_feedback_helpers._impl.assertions.Asserter object>¶
Default asserter setup for use in the
Pytestblock, exported for comodity.