Raises an AssertionError if two objects are not equal. Given two objects (scalars, lists, tuples, dictionaries or numpy arrays), check that all elements of these objects are equal. An exception is raised at the first conflicting values. When one of `actual` and `desired` is a
(actual, desired, err_msg="", verbose=True)
| 130 | |
| 131 | |
| 132 | def assert_equal(actual, desired, err_msg="", verbose=True): |
| 133 | """ |
| 134 | Raises an AssertionError if two objects are not equal. |
| 135 | |
| 136 | Given two objects (scalars, lists, tuples, dictionaries or numpy arrays), |
| 137 | check that all elements of these objects are equal. An exception is raised |
| 138 | at the first conflicting values. |
| 139 | |
| 140 | When one of `actual` and `desired` is a scalar and the other is array_like, |
| 141 | the function checks that each element of the array_like object is equal to |
| 142 | the scalar. |
| 143 | |
| 144 | This function handles NaN comparisons as if NaN was a "normal" number. |
| 145 | That is, AssertionError is not raised if both objects have NaNs in the same |
| 146 | positions. This is in contrast to the IEEE standard on NaNs, which says |
| 147 | that NaN compared to anything must return False. |
| 148 | |
| 149 | Parameters |
| 150 | ---------- |
| 151 | actual : array_like |
| 152 | The object to check. |
| 153 | desired : array_like |
| 154 | The expected object. |
| 155 | err_msg : str, optional |
| 156 | The error message to be printed in case of failure. |
| 157 | verbose : bool, optional |
| 158 | If True, the conflicting values are appended to the error message. |
| 159 | |
| 160 | Raises |
| 161 | ------ |
| 162 | AssertionError |
| 163 | If actual and desired are not equal. |
| 164 | |
| 165 | Examples |
| 166 | -------- |
| 167 | >>> np.testing.assert_equal([4,5], [4,6]) |
| 168 | Traceback (most recent call last): |
| 169 | ... |
| 170 | AssertionError: |
| 171 | Items are not equal: |
| 172 | item=1 |
| 173 | ACTUAL: 5 |
| 174 | DESIRED: 6 |
| 175 | |
| 176 | The following comparison does not raise an exception. There are NaNs |
| 177 | in the inputs, but they are in the same positions. |
| 178 | |
| 179 | >>> np.testing.assert_equal(np.array([1.0, 2.0, np.nan]), [1, 2, np.nan]) |
| 180 | |
| 181 | """ |
| 182 | __tracebackhide__ = True # Hide traceback for py.test |
| 183 | |
| 184 | num_nones = sum([actual is None, desired is None]) |
| 185 | if num_nones == 1: |
| 186 | raise AssertionError(f"Not equal: {actual} != {desired}") |
| 187 | elif num_nones == 2: |
| 188 | return True |
| 189 | # else, carry on |
searching dependent graphs…