lundi 5 janvier 2015

Mock: assert_called_once_with a numpy array as argument

I have a method in a class that I want to test using the unittest framework, using Python 3.4. I prefer to work using a Mock as the object of the class to test, as explained in Daniel Arbuckle's Learning Python Testing.


This is what I would do:



class Test_set_initial_clustering_rand(TestCase):

def setUp(self):
self.sut = Mock()

def test_gw_01(self):
self.sut.seed = 1
ClustererKmeans.set_initial_clustering_rand(self.sut, N_clusters=1, N_persons=6)
e = np.array([0, 0, 0, 0, 0, 0])
self.sut.set_clustering.assert_called_once_with(e)


This would check if the function set_clustering is called once with the expected argument. The framework tries to compare the two arguments using actual_arg == expected_arg. This goes wrong however if the argument is a numpy array.



Traceback (most recent call last):
File "/Users/.../UT_ClustererKmeans.py", line 184, in test_gw_01
self.sut.set_clustering.assert_called_once_with(e)
File "/Users/.../anaconda/lib/python3.4/unittest/mock.py", line 782, in assert_called_once_with
return self.assert_called_with(*args, **kwargs)
File "/Users/.../anaconda/lib/python3.4/unittest/mock.py", line 769, in assert_called_with
if expected != actual:
File "/Users/.../anaconda/lib/python3.4/unittest/mock.py", line 2001, in __ne__
return not self.__eq__(other)
File "/Users/.../anaconda/lib/python3.4/unittest/mock.py", line 1997, in __eq__
return (other_args, other_kwargs) == (self_args, self_kwargs)
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()


Comparing numpy arrays is done in a different way, but the comparison is made inside the unittest framework. What would be the best way to work around this problem?


Aucun commentaire:

Enregistrer un commentaire