Skip to content

Latest commit

 

History

History
52 lines (40 loc) · 2.56 KB

File metadata and controls

52 lines (40 loc) · 2.56 KB

Use in testing

Although we stress that easycheck check functions are designed to be used within code, it does not mean that they cannot be used in testing. In fact, they can be quite helpful. The easycheck package offers several assert functions. They have very similar signatures to the corresponding check functions, but they work like the assert expression, meaning they are run only in the debug mode (that is, when __debug__ is True). These functions are

  • assert_if (for check_if)
  • assert_if_not (for check_if_not)
  • assert_if_in_limits (for check_if_in_limits)
  • assert_length (for check_length)
  • assert_type (for check_type)
  • assert_paths (for check_if_paths_exist)
  • assert_if_isclose (for check_if_isclose)

They use the same syntax and arguments as their easycheck counterparts, the only differences is that they work only for __debug__ = True, which is always true for tests. See:

>>> from easycheck import assert_if, assert_type, assert_length
>>> def multiply_string(string, k):
...    """Make a list consisting of string k times.
...    >>> single_string = 'aka'
...    >>> string_multiplied = multiply_string(single_string, 3)
...    >>> assert_type(string_multiplied, list)
...    >>> for i, item in enumerate(string_multiplied):
...            assert_type(item, str)
...            assert_if(item == single_string)
...    >>> assert_length(string_multiplied, 3)
...    """
...    return [string] * k

When you run doctests, all tests will run without error, as in the below simulation of the corresponding doctests from the above docstring:

>>> single_string = 'aka'
>>> string_multiplied = multiply_string(single_string, 3)
>>> assert_type(string_multiplied, list)
>>> for i, item in enumerate(string_multiplied):
...    assert_type(item, str)
...    assert_if(item == single_string)
>>> assert_length(string_multiplied, 3)

Do remember, however, that the testing functions use only AssertionError! They do not have the handle_with argument, as they are designed to work in a similar way to regular assertions, the only difference being specificity of each function.

>>> from easycheck import check_if
>>> check_if(2 < 1)
Traceback (most recent call last):
        ...
AssertionError