[TIP] Struggling with pymock

Tobias Roth misc.lists at fsck.ch
Mon Sep 10 10:04:27 PDT 2007

Atul Varma wrote:
> Tobias,
> pymock definitely took me a bit of getting used to.  Here's example code
> to test your Example class; I've tried commenting it to make evident
> some of the things I had a hard time understanding when I first started
> using pymock.

<snip helpful example and link>

Thanks Atul, your example helped me a lot. I got my test with pymock
running partly. However, I still two have issues with it:

1) The output is very unclear. I often get a 'raise PlaybackFailure("No
further actions defined"', with no chance of finding out what went
wrong. Consider the following example:

s = open("file")
for line in s:

For the open() and close() alone, I was able to get the test running as
you suggested, by stubbing out __builtins__.open(). But when I add some
reading, I always get the "No further actions defined". How do I find
out what methods to record here? More detailed output from pymock would
really help, especially when overriding stuff from libraries.

2) How to run the tests? I am able to run it manually, but nosetest
gives me an AttributeError: 'dict' object has no attribute 'open', no
matter what I turn my code. This seems to be related to the stubbing of
__builtins__.open() though, and might be an issue with nosetest.

I'm still not quite convinced I look at the problem from the right
angle, but stubbing out __builtins__.open() and have it return a mock
object surely looks like a good way to test.


More information about the testing-in-python mailing list