Skip to main content

A library for stubbing in Python

Project description

https://secure.travis-ci.org/alex/pretend.png

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.

How do I install pretend?

It’s easy with pip!

$ pip install pretend

How do I use pretend?

It’s easy, the stub function makes it easy to create a stub:

>>> 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:

>>> 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:

>>> 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.

What if I really need to record the calls?

If you really really need to, pretend includes a call_recorder utility:

>>> 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.

Project details


Download files

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

Source Distribution

pretend-1.0.6.tar.gz (4.8 kB view details)

Uploaded Source

Built Distribution

pretend-1.0.6-py2.py3-none-any.whl (6.1 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file pretend-1.0.6.tar.gz.

File metadata

  • Download URL: pretend-1.0.6.tar.gz
  • Upload date:
  • Size: 4.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for pretend-1.0.6.tar.gz
Algorithm Hash digest
SHA256 0a599862b556561eac81e873c80cb2238212e1a628a8d4bf6fc173b2884f707c
MD5 80eeaed8b424c6d1519bdf7750485871
BLAKE2b-256 f9e3353b57311928bb609788188fe4ba0187b5c6cbe8c1f03d7c96ee9dda9e17

See more details on using hashes here.

File details

Details for the file pretend-1.0.6-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for pretend-1.0.6-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 78f18ba7c491180b4cf44f68e31d912323669675512032a53f2a41bd7028972f
MD5 fcf552c3e80b4e6e8e785aeea8a00036
BLAKE2b-256 3f2ca9e1a4f78005648235b992f8039a513fe34d23c9c4f86c58d58ad1275a63

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page