Skip to main content

enables seamless replacement of "real" objects with "fake" objects during unit testing

Project description

fakeable
By: Denver Coneybeare <denver@sleepydragon.org>
Aug 12, 2013

Fakeable is a Python library that provides a facility for transparently
replacing objects with fake versions during unit testing. The main advantage
of Fakeable is that from the point of view of the production code there is
nothing that needs to be done except to declare a specific metaclass to opt-in
to being faked. Then, during testing the tests specify to use a fake version
of a specific class and then "magically" at runtime a fake version is used.

Full documentation available at: https://fakeable.readthedocs.org
Source code available at: https://github.com/sleepydragonsw/fakeable

For example, consider the class below, which simply reads the contents of a
text file and returns it:

class FileReader(object):

__metaclass__ = fakeable.Fakeable

def read_file(self, path):
with io.open(path, "rt", encoding="utf8"):
contents = f.read()
return contents

Then consider this class from production code that makes use of it:

def file_contains(path, s):
reader = FileReader()
contents = reader.read_file("config.ini")
found = (s in contents)
return found

When the file_contains() method is invoked outside of unit tests everything
will work as expected: the real FileReader.read_file() method will be invoked.
However, during unit tests, a fake version of the FileReader class can be used
instead:

class FakeFileReader(object):
def read_file(self, path):
raise IOError("file not found")

class Test_FileReader(unittest.TestCase):
def test_file_contains(self):
fakeable.set_class("FileReader", FakeFileReader)
with self.assertRaises(IOError):
file_contains("blah_blah", "hey ho")

When file_contains() is invoked after invoking fakeable.set_class() the call to
"reader = FileReader()" will actually return an instance of FakeFileReader
instead of an instance of the *real* FileReader class.

This is achieved through the magic of metaclasses.

See the documentation in the "doc" folder for more details.

Project details


Download files

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

Source Distributions

fakeable-1.0.3.zip (28.2 kB view details)

Uploaded Source

fakeable-1.0.3.tar.gz (23.0 kB view details)

Uploaded Source

File details

Details for the file fakeable-1.0.3.zip.

File metadata

  • Download URL: fakeable-1.0.3.zip
  • Upload date:
  • Size: 28.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for fakeable-1.0.3.zip
Algorithm Hash digest
SHA256 14539ce1487af0a9422f4ae558967d89abb651e3f702529e23e66da295152d3d
MD5 46ea237a9e482d96238febf35abc3b74
BLAKE2b-256 d1cf5b3d8f2623e343418cafdbb4e159f1b49d562ee771248ecf31a3b22c8407

See more details on using hashes here.

File details

Details for the file fakeable-1.0.3.tar.gz.

File metadata

  • Download URL: fakeable-1.0.3.tar.gz
  • Upload date:
  • Size: 23.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for fakeable-1.0.3.tar.gz
Algorithm Hash digest
SHA256 57378a5d0e6169f70e9fa65d6333a2861a887ceeb28f9374f53428c540034a39
MD5 fb1111b6ce5669da757d559d3fec3e00
BLAKE2b-256 d9701be16946f18dcf1ef767dbbe9e27107d70db00c14553f990d1e40acb6af2

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