[Python-Dev] r86570 - in python/branches/py3k: Lib/unittest/case.py Lib/unittest/test/test_case.py Misc/NEWS
Antoine Pitrou
solipsis at pitrou.net
Sat Nov 20 16:59:49 CET 2010
Le samedi 20 novembre 2010 à 15:48 +0000, Michael Foord a écrit :
> On 20/11/2010 15:42, Antoine Pitrou wrote:
> > On Sat, 20 Nov 2010 16:34:26 +0100 (CET)
> > michael.foord<python-checkins at python.org> wrote:
> >> +
> >> + def testPickle(self):
> >> + # Issue 10326
> >> +
> >> + # Can't use TestCase classes defined in Test class as
> >> + # pickle does not work with inner classes
> >> + test = unittest.TestCase('run')
> >> + for protocol in range(pickle.HIGHEST_PROTOCOL + 1):
> >> +
> >> + # blew up prior to fix
> >> + pickled_test = pickle.dumps(test, protocol=protocol)
> > You must also check that the object can be unpickled, otherwise
> > making TestCase picklable is not only pointless, but misleading the
> > user. Other classes which claim to be picklable (such as e.g.
> > io.BytesIO) are careful to check that unpickling works fine and
> > produces an usable object.
>
> Well, given the *particular* bug it is fixing, ensuring that the
> TestCase instances can be pickled is enough. If they fail to unpickle
> that is a bug in pickle and not in unittest.
It wouldn't be, no. pickle provides several different APIs to ensure
that state gets correctly stored *and* restored, but it's up to
application classes such as TestCase to ensure that they implement those
APIs correctly for the intended behaviour. Therefore, checking that
pickling "works" fine (or, rather, seems to work) is only half ot the
job.
(for example, if you define a __getstate__, chances are you must define
a __setstate__ too, and it is your job to make it work properly)
Antoine.
More information about the Python-Dev
mailing list