setUp(registry=None, request=None, hook_zca=True, autocommit=True, settings=None)¶
Set Pyramid registry and request thread locals for the duration of a single unit test.
Use this function in the
setUpmethod of a unittest test case which directly or indirectly uses:
- any method of the
pyramid.config.Configuratorobject returned by this function.
If you use the
get_current_*functions (or call Pyramid code that uses these functions) without calling
pyramid.threadlocal.get_current_registry()will return a global application registry, which may cause unit tests to not be isolated with respect to registrations they perform.
None, a new empty application registry will be created (an instance of the
pyramid.registry.Registryclass). If the
registryargument is not
None, the value passed in should be an instance of the
pyramid.registry.Registryclass or a suitable testing analogue.
setUpis finished, the registry returned by the
pyramid.threadlocal.get_current_request()function will be the passed (or constructed) registry until
pyramid.testing.tearDown()is called (or
pyramid.testing.setUp()is called again) .
setUpwill attempt to perform the operation
zope.component.getSiteManager.sethook( pyramid.threadlocal.get_current_registry), which will cause the Zope Component Architecture global API (e.g.
zope.component.getAdapter(), and so on) to use the registry constructed by
setUpas the value it returns from
zope.component.getSiteManager(). If the
zope.componentpackage cannot be imported, or if
False, the hook will not be set.
settingsis not None, it must be a dictionary representing the values passed to a Configurator as its
This function returns an instance of the
pyramid.config.Configuratorclass, which can be used for further configuration to set up an environment suitable for a unit or integration test. The
registryattribute attached to the Configurator instance represents the ‘current’ application registry; the same registry will be returned by
pyramid.threadlocal.get_current_registry()during the execution of the test.
- any method of the
True(the default), call
zope.component.getSiteManager.reset(). This undoes the action of
pyramid.testing.setUp()called with the argument
zope.componentcannot be imported, ignore the argument.
DummyResource(__name__=None, __parent__=None, __provides__=None, **kw)¶
A dummy Pyramid resource object.
clone(__name__=<object object>, __parent__=<object object>, **kw)¶
Create a clone of the resource object. If
__parent__arguments are passed, use these values to override the existing
__parent__of the resource. If any extra keyword args are passed in via the
kwargument, use these keywords to add to or override existing resource keywords (attributes).
Return the items set by __setitem__
Return the keys set by __setitem__
Return the values set by __setitem__
DummyRequest(params=None, environ=None, headers=None, path='/', cookies=None, post=None, **kw)¶
A DummyRequest object (incompletely) imitates a request object.
cookiesarguments correspond to their WebOb equivalents.
postargument, if passed, populates the request’s
POSTattribute, but not
params, in order to allow testing that the app accepts data for a given view only from POST requests. This argument also sets
Extra keyword arguments are assigned as attributes of the request itself.
Note that DummyRequest does not have complete fidelity with a “real” request. For example, by default, the DummyRequest
POSTattributes are of type
dict, unlike a normal Request’s GET and POST, which are of type
MultiDict. If your code uses the features of MultiDict, you should either use a”real”
pyramid.request.Requestor adapt your DummyRequest by replacing the attributes with
Other similar incompatibilities exist. If you need all the features of a Request, use the
pyramid.request.Requestclass itself rather than this class while writing tests.
An instance of this class is returned from
pyramid.config.Configurator.testing_add_renderer(). It has a helper function (
assert_) that makes it possible to make an assertion which compares data passed to the renderer by the view function against expected key/value pairs.
Accept an arbitrary set of assertion key/value pairs. For each assertion key/value pair assert that the renderer (eg.
pyramid.renderer.render_to_response()) received the key with a value that equals the asserted value. If the renderer did not receive the key at all, or the value received by the renderer doesn’t match the assertion value, raise an