diff options
| -rw-r--r-- | CHANGES.txt | 8 | ||||
| -rw-r--r-- | docs/api/testing.rst | 4 | ||||
| -rw-r--r-- | docs/narr/MyProject/myproject/tests.py | 2 | ||||
| -rw-r--r-- | docs/narr/unittesting.rst | 9 | ||||
| -rw-r--r-- | repoze/bfg/paster_templates/starter/+package+/tests.py_tmpl | 2 | ||||
| -rw-r--r-- | repoze/bfg/paster_templates/zodb/+package+/tests.py_tmpl | 2 | ||||
| -rw-r--r-- | repoze/bfg/testing.py | 41 | ||||
| -rw-r--r-- | repoze/bfg/tests/test_testing.py | 41 |
8 files changed, 92 insertions, 17 deletions
diff --git a/CHANGES.txt b/CHANGES.txt index 4a1d498b0..dd4638199 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -17,6 +17,14 @@ Features - Renamed the existing BFG paster template to ``bfg_starter``. Added another template showing default ZODB setup using ``repoze.zodbconn``. +- Add a method named ``assert_`` to the DummyTemplateRenderer. This + method accepts keyword arguments. Each key/value pair in the + keyword arguments causes an assertion to be made that the renderer + received this key with a value equal to the asserted value. + +- Projects generated by the paster templates now use the + ``DummyTemplateRenderer.assert_`` method in their view tests. + 0.6.1 (2009-01-06) ================== diff --git a/docs/api/testing.rst b/docs/api/testing.rst index 51b017fbf..9aebc7d30 100644 --- a/docs/api/testing.rst +++ b/docs/api/testing.rst @@ -23,4 +23,8 @@ .. autoclass:: DummyRequest :members: + .. autoclass:: DummyTemplateRenderer + :members: + + diff --git a/docs/narr/MyProject/myproject/tests.py b/docs/narr/MyProject/myproject/tests.py index 4c2f6a9ec..64314616d 100644 --- a/docs/narr/MyProject/myproject/tests.py +++ b/docs/narr/MyProject/myproject/tests.py @@ -28,7 +28,7 @@ class ViewTests(unittest.TestCase): request = testing.DummyRequest() renderer = testing.registerDummyRenderer('templates/mytemplate.pt') response = my_view(context, request) - self.assertEqual(renderer.project, 'myproject') + renderer.assert_(project='myproject') class ViewIntegrationTests(unittest.TestCase): """ These tests are integration tests for the view. These test diff --git a/docs/narr/unittesting.rst b/docs/narr/unittesting.rst index 165e92411..34a0740e9 100644 --- a/docs/narr/unittesting.rst +++ b/docs/narr/unittesting.rst @@ -62,7 +62,7 @@ unittest TestCase that used the testing API. context = testing.DummyModel() request = testing.DummyRequest() response = view_fn(context, request) - self.assertEqual(renderer.say, 'Hello') + renderer.assert_(say='Hello') def test_view_fn_submitted(self): from my.package import view_fn @@ -71,7 +71,7 @@ unittest TestCase that used the testing API. request = testing.DummyRequest() request.params['say'] = 'Yo' response = view_fn(context, request) - self.assertEqual(renderer.say, 'Yo') + renderer.assert_(say='Yo') In the above example, we create a ``MyTest`` test case that inherits from ``unittest.TestCase``. If it's in our :mod:`repoze.bfg` @@ -94,7 +94,10 @@ the dummy renderer is called, it will set attributes on itself corresponding to the non-path keyword arguments provided to the ``render_template_to_response`` function. We check that the ``say`` parameter sent into the template rendering function was ``Hello`` in -this specific example. +this specific example. The ``assert_`` method of the renderer we've +created will raise an ``AssertionError`` if the value passed to the +renderer as ``say`` does not equal ``Hello`` (any number of keyword +arguments are supported). The second test method, named ``test_view_fn_submitted`` tests the alternate case, where the ``say`` form value has already been set in diff --git a/repoze/bfg/paster_templates/starter/+package+/tests.py_tmpl b/repoze/bfg/paster_templates/starter/+package+/tests.py_tmpl index ee4050b95..b7c0eb1ef 100644 --- a/repoze/bfg/paster_templates/starter/+package+/tests.py_tmpl +++ b/repoze/bfg/paster_templates/starter/+package+/tests.py_tmpl @@ -28,7 +28,7 @@ class ViewTests(unittest.TestCase): request = testing.DummyRequest() renderer = testing.registerDummyRenderer('templates/mytemplate.pt') response = my_view(context, request) - self.assertEqual(renderer.project, '{{project}}') + renderer.assert_(project='{{project}}') class ViewIntegrationTests(unittest.TestCase): """ These tests are integration tests for the view. These test diff --git a/repoze/bfg/paster_templates/zodb/+package+/tests.py_tmpl b/repoze/bfg/paster_templates/zodb/+package+/tests.py_tmpl index ee4050b95..b7c0eb1ef 100644 --- a/repoze/bfg/paster_templates/zodb/+package+/tests.py_tmpl +++ b/repoze/bfg/paster_templates/zodb/+package+/tests.py_tmpl @@ -28,7 +28,7 @@ class ViewTests(unittest.TestCase): request = testing.DummyRequest() renderer = testing.registerDummyRenderer('templates/mytemplate.pt') response = my_view(context, request) - self.assertEqual(renderer.project, '{{project}}') + renderer.assert_(project='{{project}}') class ViewIntegrationTests(unittest.TestCase): """ These tests are integration tests for the view. These test diff --git a/repoze/bfg/testing.py b/repoze/bfg/testing.py index 94f798469..be7b5fea0 100644 --- a/repoze/bfg/testing.py +++ b/repoze/bfg/testing.py @@ -5,7 +5,7 @@ from zope.interface import implements from repoze.bfg.interfaces import IRequest -_marker = () +_marker = [] def registerDummySecurityPolicy(userid=None, groupids=(), permissive=True): """ Registers a dummy ``repoze.bfg`` security policy using the @@ -172,13 +172,50 @@ def make_traverser_factory(root): return DummyTraverserFactory class DummyTemplateRenderer: + """ + An instance of this class is returned from + ``registerDummyRenderer``. 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. + """ + + def __init__(self): + self._received = {} + def implementation(self): return self def __call__(self, **kw): - self.__dict__.update(kw) + self._received.update(kw) return '' + def __getattr__(self, k): + """ Backwards compatibiity """ + val = self._received.get(k, _marker) + if val is _marker: + raise AttributeError(k) + return val + + def assert_(self, **kw): + """ Accept an arbitrary set of assertion key/value pairs. For + each assertion key/value pair assert that the renderer + (eg. ``render_template_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 AssertionError.""" + for k, v in kw.items(): + myval = self._received.get(k, _marker) + if myval is _marker: + raise AssertionError( + 'A value for key "%s" was not passed to the renderer' % k) + + if myval != v: + raise AssertionError( + '\nasserted value for %s: %r\nactual value: %r' % ( + v, k, myval)) + return True + def make_view(result): def dummy_view(context, request): from webob import Response diff --git a/repoze/bfg/tests/test_testing.py b/repoze/bfg/tests/test_testing.py index 0dd3539df..3921a4d27 100644 --- a/repoze/bfg/tests/test_testing.py +++ b/repoze/bfg/tests/test_testing.py @@ -1,12 +1,12 @@ -from zope.component.testing import PlacelessSetup +from zope.testing.cleanup import cleanUp import unittest -class TestTestingFunctions(unittest.TestCase, PlacelessSetup): +class TestTestingFunctions(unittest.TestCase): def setUp(self): - PlacelessSetup.setUp(self) + cleanUp() def tearDown(self): - PlacelessSetup.tearDown(self) + cleanUp() def test_registerDummySecurityPolicy_permissive(self): from repoze.bfg import testing @@ -49,14 +49,12 @@ class TestTestingFunctions(unittest.TestCase, PlacelessSetup): def test_registerDummyRenderer(self): from repoze.bfg import testing - template = testing.registerDummyRenderer('templates/foo') + renderer = testing.registerDummyRenderer('templates/foo') from repoze.bfg.testing import DummyTemplateRenderer - self.failUnless(isinstance(template, DummyTemplateRenderer)) + self.failUnless(isinstance(renderer, DummyTemplateRenderer)) from repoze.bfg.chameleon_zpt import render_template_to_response response = render_template_to_response('templates/foo', foo=1, bar=2) - self.assertEqual(template.foo, 1) - self.assertEqual(template.bar, 2) - self.assertEqual(response.body, '') + self.assertEqual(dict(foo=1, bar=2), renderer._received) def test_registerEventListener_single(self): from repoze.bfg import testing @@ -345,4 +343,29 @@ class TestDummyRequest(unittest.TestCase): self.assertEqual(request.environ['PATH_INFO'], '/foo') self.assertEqual(request.water, 1) +class TestDummyTemplateRenderer(unittest.TestCase): + def _getTargetClass(self): + from repoze.bfg.testing import DummyTemplateRenderer + return DummyTemplateRenderer + + def _makeOne(self,): + return self._getTargetClass()() + + def test_implementation(self): + renderer = self._makeOne() + self.assertEqual(renderer.implementation(), renderer) + + def test_getattr(self): + renderer = self._makeOne() + renderer(a=1) + self.assertEqual(renderer.a, 1) + self.assertRaises(AttributeError, renderer.__getattr__, 'b') + + def test_assert_(self): + renderer = self._makeOne() + renderer(a=1, b=2) + self.assertRaises(AssertionError, renderer.assert_, c=1) + self.assertRaises(AssertionError, renderer.assert_, b=3) + self.failUnless(renderer.assert_(a=1, b=2)) + |
