using mocking in [some!] tests

Jamu Kakar jkakar at kakar.ca
Sat May 22 14:14:36 BST 2010


Hi,

On Sat, May 22, 2010 at 10:18 AM, Robert Collins
<robert.collins at canonical.com> wrote:
> I'm thinking very seriously of using a mocking tool for tests in the
> launchpad plugin: its very time consuming to have satisfactory unit
> tests without one, and we don't have a 'real alternative
> implementation' to automatically test against today anyway - staging
> isn't that, for all that its useful for manual interactions.

I started a branch some time ago to implement support for mocking
launchpadlib.  It's missing some key functionality, like the ability
to define how lp_save will work, for example.  The branch is here:

lp:~jkakar/launchpadlib/testing-support

In launchpadlib.testing.resources there's a FakeLaunchpadResource
you can use to get a FakeLaunchpad instance in unit tests, called
self.launchpad.  It's pretty easy to use, for example:

    self.launchpad.me = dict(name="foo", display_name="Foo")
    self.assertEqual("foo", self.launchpad.me.name)
    self.assertEqual("Foo", self.launchpad.me.display_name)

self.launchpad here is an instance of FakeLaunchpad.  The first line
sets sample data and the subsequent lines use the self.launchpad
object, as application code would.  When you set data on the
FakeLaunchpad instance the things you set are validated against the
WADL file, so you can have some confidence that the situation you're
mocking is realistic.  The internals are pretty ugly, but it seems
to work.  It's been a while since I've looked at it, so it's not
obvious how much work is left to finish it, but it would be nice to
get it landed.

There was some discussion about the branch, that provide some
background details about the ideas here:

https://code.edge.launchpad.net/~jkakar/launchpadlib/testing-support/+merge/14444

> Any thoughts on this, and/or on selection of a mocking tool? I'd say
> Gustavo's mocker, but last I heard he doesn't want it packaged, and I
> don't want to add a copy of $whatever to bzr.

I think you should talk to Gustavo about this.  His original concern
was that it may not be compatible across different Python versions,
but so far, we haven't had problems using it with Landscape and
Python 2.4 through 2.6.  I would like to see it packaged, since we
have manually copied it into projects several times.

Also, for what it's worth, Mocker works quite well for us in
Landscape.

Thanks,
J.



More information about the bazaar mailing list