pretend-1.0.8/0000755000076500000240000000000012306715062014616 5ustar alex_gaynorstaff00000000000000pretend-1.0.8/LICENSE.rst0000644000076500000240000000300212175521607016431 0ustar alex_gaynorstaff00000000000000Copyright (c) Alex Gaynor and individual contributors. All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 3. Neither the name of pretend nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. pretend-1.0.8/MANIFEST.in0000644000076500000240000000004712202223677016356 0ustar alex_gaynorstaff00000000000000include README.rst include LICENSE.rst pretend-1.0.8/PKG-INFO0000644000076500000240000001206012306715062015712 0ustar alex_gaynorstaff00000000000000Metadata-Version: 1.1 Name: pretend Version: 1.0.8 Summary: A library for stubbing in Python Home-page: https://github.com/alex/pretend Author: Alex Gaynor Author-email: alex.gaynor@gmail.com License: BSD Description: pretend ======= .. image:: https://secure.travis-ci.org/alex/pretend.png :target: https://travis-ci.org/alex/pretend Pretend is a library to make stubbing with Python easier. What is stubbing? ----------------- Stubbing is a technique for writing tests. You may hear the term mixed up with mocks, fakes, or doubles. Basically a stub is an object that returns pre-canned responses, rather than doing any computation. Martin Fowler does a good job explaining the terms in his `Mocks Aren't Stubs`_ article. .. _`Mocks Aren't Stubs`: http://martinfowler.com/articles/mocksArentStubs.html How do I install ``pretend``? ----------------------------- It's easy with ``pip``! .. code:: bash $ pip install pretend How do I use ``pretend``? ------------------------- It's easy, the ``stub`` function makes it easy to create a stub: .. code:: pycon >>> from pretend import stub >>> x = stub(country_code="US") >>> some_function(x) Here ``x`` will be an object with a single attribute ``country_code`` which has the value ``"US"``. Unlike mocks, ``x`` will not respond to any other attribute or methods, nor does it have any methods for making assertions about what you accessed. If you want to add a method to the stub, simple provide a function to it: .. code:: pycon >>> from pretend import stub >>> x = stub(country_code=lambda: "US") >>> x.country_code() 'US' It's important to note that functions on stubs *do not* take a ``self`` argument, this is because stubs should be returning pre-canned values, not doing computations. Exceptions with ``pretend`` --------------------------- Sometimes a method you want to stub doesn't return a value, but instead raises an exception. To make this easy, ``pretend`` provides a helper function, ``raiser``, it can be used like so: .. code:: pycon >>> from pretend import stub, raiser >>> x = stub(func=raiser(ValueError)) >>> x.func() Traceback (most recent call last): File "", line 1, in File "pretend.py", line 74, in inner raise exc ValueError Why is stubbing better? ----------------------- Ideally stubbing tests how your system responds to a particular input, rather than which API is used. Stubbing still requires you to write tests that check the results of a computation, rather than looking for side effects. This doesn't always work though, so you do sometimes still need mocking (e.g. sometimes you really want to check for a side effect.) How do I get my stub into place? -------------------------------- If you come from other mocking libraries you're probably used to a ``patch`` method to put a mock in place. ``pretend`` doesn't include anything like this, a) we believe it's better, where possible, to pass stubs as arguments rather than monkey patch them into place, b) we believe that when you do need to monkey patch something into place you should use something provided by your testing tool. ``py.test`` includes `such a tool`_. .. _`such a tool`: http://pytest.org/latest/monkeypatch.html What if I really need to record the calls? ------------------------------------------ If you really really need to, ``pretend`` includes a ``call_recorder`` utility: .. code:: pycon >>> from pretend import call_recorder, call >>> f = call_recorder(lambda a: a + 2) >>> f(3) 5 >>> assert f.calls == [call(3)] Who wrote this? --------------- ``pretend`` is by Alex Gaynor, who was just tired of not having a good stubbing tool for Python. The name is from Idan Gazit. Platform: UNKNOWN Classifier: Development Status :: 5 - Production/Stable Classifier: Intended Audience :: Developers Classifier: License :: OSI Approved :: BSD License Classifier: Operating System :: OS Independent Classifier: Topic :: Software Development :: Testing Classifier: Programming Language :: Python :: 2.6 Classifier: Programming Language :: Python :: 2.7 Classifier: Programming Language :: Python :: 3.2 Classifier: Programming Language :: Python :: 3.3 pretend-1.0.8/pretend.egg-info/0000755000076500000240000000000012306715062017751 5ustar alex_gaynorstaff00000000000000pretend-1.0.8/pretend.egg-info/dependency_links.txt0000644000076500000240000000000112306715062024017 0ustar alex_gaynorstaff00000000000000 pretend-1.0.8/pretend.egg-info/PKG-INFO0000644000076500000240000001206012306715062021045 0ustar alex_gaynorstaff00000000000000Metadata-Version: 1.1 Name: pretend Version: 1.0.8 Summary: A library for stubbing in Python Home-page: https://github.com/alex/pretend Author: Alex Gaynor Author-email: alex.gaynor@gmail.com License: BSD Description: pretend ======= .. image:: https://secure.travis-ci.org/alex/pretend.png :target: https://travis-ci.org/alex/pretend Pretend is a library to make stubbing with Python easier. What is stubbing? ----------------- Stubbing is a technique for writing tests. You may hear the term mixed up with mocks, fakes, or doubles. Basically a stub is an object that returns pre-canned responses, rather than doing any computation. Martin Fowler does a good job explaining the terms in his `Mocks Aren't Stubs`_ article. .. _`Mocks Aren't Stubs`: http://martinfowler.com/articles/mocksArentStubs.html How do I install ``pretend``? ----------------------------- It's easy with ``pip``! .. code:: bash $ pip install pretend How do I use ``pretend``? ------------------------- It's easy, the ``stub`` function makes it easy to create a stub: .. code:: pycon >>> from pretend import stub >>> x = stub(country_code="US") >>> some_function(x) Here ``x`` will be an object with a single attribute ``country_code`` which has the value ``"US"``. Unlike mocks, ``x`` will not respond to any other attribute or methods, nor does it have any methods for making assertions about what you accessed. If you want to add a method to the stub, simple provide a function to it: .. code:: pycon >>> from pretend import stub >>> x = stub(country_code=lambda: "US") >>> x.country_code() 'US' It's important to note that functions on stubs *do not* take a ``self`` argument, this is because stubs should be returning pre-canned values, not doing computations. Exceptions with ``pretend`` --------------------------- Sometimes a method you want to stub doesn't return a value, but instead raises an exception. To make this easy, ``pretend`` provides a helper function, ``raiser``, it can be used like so: .. code:: pycon >>> from pretend import stub, raiser >>> x = stub(func=raiser(ValueError)) >>> x.func() Traceback (most recent call last): File "", line 1, in File "pretend.py", line 74, in inner raise exc ValueError Why is stubbing better? ----------------------- Ideally stubbing tests how your system responds to a particular input, rather than which API is used. Stubbing still requires you to write tests that check the results of a computation, rather than looking for side effects. This doesn't always work though, so you do sometimes still need mocking (e.g. sometimes you really want to check for a side effect.) How do I get my stub into place? -------------------------------- If you come from other mocking libraries you're probably used to a ``patch`` method to put a mock in place. ``pretend`` doesn't include anything like this, a) we believe it's better, where possible, to pass stubs as arguments rather than monkey patch them into place, b) we believe that when you do need to monkey patch something into place you should use something provided by your testing tool. ``py.test`` includes `such a tool`_. .. _`such a tool`: http://pytest.org/latest/monkeypatch.html What if I really need to record the calls? ------------------------------------------ If you really really need to, ``pretend`` includes a ``call_recorder`` utility: .. code:: pycon >>> from pretend import call_recorder, call >>> f = call_recorder(lambda a: a + 2) >>> f(3) 5 >>> assert f.calls == [call(3)] Who wrote this? --------------- ``pretend`` is by Alex Gaynor, who was just tired of not having a good stubbing tool for Python. The name is from Idan Gazit. Platform: UNKNOWN Classifier: Development Status :: 5 - Production/Stable Classifier: Intended Audience :: Developers Classifier: License :: OSI Approved :: BSD License Classifier: Operating System :: OS Independent Classifier: Topic :: Software Development :: Testing Classifier: Programming Language :: Python :: 2.6 Classifier: Programming Language :: Python :: 2.7 Classifier: Programming Language :: Python :: 3.2 Classifier: Programming Language :: Python :: 3.3 pretend-1.0.8/pretend.egg-info/SOURCES.txt0000644000076500000240000000027412306715062021640 0ustar alex_gaynorstaff00000000000000LICENSE.rst MANIFEST.in README.rst pretend.py setup.cfg setup.py pretend.egg-info/PKG-INFO pretend.egg-info/SOURCES.txt pretend.egg-info/dependency_links.txt pretend.egg-info/top_level.txtpretend-1.0.8/pretend.egg-info/top_level.txt0000644000076500000240000000001012306715062022472 0ustar alex_gaynorstaff00000000000000pretend pretend-1.0.8/pretend.py0000644000076500000240000000572012306714417016640 0ustar alex_gaynorstaff00000000000000import functools import sys PY3K = sys.version_info >= (3,) methods = set([ "__iter__", "__len__", "__contains__", "__getitem__", "__setitem__", "__delitem__", "__enter__", "__exit__", "__lt__", "__le__", "__eq__", "__ne__", "__gt__", "__ge__", "__add__", "__and__", "__divmod__", "__floordiv__", "__lshift__", "__mod__", "__mul__", "__or__", "__pow__", "__rshift__", "__sub__", "__truediv__", "__xor__", "__repr__", ]) if PY3K: methods.add("__next__") methods.add("__bool__") else: methods.add("__div__") methods.add("__nonzero__") MAGIC_METHODS = frozenset(methods) del methods def _build_magic_dispatcher(method): def inner(self, *args, **kwargs): return self.__dict__[method](*args, **kwargs) inner.__name__ = method return inner class stub(object): _classes_cache = {} def __new__(cls, **kwargs): magic_methods_present = MAGIC_METHODS.intersection(kwargs) if magic_methods_present not in cls._classes_cache: attrs = dict( (method, _build_magic_dispatcher(method)) for method in magic_methods_present ) attrs["__module__"] = cls.__module__ cls._classes_cache[magic_methods_present] = ( type("stub", (cls,), attrs) ) new_cls = cls._classes_cache[magic_methods_present] return super(stub, new_cls).__new__(new_cls) def __init__(self, **kwargs): self.__dict__.update(kwargs) def __repr__(self): return '' % ', '.join([ '%s=%r' % (key, val) for key, val in self.__dict__.items() ]) def raiser(exc): if ( not ( isinstance(exc, BaseException) or isinstance(exc, type) and issubclass(exc, BaseException) ) ): raise TypeError("exc must be either an exception instance or class.") def inner(*args, **kwargs): raise exc return inner class call(object): def __init__(self, *args, **kwargs): self.args = args self.kwargs = kwargs def __eq__(self, other): if not isinstance(other, call): return NotImplemented return self.args == other.args and self.kwargs == other.kwargs def __ne__(self, other): return not (self == other) def __hash__(self): return hash(( self.args, frozenset(self.kwargs.items()) )) def __repr__(self): args = ", ".join(map(repr, self.args)) kwargs = ", ".join("%s=%r" % (k, v) for k, v in self.kwargs.items()) comma = ", " if args and kwargs else "" return "" % (args, comma, kwargs) def call_recorder(func): @functools.wraps(func) def inner(*args, **kwargs): inner.calls.append(call(*args, **kwargs)) return func(*args, **kwargs) inner.calls = [] return inner pretend-1.0.8/README.rst0000644000076500000240000000675012175524207016320 0ustar alex_gaynorstaff00000000000000pretend ======= .. image:: https://secure.travis-ci.org/alex/pretend.png :target: https://travis-ci.org/alex/pretend Pretend is a library to make stubbing with Python easier. What is stubbing? ----------------- Stubbing is a technique for writing tests. You may hear the term mixed up with mocks, fakes, or doubles. Basically a stub is an object that returns pre-canned responses, rather than doing any computation. Martin Fowler does a good job explaining the terms in his `Mocks Aren't Stubs`_ article. .. _`Mocks Aren't Stubs`: http://martinfowler.com/articles/mocksArentStubs.html How do I install ``pretend``? ----------------------------- It's easy with ``pip``! .. code:: bash $ pip install pretend How do I use ``pretend``? ------------------------- It's easy, the ``stub`` function makes it easy to create a stub: .. code:: pycon >>> from pretend import stub >>> x = stub(country_code="US") >>> some_function(x) Here ``x`` will be an object with a single attribute ``country_code`` which has the value ``"US"``. Unlike mocks, ``x`` will not respond to any other attribute or methods, nor does it have any methods for making assertions about what you accessed. If you want to add a method to the stub, simple provide a function to it: .. code:: pycon >>> from pretend import stub >>> x = stub(country_code=lambda: "US") >>> x.country_code() 'US' It's important to note that functions on stubs *do not* take a ``self`` argument, this is because stubs should be returning pre-canned values, not doing computations. Exceptions with ``pretend`` --------------------------- Sometimes a method you want to stub doesn't return a value, but instead raises an exception. To make this easy, ``pretend`` provides a helper function, ``raiser``, it can be used like so: .. code:: pycon >>> from pretend import stub, raiser >>> x = stub(func=raiser(ValueError)) >>> x.func() Traceback (most recent call last): File "", line 1, in File "pretend.py", line 74, in inner raise exc ValueError Why is stubbing better? ----------------------- Ideally stubbing tests how your system responds to a particular input, rather than which API is used. Stubbing still requires you to write tests that check the results of a computation, rather than looking for side effects. This doesn't always work though, so you do sometimes still need mocking (e.g. sometimes you really want to check for a side effect.) How do I get my stub into place? -------------------------------- If you come from other mocking libraries you're probably used to a ``patch`` method to put a mock in place. ``pretend`` doesn't include anything like this, a) we believe it's better, where possible, to pass stubs as arguments rather than monkey patch them into place, b) we believe that when you do need to monkey patch something into place you should use something provided by your testing tool. ``py.test`` includes `such a tool`_. .. _`such a tool`: http://pytest.org/latest/monkeypatch.html What if I really need to record the calls? ------------------------------------------ If you really really need to, ``pretend`` includes a ``call_recorder`` utility: .. code:: pycon >>> from pretend import call_recorder, call >>> f = call_recorder(lambda a: a + 2) >>> f(3) 5 >>> assert f.calls == [call(3)] Who wrote this? --------------- ``pretend`` is by Alex Gaynor, who was just tired of not having a good stubbing tool for Python. The name is from Idan Gazit. pretend-1.0.8/setup.cfg0000644000076500000240000000012212306715062016432 0ustar alex_gaynorstaff00000000000000[wheel] universal = 1 [egg_info] tag_build = tag_date = 0 tag_svn_revision = 0 pretend-1.0.8/setup.py0000644000076500000240000000150712306714761016340 0ustar alex_gaynorstaff00000000000000from setuptools import setup with open("README.rst") as f: readme = f.read() setup( version="1.0.8", name="pretend", description="A library for stubbing in Python", long_description=readme, author="Alex Gaynor", author_email="alex.gaynor@gmail.com", py_modules=["pretend"], url="https://github.com/alex/pretend", license="BSD", classifiers=[ "Development Status :: 5 - Production/Stable", "Intended Audience :: Developers", "License :: OSI Approved :: BSD License", "Operating System :: OS Independent", "Topic :: Software Development :: Testing", "Programming Language :: Python :: 2.6", "Programming Language :: Python :: 2.7", "Programming Language :: Python :: 3.2", "Programming Language :: Python :: 3.3", ] )