pyramid.testing
¶
- setUp(registry=None, request=None, hook_zca=True, autocommit=True, settings=None, package=None)[source]¶
Set Pyramid registry and request thread locals for the duration of a single unit test.
Use this function in the
setUp
method of a unittest test case which directly or indirectly uses:any method of the
pyramid.config.Configurator
object returned by this function.the
pyramid.threadlocal.get_current_registry()
orpyramid.threadlocal.get_current_request()
functions.
If you use the
get_current_*
functions (or call Pyramid code that uses these functions) without callingsetUp
,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.If the
registry
argument isNone
, a new empty application registry will be created (an instance of thepyramid.registry.Registry
class). If theregistry
argument is notNone
, the value passed in should be an instance of thepyramid.registry.Registry
class or a suitable testing analogue.After
setUp
is finished, the registry returned by thepyramid.threadlocal.get_current_registry()
function will be the passed (or constructed) registry untilpyramid.testing.tearDown()
is called (orpyramid.testing.setUp()
is called again) .If the
hook_zca
argument isTrue
,setUp
will attempt to perform the operationzope.component.getSiteManager.sethook( pyramid.threadlocal.get_current_registry)
, which will cause the Zope Component Architecture global API (e.g.zope.component.getSiteManager()
,zope.component.getAdapter()
, and so on) to use the registry constructed bysetUp
as the value it returns fromzope.component.getSiteManager()
. If thezope.component
package cannot be imported, or ifhook_zca
isFalse
, the hook will not be set.If
settings
is notNone
, it must be a dictionary representing the values passed to a Configurator as itssettings=
argument.If
package
isNone
it will be set to the caller's package. Thepackage
setting in thepyramid.config.Configurator
will affect any relative imports made viapyramid.config.Configurator.include()
orpyramid.config.Configurator.maybe_dotted()
.This function returns an instance of the
pyramid.config.Configurator
class, which can be used for further configuration to set up an environment suitable for a unit or integration test. Theregistry
attribute attached to the Configurator instance represents the 'current' application registry; the same registry will be returned bypyramid.threadlocal.get_current_registry()
during the execution of the test.
- tearDown(unhook_zca=True)[source]¶
Undo the effects of
pyramid.testing.setUp()
. Use this function in thetearDown
method of a unit test that usespyramid.testing.setUp()
in itssetUp
method.If the
unhook_zca
argument isTrue
(the default), callzope.component.getSiteManager.reset()
. This undoes the action ofpyramid.testing.setUp()
when called with the argumenthook_zca=True
. Ifzope.component
cannot be imported,unhook_zca
is set toFalse
.
- testConfig(registry=None, request=None, hook_zca=True, autocommit=True, settings=None)[source]¶
Returns a context manager for test set up.
This context manager calls
pyramid.testing.setUp()
when entering andpyramid.testing.tearDown()
when exiting.All arguments are passed directly to
pyramid.testing.setUp()
. If the ZCA is hooked, it will always be un-hooked in tearDown.This context manager allows you to write test code like this:
1with testConfig() as config: 2 config.add_route('bar', '/bar/{id}') 3 req = DummyRequest() 4 resp = myview(req)
- cleanUp(*arg, **kw)[source]¶
An alias for
pyramid.testing.setUp()
.
- class DummyResource(__name__=None, __parent__=None, __provides__=None, **kw)[source]¶
A dummy Pyramid resource object.
- clone(__name__=<object object>, __parent__=<object object>, **kw)[source]¶
Create a clone of the resource object. If
__name__
or__parent__
arguments are passed, use these values to override the existing__name__
or__parent__
of the resource. If any extra keyword args are passed in via thekw
argument, use these keywords to add to or override existing resource keywords (attributes).
- class DummyRequest(params=None, environ=None, headers=None, path='/', cookies=None, post=None, accept=None, **kw)[source]¶
A DummyRequest object (incompletely) imitates a request object.
The
params
,environ
,headers
,path
, andcookies
arguments correspond to their WebOb equivalents.The
post
argument, if passed, populates the request'sPOST
attribute, but notparams
, in order to allow testing that the app accepts data for a given view only from POST requests. This argument also setsself.method
to "POST".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
GET
andPOST
attributes are of typedict
, unlike a normal Request's GET and POST, which are of typeMultiDict
. If your code uses the features of MultiDict, you should either use a realpyramid.request.Request
or adapt your DummyRequest by replacing the attributes withMultiDict
instances.Other similar incompatibilities exist. If you need all the features of a Request, use the
pyramid.request.Request
class itself rather than this class while writing tests.- request_iface¶
alias of
IRequest
- class DummyTemplateRenderer(string_response='')[source]¶
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.- assert_(**kw)[source]¶
Accept an arbitrary set of assertion key/value pairs. For each assertion key/value pair assert that the renderer (eg.
pyramid.renderers.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 anAssertionError
.