Test if DeprecationWarnings are given and raised. This first checks if the function when called gives `num` DeprecationWarnings, after that it tries to raise these DeprecationWarnings and compares them with `exceptions`. The exceptions can be different for cases wher
(self, function, num=1, ignore_others=False,
function_fails=False,
exceptions=np._NoValue,
args=(), kwargs={})
| 50 | self.warn_ctx.__exit__() |
| 51 | |
| 52 | def assert_deprecated(self, function, num=1, ignore_others=False, |
| 53 | function_fails=False, |
| 54 | exceptions=np._NoValue, |
| 55 | args=(), kwargs={}): |
| 56 | """Test if DeprecationWarnings are given and raised. |
| 57 | |
| 58 | This first checks if the function when called gives `num` |
| 59 | DeprecationWarnings, after that it tries to raise these |
| 60 | DeprecationWarnings and compares them with `exceptions`. |
| 61 | The exceptions can be different for cases where this code path |
| 62 | is simply not anticipated and the exception is replaced. |
| 63 | |
| 64 | Parameters |
| 65 | ---------- |
| 66 | function : callable |
| 67 | The function to test |
| 68 | num : int |
| 69 | Number of DeprecationWarnings to expect. This should normally be 1. |
| 70 | ignore_others : bool |
| 71 | Whether warnings of the wrong type should be ignored (note that |
| 72 | the message is not checked) |
| 73 | function_fails : bool |
| 74 | If the function would normally fail, setting this will check for |
| 75 | warnings inside a try/except block. |
| 76 | exceptions : Exception or tuple of Exceptions |
| 77 | Exception to expect when turning the warnings into an error. |
| 78 | The default checks for DeprecationWarnings. If exceptions is |
| 79 | empty the function is expected to run successfully. |
| 80 | args : tuple |
| 81 | Arguments for `function` |
| 82 | kwargs : dict |
| 83 | Keyword arguments for `function` |
| 84 | """ |
| 85 | __tracebackhide__ = True # Hide traceback for py.test |
| 86 | |
| 87 | # reset the log |
| 88 | self.log[:] = [] |
| 89 | |
| 90 | if exceptions is np._NoValue: |
| 91 | exceptions = (self.warning_cls,) |
| 92 | |
| 93 | try: |
| 94 | function(*args, **kwargs) |
| 95 | except (Exception if function_fails else tuple()): |
| 96 | pass |
| 97 | |
| 98 | # just in case, clear the registry |
| 99 | num_found = 0 |
| 100 | for warning in self.log: |
| 101 | if warning.category is self.warning_cls: |
| 102 | num_found += 1 |
| 103 | elif not ignore_others: |
| 104 | raise AssertionError( |
| 105 | "expected %s but got: %s" % |
| 106 | (self.warning_cls.__name__, warning.category)) |
| 107 | if num is not None and num_found != num: |
| 108 | msg = "%i warnings found but %i expected." % (len(self.log), num) |
| 109 | lst = [str(w) for w in self.log] |
no test coverage detected