Skip to main content
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, simply 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.

Release files for pretend 1.0.9

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pretend 1.0.9
File Size Uploaded
pretend-1.0.9.tar.gz 4.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pretend 1.0.9
File Interpreter ABI Platform
pretend-1.0.9-py2.py3-none-any.whl Python 3, Python 2 none any Details

Total release size: 8.7 kB

Release files / pretend-1.0.9.tar.gz

Download URL pretend-1.0.9.tar.gz
Size 4.8 kB
Tags Source
SHA-256 checksum
How to use checksums
c90eb810cde8ebb06dafcb8796f9a95228ce796531bc806e794c2f4649aa1b10
BLAKE2b-256 checksum
How to use checksums
3cf87c86fd40c9e83deb10891a60d2dcb1af0b3b38064d72ebdb12486acc824f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / pretend-1.0.9-py2.py3-none-any.whl

Download URL pretend-1.0.9-py2.py3-none-any.whl
Size 3.8 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
e389b12b7073604be67845dbe32bf8297360ad9a609b24846fe15d86e0b7dc01
BLAKE2b-256 checksum
How to use checksums
491f3d4f0579913edd3ad5b23ad52fcc42531cb736ad52af2ba6c057da8785b6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

1.0.9 This release

2 release files

1.0.8

2 release files

1.0.7

2 release files

1.0.6

2 release files

1.0.5

2 release files

1.0.4

1 release file

1.0.3

2 release files

1.0.2

2 release files

1.0.1

1 release file

1.0

1 release file

0.7

1 release file

0.6

1 release file

0.5

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page