Skip to main content

Mocky is a class that helps you create mock objects for use in doctests.

Project description

class Mocky(object):
"""Mocky is a class that wants to help you with setting up mock
objects for your tests. It helps you observe which functions get
called (with which parameters) and which attributes are set.

Unless given a name, a Mocky's name is 'root':

>>> Mocky().name
'root'

Let's start with a simple example that sets some variables so we
get a feeling of how Mocky works. Note that attribute accesss
will never result in AttributeError. Instead, an attribute access
to a nonexistent member variable will yield another Mocky
instance:

>>> f = Mocky('f')
>>> f
f
>>> unusual = f.unusual
>>> unusual
f.unusual
>>> type(unusual) is Mocky
True
>>> unusual is f.unusual
True
>>> f.a.c.r = 'Fidelio'
Set f.a.c.r to 'Fidelio'
>>> f.a.c.r
'Fidelio'

Note that when we set 'f.a.c.r' to 'Fidelio', Mocky printed out
that the attribute was set. Suppose we have a function 'fun' that
sets some fancy variable on a given object:

>>> def fun(obj):
... if obj.please_process_me:
... obj.there_you = 'go'
>>> myobj = Mocky('myobj')
>>> fun(myobj)
Set myobj.there_you to 'go'
>>> myobj.please_process_me = False
Set myobj.please_process_me to False
>>> fun(myobj)

Mocky also supports calling. Another function that does a bit
more with our test object:

>>> def starve(character):
... character.getStatus().hitpoints -= 1
>>> starve(Mocky('Hugo')) # doctest: +ELLIPSIS
Traceback (most recent call last):
...
TypeError: unsupported operand type(s) for -=: 'Mocky' and 'int'
>>> ezequiel = Mocky('ezequiel')
>>> ezequiel.getStatus().hitpoints = 0
Called ezequiel.getStatus()
Set ezequiel.getStatus().hitpoints to 0
>>> starve(ezequiel)
Called ezequiel.getStatus()
Set ezequiel.getStatus().hitpoints to -1

For calls, Mocky will return the same value if the signature is
the same:

>>> secret = f.unusual(password='secret')
Called f.unusual(password='secret')
>>> secret is f.unusual(password='secret')
Called f.unusual(password='secret')
True
>>> secret is f.unusual(password='unsafe')
Called f.unusual(password='unsafe')
False
"""
def __init__(self, name='root'):
self.__dict__['name'] = name
self.__dict__['_calls'] = {}

def __call__(self, *args, **kwargs):
argsstr = ', '.join([repr(arg) for arg in args])
keys = sorted(kwargs.keys())
kwargsstr = ', '.join(['%s=%r' % (key, kwargs[key]) for key in keys])
if argsstr and kwargsstr:
allargs = ', '.join([argsstr, kwargsstr])
else:
allargs = argsstr or kwargsstr

print "Called %s(%s)" % (self.name, allargs)
if allargs not in self._calls:
self._calls[allargs] = Mocky('%s(%s)' % (self.name, allargs))
return self._calls[allargs]

def __repr__(self):
return self.name

def __getattr__(self, name):
if name not in self.__dict__:
self.__dict__[name] = Mocky('%s.%s' % (self.name, name))
return self.__dict__[name]

def __setattr__(self, name, value):
print "Set %s.%s to %r" % (self.name, name, value)
self.__dict__[name] = value

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

Mocky-0.1.tar.gz (2.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

Mocky-0.1-py2.4.egg (5.8 kB view details)

Uploaded Egg

File details

Details for the file Mocky-0.1.tar.gz.

File metadata

  • Download URL: Mocky-0.1.tar.gz
  • Upload date:
  • Size: 2.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for Mocky-0.1.tar.gz
Algorithm Hash digest
SHA256 da952c9d4dcf93e0951852e04f6d4b2ee5a654ff33031f74c106748a5d459e27
MD5 9098b132c2819d8f48969f9e98c74158
BLAKE2b-256 acdbaff78c0827a93069d294ccb0b9ad98f90e842906c7e49285df9448d612ea

See more details on using hashes here.

File details

Details for the file Mocky-0.1-py2.4.egg.

File metadata

  • Download URL: Mocky-0.1-py2.4.egg
  • Upload date:
  • Size: 5.8 kB
  • Tags: Egg
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for Mocky-0.1-py2.4.egg
Algorithm Hash digest
SHA256 756715d10e89f18b91af87550148181a6a1ac5d33441b9ee251b2c65bc9bfa55
MD5 f5ffd01b13f104ba8550d65928269b95
BLAKE2b-256 432635bb10fb1b564e4b8857f36fef62c7f0cf29cecc33fd8b86db3bc63e1c9e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page