assertAlmostEqual raise confusing error message when test fails
Hi @dongyaoli10x, kindly let us know the OS Platform and Distribution used to exceute the above code. I tried executing it in Ubuntu 22.04. Please find the output below.
![image](https://user-images.githubusercontent.com/98147397/221192831-3da0fff3-e0a1-4a59-ace6-d9e739130167.png)
Thank you! > Hi @dongyaoli10x, kindly let us know the OS Platform and Distribution used to exceute the above code. I tried executing it in Ubuntu 22.04. Please find the output below. ![image](https://user-images.githubusercontent.com/98147397/221192831-3da0fff3-e0a1-4a59-ace6-d9e739130167.png) Thank you!

Thank you @synandi for your quick response! The OS is centos 7.9.2009. But it looks like the error message you got with ubuntu 22.04 is the same Thank you for sharing the details. I replicated the issue in CentOS. Please find the complete error below.
```
Running tests under Python 3.9.16: /home/ynandi/miniconda3/envs/tf/bin/python
[ RUN      ] TestDummy.test_dummy_confusing_bug
2023-02-27 11:35:49.001558: I tensorflow/core/platform/cpu_feature_guard.cc:193] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations:  AVX2 FMA
To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
INFO:tensorflow:time(__main__.TestDummy.test_dummy_confusing_bug): 0.0s
I0227 11:35:49.005805 140260578801472 test_util.py:2458] time(__main__.TestDummy.test_dummy_confusing_bug): 0.0s
[  FAILED  ] TestDummy.test_dummy_confusing_bug
[ RUN      ] TestDummy.test_dummy_confusing_error
INFO:tensorflow:time(__main__.TestDummy.test_dummy_confusing_error): 0.0s
I0227 11:35:49.007651 140260578801472 test_util.py:2458] time(__main__.TestDummy.test_dummy_confusing_error): 0.0s
[  FAILED  ] TestDummy.test_dummy_confusing_error
[ RUN      ] TestDummy.test_dummy_no_error
INFO:tensorflow:time(__main__.TestDummy.test_dummy_no_error): 0.0s
I0227 11:35:49.008224 140260578801472 test_util.py:2458] time(__main__.TestDummy.test_dummy_no_error): 0.0s
[       OK ] TestDummy.test_dummy_no_error
[ RUN      ] TestDummy.test_session
[  SKIPPED ] TestDummy.test_session
======================================================================
ERROR: test_dummy_confusing_bug (__main__.TestDummy)
TestDummy.test_dummy_confusing_bug
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/ynandi/59789.py", line 15, in test_dummy_confusing_bug
    self.assertAlmostEqual(a, b, places=2)
  File "/home/ynandi/miniconda3/envs/tf/lib/python3.9/unittest/case.py", line 882, in assertAlmostEqual
    if round(diff, places) == 0:
  File "/home/ynandi/miniconda3/envs/tf/lib/python3.9/site-packages/tensorflow/python/ops/numpy_ops/np_array_ops.py", line 733, in around
    return a.astype(dtype)
  File "/home/ynandi/miniconda3/envs/tf/lib/python3.9/site-packages/tensorflow/python/framework/ops.py", line 440, in __getattr__
    raise AttributeError(
AttributeError: EagerTensor object has no attribute 'astype'. 
        If you are looking for numpy-related methods, please run the following:
        from tensorflow.python.ops.numpy_ops import np_config
        np_config.enable_numpy_behavior()
      

======================================================================
ERROR: test_dummy_confusing_error (__main__.TestDummy)
TestDummy.test_dummy_confusing_error
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/ynandi/59789.py", line 9, in test_dummy_confusing_error
    self.assertAlmostEqual(a, b)
  File "/home/ynandi/miniconda3/envs/tf/lib/python3.9/unittest/case.py", line 882, in assertAlmostEqual
    if round(diff, places) == 0:
  File "/home/ynandi/miniconda3/envs/tf/lib/python3.9/site-packages/tensorflow/python/ops/numpy_ops/np_array_ops.py", line 733, in around
    return a.astype(dtype)
  File "/home/ynandi/miniconda3/envs/tf/lib/python3.9/site-packages/tensorflow/python/framework/ops.py", line 440, in __getattr__
    raise AttributeError(
AttributeError: EagerTensor object has no attribute 'astype'. 
        If you are looking for numpy-related methods, please run the following:
        from tensorflow.python.ops.numpy_ops import np_config
        np_config.enable_numpy_behavior()
      

----------------------------------------------------------------------
Ran 4 tests in 0.008s

FAILED (errors=2, skipped=1)
```

- The first test case `test_dummy_confusing_bug` fails with an error message 
```
AttributeError: EagerTensor object has no attribute 'astype'.
```
This error occurs because the `self.assertAlmostEqual` function is not able to perform the numpy operation 'astype' on the EagerTensor objects a and b.
- The second test case `test_dummy_confusing_error` also fails with the same error message as the first test case. To fix the issue, as a workaround you can modify the assertion to compare the numpy values of the tensors instead of the tensors themselves. Here's the modified code:
```
import tensorflow as tf

class TestDummy(tf.test.TestCase):

    def test_dummy_confusing_error(self):
        # this test should fail. It fails as expected.
        a = tf.constant(1.00002, dtype=tf.float32)
        b = tf.constant(1.00001, dtype=tf.float32)
        self.assertAlmostEqual(a.numpy(), b.numpy())

    def test_dummy_confusing_bug(self):
        # this test passes with places=2.
        a = tf.constant(1.00002, dtype=tf.float32)
        b = tf.constant(1.00001, dtype=tf.float32)
        self.assertAlmostEqual(a.numpy(), b.numpy(), places=2)

    def test_dummy_no_error(self):
        # this test passes 
        a = tf.constant(1.000000002, dtype=tf.float32)
        b = tf.constant(1.000000001, dtype=tf.float32)
        self.assertAlmostEqual(a.numpy(), b.numpy())

if __name__ == "__main__":
    tf.test.main()
```
In these modified tests, the `numpy()` method is called on the tensors a and b to obtain their numpy values, and the `assertAlmostEqual()` method is used to compare the numpy values instead of the tensors themselves.

Thank you!

 This issue is stale because it has been open for 7 days with no activity. It will be closed if no further activity occurs. Thank you. This issue was closed because it has been inactive for 7 days since being marked as stale. Please reopen if you'd like to work on this further. Are you satisfied with the resolution of your issue?
<a href="https://docs.google.com/forms/d/e/1FAIpQLSfaP12TRhd9xSxjXZjcZFNXPGk4kc1-qMdv3gc6bEP90vY1ew/viewform?entry.85265664=Yes&entry.2137816233=https://github.com/tensorflow/tensorflow/issues/59789">Yes</a>
<a href="https://docs.google.com/forms/d/e/1FAIpQLSfaP12TRhd9xSxjXZjcZFNXPGk4kc1-qMdv3gc6bEP90vY1ew/viewform?entry.85265664=No&entry.2137816233=https://github.com/tensorflow/tensorflow/issues/59789">No</a>

<details><summary>Click to expand!</summary> 
 
 ### Issue Type

Bug

### Have you reproduced the bug with TF nightly?

No

### Source

source

### Tensorflow Version

tf 2.9

### Custom Code

Yes

### OS Platform and Distribution

_No response_

### Mobile device

_No response_

### Python version

3.9

### Bazel version

_No response_

### GCC/Compiler version

_No response_
</details>

### Current Behaviour?

I seem to observe to issues with `assertAlmostEqual` of `tf.test.TestCase`
1. When using `assertAlmostEqual` and when it fails, the error message is confusing. Instead of saying the test fails, it shows the following error message:

```python

File "...", line ..., in test_dummy
    self.assertAlmostEqual(a, b)
File ".../python3.9/unittest/case.py", line 874, in assertAlmostEqual
    if round(diff, places) == 0:
File ".../tensorflow/python/ops/numpy_ops/np_array_ops.py", line 733, in around
    return a.astype(dtype)
File ".../tensorflow/python/framework/ops.py", line 440, in __getattr__
    raise AttributeError(

AttributeError: EagerTensor object has no attribute 'astype'.
        If you are looking for numpy-related methods, please run the following:
        from tensorflow.python.ops.numpy_ops import np_config
        np_config.enable_numpy_behavior()
```
2. when I set the `places` to a smaller value that I think should allow the test to pass, the test still fails with the above error message.

When the difference is indeed smaller than the default `places=7`, the test passes.


### Standalone code to reproduce the issue

```python
import tensorflow as tf

class TestDummy(tf.test.TestCase):

    def test_dummy_confusing_error(self):
        # this test should fail. But it raises the above confusing error message.
        a = tf.constant(1.00002, dtype=tf.float32)
        b = tf.constant(1.00001, dtype=tf.float32)
        self.assertAlmostEqual(a, b)
    
    def test_dummy_confusing_bug(self):
        # this test should pass with places=2. But it still raises the above confusing error message.
        a = tf.constant(1.00002, dtype=tf.float32)
        b = tf.constant(1.00001, dtype=tf.float32)
        self.assertAlmostEqual(a, b, places=2)

    def test_dummy_no_error(self):
        # this test passes 
        a = tf.constant(1.000000002, dtype=tf.float32)
        b = tf.constant(1.000000001, dtype=tf.float32)
        self.assertAlmostEqual(a, b)

if __name__ == "__main__":
    tf.test.main()
```