2

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
1
  • 1
    I believe there's a numpy testing module or utility that has 'assertions' that are suitable for testing arrays. As you see here, the regular Python assertions are not numpy aware. Commented Aug 30, 2021 at 20:23

2 Answers 2

2

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?

Sign up to request clarification or add additional context in comments.

1 Comment

Good point with the length instead. That solves the problem. Thank you.
0

If you're using Python3 and care about the ordering of contents you can use assertSequenceEqual()

1 Comment

I tried this but got a similar error AssertionError: Sequences differ: array([], dtype=float64) != array([], dtype=float64)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.