• Stars
    star
    290
  • Rank 142,981 (Top 3 %)
  • Language
    Python
  • License
    BSD 3-Clause "New...
  • Created about 12 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A library for stubbing in python

pretend

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.

More Repositories

1

what-happens-when

An attempt to answer the age old interview question "What happens when you type google.com into your browser and press enter?"
39,628
star
2

nyt-2020-election-scraper

HTML
1,761
star
3

letsencrypt-aws

Python
729
star
4

rply

An attempt to port David Beazley's PLY to RPython, and give it a cooler API.
Python
377
star
5

django-ajax-validation

A reusable application to preform ajax validation on django forms.
Python
252
star
6

django-templatetag-sugar

This project exists to make defining template tags in Django kickass
Python
213
star
7

ecs-terraform

ECS + Terraform = Crazy delicious
HCL
203
star
8

csv-sql

Query your CSV files with SQL
Rust
192
star
9

django-fixture-generator

django-fixture-generator is a reusable django application to make writing fixtures not suck.
Python
138
star
10

zero_buffer

zero_buffer is a high-performance, zero-copy, implementation of a byte-buffer for Python.
Python
136
star
11

pyvcs

A pure python abstraction layer of multiple VCS, very lightweight.
Python
128
star
12

django-admin-histograms

A library for simple histograms in Django's admin.
Python
106
star
13

alchimia

Python
103
star
14

rust-asn1

A Rust ASN.1 (DER) serializer.
Rust
99
star
15

django-vcs

A django application for working with a VCS, analagous to some of Trac's features.
Python
86
star
16

just-use

Just Use /dev/urandom -- now with more safety at early boot
Rust
60
star
17

django-wsgi

A library for better integration between django and the WSGI world.
Python
49
star
18

python-decompiler

A decompiler for CPython bytecode
Python
48
star
19

piano-man

Python
44
star
20

django-project-skeleton

A skeleton for Django 1.4's new project template support.
Python
41
star
21

django-resume-builder

Python
36
star
22

line-counter

Like `wc -l`, but in Rust and maybe faster
Rust
29
star
23

django-plugins

A generic plugin system for django.
Python
28
star
24

otp-cop

Tooling for verifying that everyone in your orgs has 2fa enabled.
Rust
28
star
25

nba-gamethread

A game thread generator for r/nba
Python
26
star
26

vcs-translator

Python
25
star
27

bagel

Bagels are delicious
Python
22
star
28

Shore

A statically typed programming language with inspiration from C++, Python, and others.
Python
19
star
29

ct-tools

Some Rust code for submitting a cert to all of Chrome's trusted CT logs and getting back the SCTs
Rust
17
star
30

x509-validator

A pure-Python x509 validation library based on pyca/cryptography
Python
16
star
31

httpfuzz

Python
13
star
32

tracebin

The ultimate performance command and control center for PyPy.
Python
12
star
33

django-object-cache

A reusable django object cache.
12
star
34

tls-stats

A webExtension to collect TLS stats
JavaScript
11
star
35

pyelection

A python application for following the US primaries
Python
10
star
36

recipes

My Recipes
Python
10
star
37

ex-post-progress

Rust
9
star
38

evolves

A Python implementation of a genetic algorithm for generating images from polygons
Python
9
star
39

doc-extractor

Go
8
star
40

tls-cal

Python
8
star
41

election-sim

An application for following and simulating the 2008 US Presidential Election.
Python
7
star
42

alex-s-language

Inteded to be a compiled Python, by translating source files to C++
Python
7
star
43

optimizer-model

A work in progress model for a new optimizer for PyPy's JIT.
Python
7
star
44

http-client-bench

Python
7
star
45

revocation-tracker

Python
7
star
46

flickr-avatars

a small webservice to make getting flickr users' avatars
Python
6
star
47

yaffi

Yet Another FFI
Python
6
star
48

json_writer

Python
5
star
49

rdio-export

JavaScript
4
star
50

space-hogs

A game made in python with pyglet.
Python
4
star
51

csv-to-parquet

Go
3
star
52

independent-study-tracebin

Documentation for my independent study on tracebin
Python
3
star
53

montgomery-ladder-go

An implementation of modular exponentiation using the montgomery ladder in Go.
Go
2
star
54

api-serializer

Random hack, don't use.
Python
2
star
55

azure-pipelines-docker-test

2
star
56

win32k-stuff

JavaScript
2
star
57

client-beta

Go
2
star
58

temp-rust-coverage

Rust
1
star
59

doc8

Python
1
star
60

temp-gha-macos-python

1
star
61

botan-fuzzers

Fuzzer instrumentation for botan
C++
1
star