/usr/share/doc/pypy-pretend/README.rst is in pypy-pretend 1.0.8-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 | 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 "<stdin>", line 1, in <module>
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.
|