| 1 |
Metadata-Version: 1.1 |
| 2 |
Name: pretend |
| 3 |
Version: 1.0.9 |
| 4 |
Summary: A library for stubbing in Python |
| 5 |
Home-page: https://github.com/alex/pretend |
| 6 |
Author: Alex Gaynor |
| 7 |
Author-email: [email protected] |
| 8 |
License: BSD |
| 9 |
Description: pretend |
| 10 |
======= |
| 11 |
|
| 12 |
.. image:: https://secure.travis-ci.org/alex/pretend.png |
| 13 |
:target: https://travis-ci.org/alex/pretend |
| 14 |
|
| 15 |
Pretend is a library to make stubbing with Python easier. |
| 16 |
|
| 17 |
What is stubbing? |
| 18 |
----------------- |
| 19 |
|
| 20 |
Stubbing is a technique for writing tests. You may hear the term mixed up with |
| 21 |
mocks, fakes, or doubles. Basically a stub is an object that returns pre-canned |
| 22 |
responses, rather than doing any computation. |
| 23 |
|
| 24 |
Martin Fowler does a good job explaining the terms in his `Mocks Aren't Stubs`_ |
| 25 |
article. |
| 26 |
|
| 27 |
.. _`Mocks Aren't Stubs`: http://martinfowler.com/articles/mocksArentStubs.html |
| 28 |
|
| 29 |
How do I install ``pretend``? |
| 30 |
----------------------------- |
| 31 |
|
| 32 |
It's easy with ``pip``! |
| 33 |
|
| 34 |
.. code:: bash |
| 35 |
|
| 36 |
$ pip install pretend |
| 37 |
|
| 38 |
How do I use ``pretend``? |
| 39 |
------------------------- |
| 40 |
|
| 41 |
It's easy, the ``stub`` function makes it easy to create a stub: |
| 42 |
|
| 43 |
.. code:: pycon |
| 44 |
|
| 45 |
>>> from pretend import stub |
| 46 |
>>> x = stub(country_code="US") |
| 47 |
>>> some_function(x) |
| 48 |
|
| 49 |
Here ``x`` will be an object with a single attribute ``country_code`` which has |
| 50 |
the value ``"US"``. Unlike mocks, ``x`` will not respond to any other attribute |
| 51 |
or methods, nor does it have any methods for making assertions about what you |
| 52 |
accessed. |
| 53 |
|
| 54 |
If you want to add a method to the stub, simply provide a function to it: |
| 55 |
|
| 56 |
.. code:: pycon |
| 57 |
|
| 58 |
>>> from pretend import stub |
| 59 |
>>> x = stub(country_code=lambda: "US") |
| 60 |
>>> x.country_code() |
| 61 |
'US' |
| 62 |
|
| 63 |
It's important to note that functions on stubs *do not* take a ``self`` |
| 64 |
argument, this is because stubs should be returning pre-canned values, not |
| 65 |
doing computations. |
| 66 |
|
| 67 |
Exceptions with ``pretend`` |
| 68 |
--------------------------- |
| 69 |
|
| 70 |
Sometimes a method you want to stub doesn't return a value, but instead raises |
| 71 |
an exception. To make this easy, ``pretend`` provides a helper function, |
| 72 |
``raiser``, it can be used like so: |
| 73 |
|
| 74 |
.. code:: pycon |
| 75 |
|
| 76 |
>>> from pretend import stub, raiser |
| 77 |
>>> x = stub(func=raiser(ValueError)) |
| 78 |
>>> x.func() |
| 79 |
Traceback (most recent call last): |
| 80 |
File "<stdin>", line 1, in <module> |
| 81 |
File "pretend.py", line 74, in inner |
| 82 |
raise exc |
| 83 |
ValueError |
| 84 |
|
| 85 |
Why is stubbing better? |
| 86 |
----------------------- |
| 87 |
|
| 88 |
Ideally stubbing tests how your system responds to a particular input, rather |
| 89 |
than which API is used. Stubbing still requires you to write tests that check |
| 90 |
the results of a computation, rather than looking for side effects. This |
| 91 |
doesn't always work though, so you do sometimes still need mocking (e.g. |
| 92 |
sometimes you really want to check for a side effect.) |
| 93 |
|
| 94 |
How do I get my stub into place? |
| 95 |
-------------------------------- |
| 96 |
|
| 97 |
If you come from other mocking libraries you're probably used to a ``patch`` |
| 98 |
method to put a mock in place. ``pretend`` doesn't include anything like this, |
| 99 |
a) we believe it's better, where possible, to pass stubs as arguments rather |
| 100 |
than monkey patch them into place, b) we believe that when you do need to |
| 101 |
monkey patch something into place you should use something provided by your |
| 102 |
testing tool. ``py.test`` includes `such a tool`_. |
| 103 |
|
| 104 |
.. _`such a tool`: http://pytest.org/latest/monkeypatch.html |
| 105 |
|
| 106 |
What if I really need to record the calls? |
| 107 |
------------------------------------------ |
| 108 |
|
| 109 |
If you really really need to, ``pretend`` includes a ``call_recorder`` utility: |
| 110 |
|
| 111 |
.. code:: pycon |
| 112 |
|
| 113 |
>>> from pretend import call_recorder, call |
| 114 |
>>> f = call_recorder(lambda a: a + 2) |
| 115 |
>>> f(3) |
| 116 |
5 |
| 117 |
>>> assert f.calls == [call(3)] |
| 118 |
|
| 119 |
Who wrote this? |
| 120 |
--------------- |
| 121 |
|
| 122 |
``pretend`` is by Alex Gaynor, who was just tired of not having a good stubbing |
| 123 |
tool for Python. The name is from Idan Gazit. |
| 124 |
|
| 125 |
Platform: UNKNOWN |
| 126 |
Classifier: Development Status :: 5 - Production/Stable |
| 127 |
Classifier: Intended Audience :: Developers |
| 128 |
Classifier: License :: OSI Approved :: BSD License |
| 129 |
Classifier: Operating System :: OS Independent |
| 130 |
Classifier: Topic :: Software Development :: Testing |
| 131 |
Classifier: Programming Language :: Python :: 2.6 |
| 132 |
Classifier: Programming Language :: Python :: 2.7 |
| 133 |
Classifier: Programming Language :: Python :: 3.2 |
| 134 |
Classifier: Programming Language :: Python :: 3.3 |
| 135 |
Classifier: Programming Language :: Python :: 3.4 |
| 136 |
Classifier: Programming Language :: Python :: 3.5 |
| 137 |
Classifier: Programming Language :: Python :: 3.6 |