summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChris McDonough <chrism@plope.com>2012-09-15 19:33:54 -0400
committerChris McDonough <chrism@plope.com>2012-09-15 19:33:54 -0400
commit97150cc540bad56f8ff7760616d75db8894da73b (patch)
treec1ae5b10d93350b1a763eecdb3857d4aa7af92b6
parenta98f0cf435e31864e632d9105ffdda30aa776212 (diff)
parentd6f0fd56fb93d30d8d7d6b490a57967d940b0135 (diff)
downloadpyramid-97150cc540bad56f8ff7760616d75db8894da73b.tar.gz
pyramid-97150cc540bad56f8ff7760616d75db8894da73b.tar.bz2
pyramid-97150cc540bad56f8ff7760616d75db8894da73b.zip
merge jinty's testConfig code
-rw-r--r--CHANGES.txt3
-rw-r--r--CONTRIBUTORS.txt3
-rw-r--r--docs/api/testing.rst2
-rw-r--r--docs/narr/testing.rst24
-rw-r--r--pyramid/testing.py35
-rw-r--r--pyramid/tests/test_testing.py55
6 files changed, 122 insertions, 0 deletions
diff --git a/CHANGES.txt b/CHANGES.txt
index 94c331cef..da2399d9b 100644
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@ -163,6 +163,9 @@ Features
- The ``pyramid.decorator.reify`` function is now an API, and was added to
the API documentation.
+- Added the ``pyramid.testing.testConfig`` context manager, which can be used
+ to generate a configurator in a test, e.g. ``with testing.testConfig(...):``.
+
Deprecations
------------
diff --git a/CONTRIBUTORS.txt b/CONTRIBUTORS.txt
index bb11006f3..e2a6ec018 100644
--- a/CONTRIBUTORS.txt
+++ b/CONTRIBUTORS.txt
@@ -177,6 +177,8 @@ Contributors
- Marc Abramowitz, 2012/06/13
+- Brian Sutherland, 2012/06/16
+
- Jeff Cook, 2012/06/16
- Ian Wilson, 2012/06/17
@@ -184,3 +186,4 @@ Contributors
- Roman Kozlovskyi, 2012/08/11
- Domen Kozar, 2012/09/11
+
diff --git a/docs/api/testing.rst b/docs/api/testing.rst
index f388dc263..1366a1795 100644
--- a/docs/api/testing.rst
+++ b/docs/api/testing.rst
@@ -9,6 +9,8 @@
.. autofunction:: tearDown
+ .. autofunction:: testConfig(registry=None, request=None, hook_zca=True, autocommit=True, settings=None)
+
.. autofunction:: cleanUp
.. autoclass:: DummyResource
diff --git a/docs/narr/testing.rst b/docs/narr/testing.rst
index 50e9d5604..20017064b 100644
--- a/docs/narr/testing.rst
+++ b/docs/narr/testing.rst
@@ -157,6 +157,30 @@ We use a "dummy" request implementation supplied by
:class:`pyramid.testing.DummyRequest` because it's easier to construct
than a "real" :app:`Pyramid` request object.
+Test setup using a context manager
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+An alternative style of setting up a test configuration is to use the
+`with` statement and :func:`pyramid.testing.testConfig` to create a
+context manager. The context manager will call
+:func:`pyramid.testing.setUp` before the code under test and
+:func:`pyramid.testing.tearDown` afterwards.
+
+This style is useful for small self-contained tests. For example:
+
+.. code-block:: python
+ :linenos:
+
+ import unittest
+
+ class MyTest(unittest.TestCase):
+
+ def test_my_function(self):
+ from pyramid import testing
+ with testing.testConfig() as config:
+ config.add_route('bar', '/bar/{id}')
+ my_function_which_needs_route_bar()
+
What?
~~~~~
diff --git a/pyramid/testing.py b/pyramid/testing.py
index e091bac4f..9e8f2bff3 100644
--- a/pyramid/testing.py
+++ b/pyramid/testing.py
@@ -1,5 +1,6 @@
import copy
import os
+from contextlib import contextmanager
from zope.interface import (
implementer,
@@ -572,3 +573,37 @@ def skip_on(*platforms): # pragma: no cover
return wrapper
return decorator
skip_on.os_name = os.name # for testing
+
+@contextmanager
+def testConfig(registry=None,
+ request=None,
+ hook_zca=True,
+ autocommit=True,
+ settings=None):
+ """Returns a context manager for test set up.
+
+ This context manager calls :func:`pyramid.testing.testSetup` when
+ entering and :func:`pyramid.testing.tearDown` when exiting.
+
+ All arguments are passed directly to :func:`pyramid.testing.testSetup`.
+ If the ZCA is hooked, it will always be un-hooked in tearDown.
+
+ This context manager allows you to write test code like this:
+
+ .. code-block:: python
+ :linenos:
+
+ with testConfig() as config:
+ config.add_route('bar', '/bar/{id}')
+ req = DummyRequest()
+ resp = myview(req),
+ """
+ config = setUp(registry=registry,
+ request=request,
+ hook_zca=hook_zca,
+ autocommit=autocommit,
+ settings=settings)
+ try:
+ yield config
+ finally:
+ tearDown(unhook_zca=hook_zca)
diff --git a/pyramid/tests/test_testing.py b/pyramid/tests/test_testing.py
index 339a39cd8..7f14462a2 100644
--- a/pyramid/tests/test_testing.py
+++ b/pyramid/tests/test_testing.py
@@ -646,3 +646,58 @@ class DummyRendererInfo(object):
def __init__(self, kw):
self.__dict__.update(kw)
+class Test_testConfig(unittest.TestCase):
+
+ def _setUp(self, **kw):
+ self._log.append(('setUp', kw))
+ return 'fake config'
+
+ def _tearDown(self, **kw):
+ self._log.append(('tearDown', kw))
+
+ def setUp(self):
+ from pyramid import testing
+ self._log = []
+ self._orig_setUp = testing.setUp
+ testing.setUp = self._setUp
+ self._orig_tearDown = testing.tearDown
+ testing.tearDown = self._tearDown
+
+ def tearDown(self):
+ from pyramid import testing
+ testing.setUp = self._orig_setUp
+ testing.tearDown = self._orig_tearDown
+
+ def _callFUT(self, inner, **kw):
+ from pyramid.testing import testConfig
+ with testConfig(**kw) as config:
+ inner(config)
+
+ def test_ok_calls(self):
+ self.assertEqual(self._log, [])
+ def inner(config):
+ self.assertEqual(self._log,
+ [('setUp',
+ {'autocommit': True,
+ 'hook_zca': True,
+ 'registry': None,
+ 'request': None,
+ 'settings': None})])
+ self._log.pop()
+ self._callFUT(inner)
+ self.assertEqual(self._log,
+ [('tearDown', {'unhook_zca': True})])
+
+ def test_teardown_called_on_exception(self):
+ class TestException(Exception):
+ pass
+ def inner(config):
+ self._log = []
+ raise TestException('oops')
+ self.assertRaises(TestException, self._callFUT, inner)
+ self.assertEqual(self._log[0][0], 'tearDown')
+
+ def test_ok_get_config(self):
+ def inner(config):
+ self.assertEqual(config, 'fake config')
+ self._callFUT(inner)