Test if two objects are equal, and print an error message if test fails. The test is performed with ``actual == desired``. Parameters ---------- test_string : str The message supplied to AssertionError. actual : object The object to test for equality agains
(test_string, actual, desired)
| 283 | |
| 284 | |
| 285 | def print_assert_equal(test_string, actual, desired): |
| 286 | """ |
| 287 | Test if two objects are equal, and print an error message if test fails. |
| 288 | |
| 289 | The test is performed with ``actual == desired``. |
| 290 | |
| 291 | Parameters |
| 292 | ---------- |
| 293 | test_string : str |
| 294 | The message supplied to AssertionError. |
| 295 | actual : object |
| 296 | The object to test for equality against `desired`. |
| 297 | desired : object |
| 298 | The expected result. |
| 299 | |
| 300 | Examples |
| 301 | -------- |
| 302 | >>> np.testing.print_assert_equal('Test XYZ of func xyz', [0, 1], [0, 1]) # doctest: +SKIP |
| 303 | >>> np.testing.print_assert_equal('Test XYZ of func xyz', [0, 1], [0, 2]) # doctest: +SKIP |
| 304 | Traceback (most recent call last): |
| 305 | ... |
| 306 | AssertionError: Test XYZ of func xyz failed |
| 307 | ACTUAL: |
| 308 | [0, 1] |
| 309 | DESIRED: |
| 310 | [0, 2] |
| 311 | |
| 312 | """ |
| 313 | __tracebackhide__ = True # Hide traceback for py.test |
| 314 | import pprint |
| 315 | |
| 316 | if not (actual == desired): |
| 317 | msg = StringIO() |
| 318 | msg.write(test_string) |
| 319 | msg.write(" failed\nACTUAL: \n") |
| 320 | pprint.pprint(actual, msg) |
| 321 | msg.write("DESIRED: \n") |
| 322 | pprint.pprint(desired, msg) |
| 323 | raise AssertionError(msg.getvalue()) |
| 324 | |
| 325 | |
| 326 | def assert_almost_equal(actual, desired, decimal=7, err_msg="", verbose=True): |