Mock unit test case based on ``mocker``
Project description
Introduction
============
LEGACY: Do not use this any longer, better use the Python mock module!
This package contains a unittest test class based on the one from the
``Mocker`` mock library (http://labix.org/mocker).
This class provides support for registering Zope 3 components (utilities,
adapters, subscription adapters and event handlers) from mocks and tearing
down the global component registry during test tear-down.
There are also a few convenience methods and parameter checkers that are
useful to Zope and Plone testing.
Please see the Mocker documentation for more detail:
http://labix.org/mocker
A test case that mocks a utility may look like this::
from plone.mocktestcase import MockTestCase
from my.package.interfaces import IMyInterface
from my.package.foo import testable_method
class MyTestCase(MockTestCase):
def test_something(self):
utility_mock = self.mocker.mock()
self.expect(utility_mock.do_something()).result("foo")
self.mock_utility(utility_mock, IMyInterface)
# Put mocker into replay mode
self.replay()
# Verify that testable_method() looks up a utility for
# IMyInterface and calls do_something() on it, which returns
# "foo".
testable_method()
The following helper methods are available:
self.replay()
Puts the mock into replay mode.
self.create_dummy(**kw)
Return a dummy object that is *not* a mock object, just a dumb object
with whatever attributes or methods you pass as keyword arguments.
To make a dummy method, pass a function object or a lambda, e.g.
self.create_dummy(id="foo", absolute_url=lambda:'http://example.org/foo')
self.mock_utility(mock, provides, name=u"")
Register the given mock object as a global utility providing the given
interface, with the given name (defaults to the unnamed default utility).
self.mock_adapter(mock, provides, adapts, name=u"")
Register the given mock object as a global adapter providing the given
interface and adapting the given interfaces, with the given name
(defaults to the unnamed default adapter).
self.mock_subscription_adapter(mock, provides, adapts)
Register the given mock object as a global subscription adapter providing
the given interface and adapting the given interfaces.
self.mock_handler(mock, adapts)
Register the given mock object as a global event subscriber for the
given event types.
self.mock_tool(mock, name)
Create a getToolByName() mock (using 'replace' mode) and configure it so
that code calling getToolByName(context, name) obtains the given mock
object. Can be used multiple times: the getToolByName() mock is created
lazily the first time this method is called in any one test fixture.
self.match_provides(interface)
A custom matcher that can be used to check whether an argument to a mock
call provides the given interface. Uses interface.providedBy(arg).
self.match_type(type)
A custom matcher that can be used to check whether an argument to a mock
call is if the given type. Uses isinstance(arg, type).
Changelog
=========
1.0 (2017-01-13)
----------------
- Add MANIFEST.in.
[timo]
- fix to work with zope interface after https://github.com/zopefoundation/zope.interface/commit/79a266200384fa53fb3810ed316a94394166f4e3
[jensens]
- Fix relative import to fix Python 3 compatibility.
[datakurre]
1.0b3
-----
* Added mock_tool() helper.
1.0b2
-----
* Minor amendments to mock Zope 3 component support
1.0b1
-----
* Initial release
============
LEGACY: Do not use this any longer, better use the Python mock module!
This package contains a unittest test class based on the one from the
``Mocker`` mock library (http://labix.org/mocker).
This class provides support for registering Zope 3 components (utilities,
adapters, subscription adapters and event handlers) from mocks and tearing
down the global component registry during test tear-down.
There are also a few convenience methods and parameter checkers that are
useful to Zope and Plone testing.
Please see the Mocker documentation for more detail:
http://labix.org/mocker
A test case that mocks a utility may look like this::
from plone.mocktestcase import MockTestCase
from my.package.interfaces import IMyInterface
from my.package.foo import testable_method
class MyTestCase(MockTestCase):
def test_something(self):
utility_mock = self.mocker.mock()
self.expect(utility_mock.do_something()).result("foo")
self.mock_utility(utility_mock, IMyInterface)
# Put mocker into replay mode
self.replay()
# Verify that testable_method() looks up a utility for
# IMyInterface and calls do_something() on it, which returns
# "foo".
testable_method()
The following helper methods are available:
self.replay()
Puts the mock into replay mode.
self.create_dummy(**kw)
Return a dummy object that is *not* a mock object, just a dumb object
with whatever attributes or methods you pass as keyword arguments.
To make a dummy method, pass a function object or a lambda, e.g.
self.create_dummy(id="foo", absolute_url=lambda:'http://example.org/foo')
self.mock_utility(mock, provides, name=u"")
Register the given mock object as a global utility providing the given
interface, with the given name (defaults to the unnamed default utility).
self.mock_adapter(mock, provides, adapts, name=u"")
Register the given mock object as a global adapter providing the given
interface and adapting the given interfaces, with the given name
(defaults to the unnamed default adapter).
self.mock_subscription_adapter(mock, provides, adapts)
Register the given mock object as a global subscription adapter providing
the given interface and adapting the given interfaces.
self.mock_handler(mock, adapts)
Register the given mock object as a global event subscriber for the
given event types.
self.mock_tool(mock, name)
Create a getToolByName() mock (using 'replace' mode) and configure it so
that code calling getToolByName(context, name) obtains the given mock
object. Can be used multiple times: the getToolByName() mock is created
lazily the first time this method is called in any one test fixture.
self.match_provides(interface)
A custom matcher that can be used to check whether an argument to a mock
call provides the given interface. Uses interface.providedBy(arg).
self.match_type(type)
A custom matcher that can be used to check whether an argument to a mock
call is if the given type. Uses isinstance(arg, type).
Changelog
=========
1.0 (2017-01-13)
----------------
- Add MANIFEST.in.
[timo]
- fix to work with zope interface after https://github.com/zopefoundation/zope.interface/commit/79a266200384fa53fb3810ed316a94394166f4e3
[jensens]
- Fix relative import to fix Python 3 compatibility.
[datakurre]
1.0b3
-----
* Added mock_tool() helper.
1.0b2
-----
* Minor amendments to mock Zope 3 component support
1.0b1
-----
* Initial release
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
plone.mocktestcase-1.0.tar.gz
(10.3 kB
view details)
File details
Details for the file plone.mocktestcase-1.0.tar.gz
.
File metadata
- Download URL: plone.mocktestcase-1.0.tar.gz
- Upload date:
- Size: 10.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5bf5e814b889c992db113b0170bf2c919bdf0e07de3665c92c9da5b0b77494b9 |
|
MD5 | 747f336b38c6eced9a1315150093a989 |
|
BLAKE2b-256 | da321b8b1c596ad8015b9793c51970e76e162ecfeb7acf943d0fbc6281dc2f7c |