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:

>>> from astropop.testing import assert_true, assert_equal
>>> assert_true(True, 'true is true, so not raised')
>>> assert_true(1 == 2, 'one is not equal to two')   
AssertionError: one is not equal to two
>>> assert_equal(1, 1, 'one is equal to one, so not raised')
>>> assert_equal(1, 2, 'one is not equal to two')   
AssertionError: one is not equal to two

For all the functions, check the API.

Testing Helpers API

astropop.testing.assert_true(a, msg=None)

Raise assertion error if a is not True.

astropop.testing.assert_false(a, msg=None)

Raise assertion error if condition is not False.

astropop.testing.assert_equal(a, b, msg=None)

Check if two objects are equal. Arrays supported.

astropop.testing.assert_not_equal(a, b, msg=None)

Raise assertion error if values are equal.

astropop.testing.assert_almost_equal(a, b, decimal=6, msg=None)

Check if two objects are almost equal. Arrays supported.

astropop.testing.assert_is(a, b, msg=None)

Raise assertion error if a is b.

astropop.testing.assert_is_not(a, b, msg=None)

Raise assertion error if a is not b.

astropop.testing.assert_is_instance(a, b, msg=None)

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

astropop.testing.assert_is_not_instance(a, b, msg=None)

Raise assertion error if isinstance(a, b).

astropop.testing.assert_in(a, b, msg=None)

Raise assertion error if a not in b.

astropop.testing.assert_not_in(a, b, msg=None)

Raise assertion error if a in b.

astropop.testing.assert_greater(a, b, msg=None)

Raise assertion error if a is not greater then b.

astropop.testing.assert_greater_equal(a, b, msg=None)

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

astropop.testing.assert_less(a, b, msg=None)

Raise assertion error if a is not less b.

astropop.testing.assert_less_equal(a, b, msg=None)

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

astropop.testing.assert_raises(exception_class, callable, *args, **kwargs)
astropop.testing.assert_raises(exception_class) None

Fail unless an exception of class exception_class is thrown by callable when invoked with arguments args and keyword arguments kwargs. If a different type of exception is thrown, it will not be caught, and the test case will be deemed to have suffered an error, exactly as for an unexpected exception.

Alternatively, assert_raises can be used as a context manager:

>>> from numpy.testing import assert_raises
>>> with assert_raises(ZeroDivisionError):
...     1 / 0

is equivalent to

>>> def div(x, y):
...     return x / y
>>> assert_raises(ZeroDivisionError, div, 1, 0)
astropop.testing.assert_raises_regex(exception_class, expected_regexp, *args, **kwargs)
assert_raises_regex(exception_class, expected_regexp, callable, *args,

**kwargs)

assert_raises_regex(exception_class, expected_regexp)

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.

Alternatively, can be used as a context manager like assert_raises.

Notes

New in version 1.9.0.

astropop.testing.assert_warns(warning_class, *args, **kwargs)

Fail unless the given callable throws the specified warning.

A warning of class warning_class should be thrown by the callable when invoked with arguments args and keyword arguments kwargs. If a different type of warning is thrown, it will not be caught.

If called with all arguments other than the warning class omitted, may be used as a context manager:

with assert_warns(SomeWarning):

do_something()

The ability to be used as a context manager is new in NumPy v1.11.0.

New in version 1.4.0.

Parameters
  • warning_class (class) – The class defining the warning that func is expected to throw.

  • func (callable, optional) – Callable to test

  • *args (Arguments) – Arguments for func.

  • **kwargs (Kwargs) – Keyword arguments for func.

Returns

The value returned by func.

Examples

>>> import warnings
>>> def deprecated_func(num):
...     warnings.warn("Please upgrade", DeprecationWarning)
...     return num*num
>>> with np.testing.assert_warns(DeprecationWarning):
...     assert deprecated_func(4) == 16
>>> # or passing a func
>>> ret = np.testing.assert_warns(DeprecationWarning, deprecated_func, 4)
>>> assert ret == 16