Welcome, guest | Sign In | My Account | Store | Cart

Notice! PyPM is being replaced with the ActiveState Platform, which enhances PyPM’s build and deploy capabilities. Create your free Platform account to download ActivePython or customize Python with the packages you require and get automatic updates.

Download
ActivePython
INSTALL>
pypm install eyam

How to install eyam

  1. Download and install ActivePython
  2. Open Command Prompt
  3. Type pypm install eyam
 Python 2.7Python 3.2Python 3.3
Windows (32-bit)
0.1 Available View build log
Windows (64-bit)
0.1 Available View build log
Mac OS X (10.5+)
0.1 Available View build log
Linux (32-bit)
0.1 Available View build log
Linux (64-bit)
0.1 Available View build log
 
Author
Imports
Lastest release
version 0.1 on Feb 29th, 2012

eyam is a Python module that provides isolation of objects, functions and methods by mocking most of the contents in a module with Mock objects (from Michael Foord's Mock library). It removes the need to individually mock all objects which the method under test depends on. After performing an action, you can inspect calls made by the code under test in the usual way with tools provided by the mock.py library.

mock is tested on Python version 2.6.

eyam is very easy to use and is designed for use with Michael Foord's Mock library.

You can mock classes, instances, functions and methods. The syntax is straightforward:

>>> from eyam import isolate
>>> import tests.isolationfixture as mod
>>> def dump(expr):
...     print '%s == %r' % (expr, eval('mod.%s' % expr))

Whole classes can be isolated:

>>> with isolate(mod, 'UnmockedClass'):  #doctest:+ELLIPSIS
...     dump('MyClass')
...     dump('UnmockedClass')
MyClass == <mock.ClassMock object at 0x...>
UnmockedClass == <class 'tests.isolationfixture.UnmockedClass'>

Instances of an isolated class work as normally. Instances of mocked classes are Mock objects, so their methods return Mock objects:

>>> with isolate(mod, 'UnmockedClass'):  #doctest:+ELLIPSIS
...     dump('MyClass().bogus()')
...     dump('UnmockedClass().instance_method()')
MyClass().bogus() == <mock.Mock object at 0x...>
UnmockedClass().instance_method() == 'return value of UnmockedClass.instance_method()'

Instances can be isolated as well:

>>> with isolate(mod, 'unmocked_instance'):  #doctest:+ELLIPSIS
...     dump('my_instance')
...     dump('unmocked_instance')
my_instance == <mock.Mock object at 0x...>
unmocked_instance == <tests.isolationfixture.MyClass object at 0x...>

Individual methods of an instance can be isolated. The instance becomes a Mock object, but the original method is re-bound to it:

>>> with isolate(mod, 'my_instance.unmocked_method'):  #doctest:+ELLIPSIS
...     dump('my_instance')
...     dump('my_instance.unmocked_method')
...     dump('my_instance.unmocked_method()')
my_instance == <mock.Mock object at 0x...>
my_instance.unmocked_method == <bound method ?.unmocked_method of <mock.Mock object at 0x...>>
my_instance.unmocked_method() == 'return value of MyClass.unmocked_method'

All other methods and attributes of the instance are mocked.

>>> with isolate(mod, 'my_instance.unmocked_method'):  #doctest:+ELLIPSIS
...     dump('my_instance.instance_method')
...     dump('my_instance.instance_method()')
my_instance.instance_method == <Mock name='mock.instance_method' id='...'>
my_instance.instance_method() == <mock.Mock object at 0x...>

Non-isolated instances become Mock objects, and their methods return Mock objects:

>>> with isolate(mod, 'my_instance.unmocked_method'):  #doctest:+ELLIPSIS
...     dump('unmocked_instance.instance_method')
...     dump('unmocked_instance.instance_method()')
unmocked_instance.instance_method == <Mock name='mock.instance_method' id='...>
unmocked_instance.instance_method() == <mock.Mock object at 0x...>

Finally, module-level functions can be isolated:

>>> with isolate(mod, 'unmocked_function'):  #doctest:+ELLIPSIS
...     dump('unmocked_function')
...     dump('unmocked_function()')
unmocked_function == <function unmocked_function at 0x...>
unmocked_function() == 'return value of unmocked_function'

Other functions are replaced with mock objects:

>>> with isolate(mod, 'unmocked_function'):  #doctest:+ELLIPSIS
...     dump('my_function')
...     dump('my_function()')
my_function == <mock.Mock object at 0x...>
my_function() == <mock.Mock object at 0x...>

The distribution contains tests and documentation. The tests require unittest2 to run.

Subscribe to package updates

Last updated Feb 29th, 2012

Download Stats

Last month:1

What does the lock icon mean?

Builds marked with a lock icon are only available via PyPM to users with a current ActivePython Business Edition subscription.

Need custom builds or support?

ActivePython Enterprise Edition guarantees priority access to technical support, indemnification, expert consulting and quality-assured language builds.

Plan on re-distributing ActivePython?

Get re-distribution rights and eliminate legal risks with ActivePython OEM Edition.