[TIP] Struggling with pymock

Rod Hyde rod.hyde at gmail.com
Wed Sep 19 02:15:36 PDT 2007


I think I found the problem. It appears to be a bug in pymock itself.
The issue is in the Controller's playback() method which starts on
line 142 of pymock.py.

        def playback(self, action):
                recordedAction = self.actions.get(action)
                if recordedAction == None:
                        raise PlaybackFailure("No further actions defined")
                elif not action == recordedAction:
                        raise PlaybackFailure("Inappropriate action")
                else:
                        exprValue = recordedAction.playback()
                        if recordedAction.isReadyForRemoval:
                                self.actions.pop(recordedAction)
                        return exprValue

Looking at the final 'else' clause, if exprValue =
recordedAction.playback() raises an exception, as would be expected
when using expectAndRaise() then the action will never be removed.

Using try..finally to cater for this (as shown here) has fixed the problem...


                else:
                        try:
                                exprValue = recordedAction.playback()
                        finally:
                                if recordedAction.isReadyForRemoval:
                                        self.actions.pop(recordedAction)
                        return exprValue

--- Rod



More information about the testing-in-python mailing list