Issue
I am creating unit tests and I have a test case where the output is expected to be an empty numpy array. I have a second test case where I test the other expected array which has elements.
class myTests(unittest.TestCase):
def test_function(self):
var = 0
result = myFunction(var)
self.assertEqual(np.array([], dtype=np.int64), result)
AssertionError: array([], dtype=int64) != array([], dtype=int64)
I have a feeling it's because they are both empty but I'm not sure. How should I do this test instead?
I ran a debugger to compare the two variables and this is what I got.
result
array([], dtype=int64)
special variables
[0:0] :[]
dtype:dtype('int64')
max:'array is empty'
min:'array is empty'
shape:(0,)
size:0
np.array([], dtype=np.int64)
array([], dtype=int64)
special variables
[0:0] :[]
dtype:dtype('int64')
max:'array is empty'
min:'array is empty'
shape:(0,)
size:0
Solution
I believe it has to do with comparing equality of empty arrays.
The truth value of an empty array is ambiguous.
If you look at the result of the comparison you can see that it's not a bool value:
>>> np.array([], dtype=np.int64) == np.array([], dtype=np.int64)
array([], dtype=bool)
Have you considered checking for length of the array instead/first?
Answered By - Zla Patka
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.