Skip to content Skip to sidebar Skip to footer

How Do I Test That I'm Calling Pickle.dump() Correctly?

I want to test this method: class Data(object): def save(self, filename=''): if filename: self.filename = filename if not self.filename:

Solution 1:

Patch open() and return an instance of writeable StringIO from it. Load pickled data from that StringIO and test its structure and values (test that it's equivalent to self.data). Something like this:

import builtins # or __builtin__ for Python 2
builtins.open = open = Mock()
open.return_value = sio = StringIO()
self.data.save('foo.pkl')
new_data = pickle.load(sio.getvalue())
self.assertEqual(self.data, new_data)

Post a Comment for "How Do I Test That I'm Calling Pickle.dump() Correctly?"