[openstack-dev] Proposal for including fake implementations in python-client packages

Christopher Armstrong chris.armstrong at rackspace.com
Tue Jul 2 16:29:08 UTC 2013

On Tue, Jul 2, 2013 at 11:10 AM, Kurt Griffiths
<kurt.griffiths at rackspace.com> wrote:
> The idea has merit; my main concern is that we would be duplicating
> significant chunks of code/logic between the fakes and the real services.
> How can we do this in a DRY way?

I've done it a few different ways for libraries I've worked on.

Usually, the fakes don't actually duplicate much code from the real
implementation. But in the cases they do, I've had situations like

class RealImplementation(object):

  def do_network_stuff(self, stuff):

  def low_level_operation(self):
    return self.do_network_stuff("GET /integer")

  def high_level_operation(self):
    return self.low_level_operation() + 5

I'd just create a subclass like this:

class FakeImplementation(RealImplementation):

  def do_network_stuff(self, stuff):
    raise NotImplementedError("This should never be called!")

  def low_level_operation(self):
    return self.integer # or however you implement your fake

This has two interesting properties:

1. I don't have to reimplement the high_level_operation
2. If I forget to implement a fake version of some method that invokes
do_network_stuff, then it will blow up with a NotImplementedError so
my test doesn't accidentally do real network stuff.

This is just an example from some recent work I did on a simple RPC
client with an HTTP API (unrelated to OpenStack client libraries), but
that just so happens to be the case that Alex is discussing, so I
think it can work well.

IRC: radix
Christopher Armstrong

More information about the OpenStack-dev mailing list