[TIP] Struggling with pymock
Thomas Spreng
spreng at socket.ch
Wed Sep 12 05:23:50 PDT 2007
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
First of all, hi everybody (this is my first post on this list :))
Tobias Roth wrote:
> Now there's only one (I hope) problem left, I repeat it here for people
> not having to read the whole thread again:
>
>
> ** repetition **
> 1) The output [of pymock] 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:
> pass
> s.close()
>
> 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.
> ** end repetition **
not that I'm an expert in testing (let alone py-mocking ;)) but as far
as I know, you can use pymock's generator() method to turn a mock object
into a python generator. The problem using this approach is that I was
unable to record any other function calls on such a generator object.
Note that I'm not recording file.close() in the second test function. I
have no idea why verify() doesn't raise an exception in this case. Maybe
someone else can enlighten me? :)
Keep in mind, that it is also possible to just use a StringIO
(file-like) object instead of a mock object in your example.
Sample tests using both methods are shown below:
<code>
from pymock import *
from StringIO import StringIO
class Foo(object):
def __init__(self, cf, opener=open):
self.arg = []
self.conffile = cf
f = opener(self.conffile)
for line in f:
self.arg.append(line.rstrip())
f.close()
class TestFoo(PyMockTestCase):
path = "/path/to/nonexistant.txt"
content = ["nothing", "to", "see", "here"]
def testFooWithStringIO(self):
file = StringIO('\n'.join(self.content))
m = self.mock()
self.expectAndReturn(m.open(self.path), file)
self.replay()
# done recording, start testing
f = Foo(self.path, m.open)
self.assertEquals(f.arg, self.content)
self.verify()
def testFooWithMockFile(self):
m = self.mock()
file = self.mock()
self.expectAndReturn(m.open(self.path), file)
self.generator(file, self.content)
self.replay()
# done recording, start testing
f = Foo(self.path, m.open)
self.assertEquals(f.arg, self.content)
# didn't record file.close(), why does
# verify() not raise an exception?
self.verify()
</code>
cheers,
tom.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.7 (Darwin)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org
iD8DBQFG59pWyEq/LfKlTbsRAhPgAJ41NKd052m4j+aZzJhfpb9q0CliaQCgts0q
QSMGX34ehwat0MFKzjNhNF8=
=GE2k
-----END PGP SIGNATURE-----
More information about the testing-in-python
mailing list