summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Merickel <michael@merickel.org>2014-05-05 23:25:11 -0500
committerMichael Merickel <michael@merickel.org>2014-05-05 23:25:11 -0500
commit8f22d846af8faba71a2e20e0fb9da52ec4ec5a5e (patch)
tree023b727a9745e7d3e66db403564e10d60d735aac
parent50d2b85e5e2795c2b129a86e8d388909fbc2835f (diff)
parentd24055e3ec0c09974f180a459143676ba457a32f (diff)
downloadpyramid-8f22d846af8faba71a2e20e0fb9da52ec4ec5a5e.tar.gz
pyramid-8f22d846af8faba71a2e20e0fb9da52ec4ec5a5e.tar.bz2
pyramid-8f22d846af8faba71a2e20e0fb9da52ec4ec5a5e.zip
Merge pull request #1322 from Pylons/fix.setUp-relative-package
modify p.testing.setUp() to configure the package relative to caller
-rw-r--r--CHANGES.txt5
-rw-r--r--pyramid/testing.py20
-rw-r--r--pyramid/tests/test_testing.py5
3 files changed, 27 insertions, 3 deletions
diff --git a/CHANGES.txt b/CHANGES.txt
index 2b80e87e2..de0a4324b 100644
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@ -12,6 +12,11 @@ Bug Fixes
add_route_predicate for example can not take a string and turn it into the
actual callable function.
+- Fix ``pyramid.testing.setUp`` to return a ``Configurator`` with a proper
+ package. Previously it was not possible to do package-relative includes
+ using the returned ``Configurator`` during testing. There is now a
+ ``package`` argument that can override this behavior as well.
+
Docs
----
diff --git a/pyramid/testing.py b/pyramid/testing.py
index 91dc41dd5..8cbd8b82b 100644
--- a/pyramid/testing.py
+++ b/pyramid/testing.py
@@ -21,6 +21,7 @@ from pyramid.compat import (
from pyramid.config import Configurator
from pyramid.decorator import reify
+from pyramid.path import caller_package
from pyramid.response import Response
from pyramid.registry import Registry
@@ -388,7 +389,7 @@ class DummyRequest(
have_zca = True
def setUp(registry=None, request=None, hook_zca=True, autocommit=True,
- settings=None):
+ settings=None, package=None):
"""
Set :app:`Pyramid` registry and request thread locals for the
duration of a single unit test.
@@ -432,9 +433,15 @@ def setUp(registry=None, request=None, hook_zca=True, autocommit=True,
:mod:`zope.component` package cannot be imported, or if
``hook_zca`` is ``False``, the hook will not be set.
- If ``settings`` is not None, it must be a dictionary representing the
+ If ``settings`` is not ``None``, it must be a dictionary representing the
values passed to a Configurator as its ``settings=`` argument.
+ If ``package`` is ``None`` it will be set to the caller's package. The
+ ``package`` setting in the :class:`pyramid.config.Configurator` will
+ affect any relative imports made via
+ :meth:`pyramid.config.Configurator.include` or
+ :meth:`pyramid.config.Configurator.maybe_dotted`.
+
This function returns an instance of the
:class:`pyramid.config.Configurator` class, which can be
used for further configuration to set up an environment suitable
@@ -447,7 +454,10 @@ def setUp(registry=None, request=None, hook_zca=True, autocommit=True,
manager.clear()
if registry is None:
registry = Registry('testing')
- config = Configurator(registry=registry, autocommit=autocommit)
+ if package is None:
+ package = caller_package()
+ config = Configurator(registry=registry, autocommit=autocommit,
+ package=package)
if settings is None:
settings = {}
if getattr(registry, 'settings', None) is None:
@@ -505,6 +515,10 @@ def tearDown(unhook_zca=True):
def cleanUp(*arg, **kw):
""" An alias for :func:`pyramid.testing.setUp`. """
+ package = kw.get('package', None)
+ if package is None:
+ package = caller_package()
+ kw['package'] = package
return setUp(*arg, **kw)
class DummyRendererFactory(object):
diff --git a/pyramid/tests/test_testing.py b/pyramid/tests/test_testing.py
index da57c6301..2d0548b33 100644
--- a/pyramid/tests/test_testing.py
+++ b/pyramid/tests/test_testing.py
@@ -347,6 +347,7 @@ class Test_setUp(unittest.TestCase):
self.assertEqual(config.registry, current['registry'])
self.assertEqual(current['registry'].__class__, Registry)
self.assertEqual(current['request'], None)
+ self.assertEqual(config.package.__name__, 'pyramid.tests')
self._assertSMHook(get_current_registry)
def test_it_with_registry(self):
@@ -364,6 +365,10 @@ class Test_setUp(unittest.TestCase):
current = manager.get()
self.assertEqual(current['request'], request)
+ def test_it_with_package(self):
+ config = self._callFUT(package='pyramid')
+ self.assertEqual(config.package.__name__, 'pyramid')
+
def test_it_with_hook_zca_false(self):
from pyramid.registry import Registry
registry = Registry()