本文整理汇总了Python中pyramid.configuration.Configurator.hook_zca方法的典型用法代码示例。如果您正苦于以下问题:Python Configurator.hook_zca方法的具体用法?Python Configurator.hook_zca怎么用?Python Configurator.hook_zca使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pyramid.configuration.Configurator
的用法示例。
在下文中一共展示了Configurator.hook_zca方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: setUp
# 需要导入模块: from pyramid.configuration import Configurator [as 别名]
# 或者: from pyramid.configuration.Configurator import hook_zca [as 别名]
def setUp(registry=None, request=None, hook_zca=True):
"""
Set :app:`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 of the ``register*`` functions in :mod:`pyramid.testing`
(such as :func:`pyramid.testing.registerModels`)
- any method of the :class:`pyramid.configuration.Configurator`
object returned by this function.
- the :func:`pyramid.threadlocal.get_current_registry` or
:func:`pyramid.threadlocal.get_current_request` functions.
If you use the ``testing.register*`` APIs, or the
``get_current_*`` functions (or call :app:`Pyramid` code that
uses these functions) without calling ``setUp``,
:func:`pyramid.threadlocal.get_current_registry` will return a
*global* :term:`application registry`, which may cause unit tests
to not be isolated with respect to registrations they perform.
If the ``registry`` argument is ``None``, a new empty
:term:`application registry` will be created (an instance of the
:class:`pyramid.registry.Registry` class). If the ``registry``
argument is not ``None``, the value passed in should be an
instance of the :class:`pyramid.registry.Registry` class or a
suitable testing analogue.
After ``setUp`` is finished, the registry returned by the
:func:`pyramid.threadlocal.get_current_request` function will
be the passed (or constructed) registry until
:func:`pyramid.testing.tearDown` is called (or
:func:`pyramid.testing.setUp` is called again) .
If the ``hook_zca`` argument is ``True``, ``setUp`` will attempt
to perform the operation ``zope.component.getSiteManager.sethook(
pyramid.threadlocal.get_current_registry)``, which will cause
the :term:`Zope Component Architecture` global API
(e.g. :func:`zope.component.getSiteManager`,
:func:`zope.component.getAdapter`, and so on) to use the registry
constructed by ``setUp`` as the value it returns from
:func:`zope.component.getSiteManager`. If the
:mod:`zope.component` package cannot be imported, or if
``hook_zca`` is ``False``, the hook will not be set.
This function returns an instance of the
:class:`pyramid.configuration.Configurator` class, which can be
used for further configuration to set up an environment suitable
for a unit or integration test. The ``registry`` attribute
attached to the Configurator instance represents the 'current'
:term:`application registry`; the same registry will be returned
by :func:`pyramid.threadlocal.get_current_registry` during the
execution of the test.
.. warning:: Although this method of setting up a test registry
will never disappear, after :app:`Pyramid` 1.0,
using the ``begin`` and ``end`` methods of a
``Configurator`` are preferred to using
``pyramid.testing.setUp`` and
``pyramid.testing.tearDown``. See
:ref:`unittesting_chapter` for more information.
"""
manager.clear()
if registry is None:
registry = Registry("testing")
config = Configurator(registry=registry)
if hasattr(registry, "registerUtility"):
# Sometimes nose calls us with a non-registry object because
# it thinks this function is module test setup. Likewise,
# someone may be passing us an esoteric "dummy" registry, and
# the below won't succeed if it doesn't have a registerUtility
# method.
from pyramid.configuration import DEFAULT_RENDERERS
for name, renderer in DEFAULT_RENDERERS:
# Cause the default renderers to be registered because
# in-the-wild test code relies on being able to call
# e.g. ``pyramid.chameleon_zpt.render_template``
# without registering a .pt renderer, expecting the "real"
# template to be rendered. This is a holdover from when
# individual template system renderers weren't indirected
# by the ``pyramid.renderers`` machinery, and
# ``render_template`` and friends went behind the back of
# any existing renderer factory lookup system.
config.add_renderer(name, renderer)
hook_zca and config.hook_zca()
config.begin(request=request)
return config