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_feedback or negative_feedback keyword arguments;

  • Define custom feedback makers in the Config when instantiating any implementation of Asserter.

Parameters:

config (Config) – The configuration overrides. Check Config.

Existence Assertions

variable_exists

Asserts whether a variable exists in the given set of variables.

is_set

Asserts whether a certain key exists in the given dictionary.

is_not_set

Asserts whether a certain key does not exist in the given dictionary.

only_defined_names

Asserts that only certain keys are exist in the given dictionary.

is_imported

Assrets that the provided module exists in a certain set of modules.

file_exists

Asserts that the provided file exists in the file system.

has_attr

Asserts that a given attribute exists in a certain object.

Comparison Assertions

equals

Asserts that the two values are equal.

not_equals

Asserts that the two values are not equal.

equals_float

Asserts that the two floating point values are equal.

is_of_type

Asserts that the value is of the provided type.

not_none

Asserts that the value is not None.

has_length

Asserts that the provided sequence is of a certain length.

string_contains

Asserts that the a certain string contains the provided value.

is_true

Asserts that the value is True.

is_false

Asserts that the value is False.

Other Methods

never

Assertion that always fails.

emit_success

Writes to the configured output a message giving the user feedback that all the assertions have passed and he successfully passed the tests.

finalize

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 be globals().

  • 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 that val should 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 value val should 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.isclose with the default relative and absolute tolerances.

Parameters:
  • val (float) – The value to check.

  • expected (float) – The expected value val should 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 type val should 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:
  • val (object) – The value that should not be None.

  • 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

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 length val should 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 contain val.

  • 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 modules to be sys.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:
  • file_path (str | PathLike[str]) – The file that should exist.

  • 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

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.

Parameters:
  • val (bool) – The value to check.

  • 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_false(val, *, positive_feedback=None, negative_feedback=None)

Asserts that the value is False.

Parameters:
  • val (bool) – The value to check.

  • 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

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.

Parameters:

feedback (Union[str, NoFeedback, None]) – Overrides the default feedback displayed to the user.

Return type:

NoReturn

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 Pytest block, exported for comodity.