Testing Helpers#

For better unit testing, we implemented (and imported), a lot of testing helpers for the most common checkings. These tests encloses some assert, so in a single function we can decide the best way to do this in the future.

Also, this open the doors to, in a future release, perform a non catastrofic failure checking, meaning that the test will continue even with failure and, in the end, all the failures are presented at once.

They are simple functions that will raise AssertionError if the condition they are designed is not satisfied. Like:

In [1]: from astropop.testing import assert_true, assert_equal

In [2]: assert_true(True, 'true is true, so not raised')

In [3]: assert_true(1 == 2, 'one is not equal to two')
---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
Cell In[3], line 1
----> 1 assert_true(1 == 2, 'one is not equal to two')

File ~/checkouts/readthedocs.org/user_builds/astropop/envs/latest/lib/python3.11/site-packages/astropop/testing.py:51, in assert_true(a, msg)
     49 if not a:
     50     msg = msg or f"{a} is not True."
---> 51     raise AssertionError(msg)

AssertionError: one is not equal to two
In [4]: assert_equal(1, 1, 'one is equal to one, so not raised')

In [5]: assert_equal(1, 2, 'one is not equal to two')
---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
Cell In[5], line 1
----> 1 assert_equal(1, 2, 'one is not equal to two')

File ~/checkouts/readthedocs.org/user_builds/astropop/envs/latest/lib/python3.11/site-packages/astropop/testing.py:72, in assert_equal(a, b, msg)
     70 if not a == b:
     71     msg = msg or f"{a} is not equal to {b}."
---> 72     raise AssertionError(msg)

AssertionError: one is not equal to two

For all the functions, check the API.

Testing Helpers API#

astropop.testing Module#

Collection of testing helpers.

Notes#

  • Based on pytest_check. However, we are removing the dependency.

  • Lots of functions imported directly from numpy.testing

Functions#

assert_equal(a, b[, msg])

Check if two objects are equal.

assert_not_equal(a, b[, msg])

Raise assertion error if values are equal.

assert_almost_equal(a, b[, decimal, msg])

Check if two objects are almost equal.

assert_true(a[, msg])

Raise assertion error if a is not True.

assert_false(a[, msg])

Raise assertion error if condition is not False.

assert_is(a, b[, msg])

Raise assertion error if a is b.

assert_is_not(a, b[, msg])

Raise assertion error if a is not b.

assert_is_none(a[, msg])

Raise assertion error if a is not None.

assert_is_not_none(a[, msg])

Raise assertion error if a is None.

assert_in(a, b[, msg])

Raise assertion error if a not in b.

assert_not_in(a, b[, msg])

Raise assertion error if a in b.

assert_is_instance(a, b[, msg])

Raise assertion error if not isinstance(a, b).

assert_is_not_instance(a, b[, msg])

Raise assertion error if isinstance(a, b).

assert_greater(a, b[, msg])

Raise assertion error if a is not greater then b.

assert_greater_equal(a, b[, msg])

Raise assertion error if a is not greater or equal then b.

assert_less(a, b[, msg])

Raise assertion error if a is not less b.

assert_less_equal(a, b[, msg])

Raise assertion error if a is not less or equal then b.

assert_raises(assert_raises)

Fail unless an exception of class exception_class is thrown by callable when invoked with arguments args and keyword arguments kwargs.

assert_raises_regex(exception_class, ...)

Fail unless an exception of class exception_class and with message that matches expected_regexp is thrown by callable when invoked with arguments args and keyword arguments kwargs.

assert_warns(warning_class, *args, **kwargs)

Fail unless the given callable throws the specified warning.

assert_not_warnings(*args, **kwargs)

Fail if the given callable produces any warnings.

assert_path_exists(a[, msg])

Raise assertion error if path a do not exists.

assert_path_not_exists(a[, msg])

Raise assertion error if path a do exists.