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)
| 469 | |
| 470 | |
| 471 | def print_assert_equal(test_string, actual, desired): |
| 472 | """ |
| 473 | Test if two objects are equal, and print an error message if test fails. |
| 474 | |
| 475 | The test is performed with ``actual == desired``. |
| 476 | |
| 477 | Parameters |
| 478 | ---------- |
| 479 | test_string : str |
| 480 | The message supplied to AssertionError. |
| 481 | actual : object |
| 482 | The object to test for equality against `desired`. |
| 483 | desired : object |
| 484 | The expected result. |
| 485 | |
| 486 | Examples |
| 487 | -------- |
| 488 | >>> np.testing.print_assert_equal('Test XYZ of func xyz', [0, 1], [0, 1]) |
| 489 | >>> np.testing.print_assert_equal('Test XYZ of func xyz', [0, 1], [0, 2]) |
| 490 | Traceback (most recent call last): |
| 491 | ... |
| 492 | AssertionError: Test XYZ of func xyz failed |
| 493 | ACTUAL: |
| 494 | [0, 1] |
| 495 | DESIRED: |
| 496 | [0, 2] |
| 497 | |
| 498 | """ |
| 499 | __tracebackhide__ = True # Hide traceback for py.test |
| 500 | import pprint |
| 501 | |
| 502 | if not (actual == desired): |
| 503 | msg = StringIO() |
| 504 | msg.write(test_string) |
| 505 | msg.write(' failed\nACTUAL: \n') |
| 506 | pprint.pprint(actual, msg) |
| 507 | msg.write('DESIRED: \n') |
| 508 | pprint.pprint(desired, msg) |
| 509 | raise AssertionError(msg.getvalue()) |
| 510 | |
| 511 | |
| 512 | def assert_almost_equal(actual, desired, decimal=7, err_msg='', verbose=True): |