Skip to main content

Platform for test doubles in Python (mocks, stubs, fakes, and dummies)

Project description

Ludibrio

Author

Gustavo Rezende <nsigustavo@gmail.com>

Development

Ludibrio’s development may be viewed and followed on github:

http://github.com/nsigustavo/ludibrio

Retrieve the source code using ‘git’:

git clone git://github.com/nsigustavo/ludibrio.git

Tutorial

Install

$ sudo easy_install ludibrio

Mock

Mocks are objects pre-programmed with expectations which form a specification of the calls they are expected to receive.

A Mocker or Stub instance uses the ‘with’ statement to record and replay expectations:

>>> from ludibrio import Mock

>>> with Mock() as MySQLdb:
...     con = MySQLdb.connect('servidor', ' usuario', 'senha')
...     con.select_db('banco de dados') >> None
...     cursor = con.cursor()
...     cursor.execute('ALGUM SQL') >> None
...     cursor.fetchall() >> [1,2,3,4,5]
>>> con = MySQLdb.connect('servidor', ' usuario', 'senha')
>>> con.select_db('banco de dados')
>>> cursor = con.cursor()
>>> cursor.execute('ALGUM SQL')
>>> cursor.fetchall()
[1, 2, 3, 4, 5]

>>> MySQLdb.validate() #passed

Stub

Stubs provide pre-defined answers to method calls made during a test:

>>> from ludibrio import Stub

>>> with Stub() as x:
...     x.anything() >> 'response'

>>> x.anything()
'response'

Trivial mocking or stubing for any external module

Ludibrio also offers a replace mode, which basically means if a “from … import …” statement is defined into a ‘with’ scope, the replay mechanism will return a mock object to replace the original object in namespace of the whole Python interpreter (including any modules, etc). There’s a simple example below to illustrate how use it:

>>> from ludibrio import Stub

>>> with Stub() as time:
...     from time import time
...     time() >> 171

>>> from time import time
>>> time()
171

Proxy

Two Ludibrio’s powerful features that aren’t found in other mocking systems is the ability of proxying existing objects, or patching a real instance or class.

When an object is proxied, Ludibrio create a Test Double object holding a reference to the real object, allowing expressions passthrough to it(mocked or not, and by default or on request).

>>> from os.path import splitext
>>> with Stub(proxy=splitext) as splitext:
...     splitext('ludibrio/stubed.py') >> ('/temp/temp','.temp')
>>> splitext('mock.py')
('mock', '.py')
>>> splitext('ludibrio/stubed.py')
('/temp/temp', '.temp')

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

ludibrio-2.0.tar.gz (5.7 kB view details)

Uploaded Source

Built Distributions

ludibrio-2.0.linux-i686.tar.gz (14.6 kB view details)

Uploaded Source

ludibrio-2.0-py2.5.egg (11.5 kB view details)

Uploaded Source

File details

Details for the file ludibrio-2.0.tar.gz.

File metadata

  • Download URL: ludibrio-2.0.tar.gz
  • Upload date:
  • Size: 5.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for ludibrio-2.0.tar.gz
Algorithm Hash digest
SHA256 044b9f356b8bbc14612095d7a0fc3c24c006b275cf64bd6574df6fedd195d8c2
MD5 ab38774451fc1cfb4b7e4874cb0187d5
BLAKE2b-256 d7ca761d4f313bd932941e75c453b9795364a532b461043fc66e2a67778d8193

See more details on using hashes here.

File details

Details for the file ludibrio-2.0.linux-i686.tar.gz.

File metadata

File hashes

Hashes for ludibrio-2.0.linux-i686.tar.gz
Algorithm Hash digest
SHA256 9dc0d22a892c1899e32e8e4892206da119dc69186c5f309fe32dfd47883c54d5
MD5 7d5cb9e95daa4088d1dc22c2f05a55af
BLAKE2b-256 8857a56dd2b3d6174fecffceb378c2b1bc54277b026825175eee4fb434496eaa

See more details on using hashes here.

File details

Details for the file ludibrio-2.0-py2.5.egg.

File metadata

  • Download URL: ludibrio-2.0-py2.5.egg
  • Upload date:
  • Size: 11.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for ludibrio-2.0-py2.5.egg
Algorithm Hash digest
SHA256 9e4db7a8126455467768eb98030e619d11dede10f90e96a2e6bf9f61880d9f99
MD5 14a8eb4ecee839e2ce9f8234dad3afaf
BLAKE2b-256 d6307a778740d510af94269a9ce353b7f40367d88f2e4f14bb6ae5aeb3297408

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