Created
July 10, 2011 23:46
-
-
Save ilpoldo/1075097 to your computer and use it in GitHub Desktop.
Python Useful Test Objects
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys,types | |
class MockCallable(): | |
""" Mocks a function, can be enquired on how many calls it received """ | |
def __init__(self, result): | |
self.result = result | |
self._calls = [] | |
def __call__(self, *arguments): | |
"""Mock callable""" | |
self._calls.append(arguments) | |
return self.result | |
def called(self): | |
"""docstring for called""" | |
return self._calls | |
class StubModule(types.ModuleType, object): | |
""" Uses a stub instead of loading libraries """ | |
def __init__(self, moduleName): | |
self.__name__ = moduleName | |
sys.modules[moduleName] = self | |
def __repr__(self): | |
"Return a string representation of the stub module: It's name and the methods and attributes it holds" | |
name = self.__name__ | |
mocks = ', '.join(set(dir(self)) - set(['__name__'])) | |
return "<StubModule: %(name)s; mocks: %(mocks)s>" % locals() | |
class StubObject(object): | |
pass | |
class Null: | |
""" An object that is neutral to most kind of interaction """ | |
def __init__(self, *args, **kwargs): return None | |
def __call__(self, *args, **kwargs): return self | |
def __getattr__(self, mname): return self | |
def __setattr__(self, name, value): return self | |
def __delattr__(self, name): return self | |
def __repr__(self): return "<Null>" | |
def __str__(self): return "Null" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment