160

I want to make some unit-tests for my app, and I need to compare two arrays. Since array.__eq__ returns a new array (so TestCase.assertEqual fails), what is the best way to assert for equality?

Currently I'm using

self.assertTrue((arr1 == arr2).all())

but I don't really like it

2
  • 4
    note that your example can yield True unexpectedly, e.g. (np.array([1, 1]) == np.array([1])).all() will yield True Commented Apr 27, 2020 at 9:01
  • 3
    self.assertTrue(np.array_equal(array1, array2)) Commented Jul 15, 2020 at 13:36

9 Answers 9

172

check out the assert functions in numpy.testing, e.g.

assert_array_equal

for floating point arrays equality test might fail and assert_almost_equal is more reliable.

update

A few versions ago numpy obtained assert_allclose which is now my favorite since it allows us to specify both absolute and relative error and doesn't require decimal rounding as the closeness criterion.

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

6 Comments

How does this interact with unittest? I think that some words on the matter would be useful.
I never use unittest. However, it works very well with nosetests which are used by numpy, scipy and statsmodels. Just use the asserts inside a test function or method.
This doesn't verify that the two arguments are both numpy arrays. For example, it would succeed on an array and a list. For testing, it might be useful to verify that these are actually arrays, but I guess it would require manually checking the type?
@RamonMartinez assert_allclose seems to play nicely with unittest :)
@RamonMartinez if you use Python's unittest you can use self.assertIsNone(np.testing.assert_array_equal(a, b)) as it returns None if the arrays are equal.
|
36

I think (arr1 == arr2).all() looks pretty nice. But you could use:

numpy.allclose(arr1, arr2)

but it's not quite the same.

An alternative, almost the same as your example is:

numpy.alltrue(arr1 == arr2)

Note that scipy.array is actually a reference numpy.array. That makes it easier to find the documentation.

Comments

25

I find that using self.assertEqual(arr1.tolist(), arr2.tolist()) is the easiest way of comparing arrays with unittest.

I agree it's not the prettiest solution and it's probably not the fastest but it's probably more uniform with the rest of your test cases, you get all the unittest error description and it's really simple to implement.

1 Comment

Note this won't work well with np.nan, since np.nan != np.nan and the self.assertEqual attempt won't be able to account for that.
9

In my tests I use this:

numpy.testing.assert_array_equal(arr1, arr2)

1 Comment

This is best, because it gives an error message pointing to where the error is
8

Since Python 3.2 you can use assertSequenceEqual(array1.tolist(), array2.tolist()).

This has the added value of showing you the exact items in which the arrays differ.

1 Comment

Unfortunately, it doesn't work well when arrays are of float type. We really need assertSequenceAlmostEqual
8
self.assertTrue(np.array_equal(x, y, equal_nan=True))

equal_nan = True if you want to np.nan == np.nan returns True

or you can use numpy.allclose to compare with torelance.

Comments

3

Use numpy

numpy.array_equal(a, b)

Comments

2

np.linalg.norm(arr1 - arr2) < 1e-6

1 Comment

Please add some context
0

Using built-in unittest module works okay for nested array (deep equality) using Python 3.10.12

self.assertEqual([
    ["1","0","1","1","0","1","1"]
], [
    ["1","0","1","1","0","1","x"]
])

and it prints a nice output message for failure.

First differing element 0:
['1', '0', '1', '1', '0', '1', '1']
['1', '0', '1', '1', '0', '1', 'x']

- [['1', '0', '1', '1', '0', '1', '1']]
?                                  ^

+ [['1', '0', '1', '1', '0', '1', 'x']]
?   

A note based on your question: if you're always comparing a pointer to the same array (or modifying the array in place then comparing it to itself) the result will yield true every time... so that will be a mistake.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.