Setup

Installation

  1. Get the stable Sinon.PY from pypi:
$ pip install sinon
  1. Install latest version manually:
$ git clone https://github.com/note35/sinon.git
$ cd sinon/
$ python setup.py install
  1. With venv, since Sinon.PY is a library for unittest, It would be best if virtualenv is used in the development platform by following the command below:
$ pip install virtualenv
$ virtualenv env
$ . env/bin/activate
$ pip install sinon

Getting Started

Spies

>>> import sinon
>>> import os
>>> spy = sinon.spy(os, "system")
>>> os.system("pwd")
to/your/current/path
>>> spy.called
True
>>> spy.calledWith("pwd")
True
>>> spy.calledWith(sinon.match(str))
True
>>> spy.calledOnce
True
>>> spy.restore()

Stubs

>>> import sinon
>>> import os
>>> stub = sinon.stub(os, "system").returns("stub result")
>>> os.system("pwd")
'stub result'
>>> stub.restore()
>>>
>>> stub = sinon.stub(os, "system").onCall(2).returns("stub result")
>>> os.system("pwd")
>>> os.system("pwd")
'stub result'
>>> os.system("pwd")
>>> stub.restore()
>>>
>>> sinon.stub(os, "system").throws()
>>> stub = sinon.stub(os, "system").throws()
>>> try:
...     system("pwd")
... except:
...     pass
...
>>> stub.restore()