summaryrefslogtreecommitdiff
path: root/docs/narr
diff options
context:
space:
mode:
authorChris McDonough <chrism@agendaless.com>2008-11-08 22:40:37 +0000
committerChris McDonough <chrism@agendaless.com>2008-11-08 22:40:37 +0000
commita9a8a2dcfa3381ee9da3550f84dcadd55825e1d4 (patch)
treeef5a6b4f61bd34dc5b96b80b43f75dac607b3951 /docs/narr
parenteabf3b6fae0714f305f5786c042f19734293fb0c (diff)
downloadpyramid-a9a8a2dcfa3381ee9da3550f84dcadd55825e1d4.tar.gz
pyramid-a9a8a2dcfa3381ee9da3550f84dcadd55825e1d4.tar.bz2
pyramid-a9a8a2dcfa3381ee9da3550f84dcadd55825e1d4.zip
Get rid of BFGTestCase base class: use only functions.
Diffstat (limited to 'docs/narr')
-rw-r--r--docs/narr/unittesting.rst84
1 files changed, 49 insertions, 35 deletions
diff --git a/docs/narr/unittesting.rst b/docs/narr/unittesting.rst
index 8724e4a87..00db66e96 100644
--- a/docs/narr/unittesting.rst
+++ b/docs/narr/unittesting.rst
@@ -10,15 +10,14 @@ test cases easier to write. The facilities become particularly useful
when your code calls into :mod:`repoze.bfg` -related framework
functions.
-The ``BFGTestCase`` Base Class
-------------------------------
+Writing A Unit Test
+-------------------
-The ``repoze.bfg.testing`` module provides a class named
-``BFGTestCase`` which you can use as a base class for unittest test
-classes.
+The ``repoze.bfg.testing`` module provides a number of functions which
+can be used during unit testing. An example of a unit test class that
+uses these functions is below.
-.. code-block:: python
- :linenos:
+.. code-block:: python :linenos:
def view_fn(context, request):
from repoze.bfg.chameleon_zpt import render_template_to_response
@@ -27,48 +26,63 @@ classes.
say=request.params['say'])
return render_template_to_response('templates/show.pt', say='Hello')
- from repoze.bfg.testing import BFGTestCase
+ import unittest
+ from zope.testing.cleanup import cleanUp
+ from.repoze.bfg import testing
- class MyTest(BFGTestCase):
+ class MyTest(unittest.TestCase):
+ def setUp(self):
+ cleanUp()
+
+ def tearDown(self):
+ cleanUp()
+
def test_view_fn_not_submitted(self):
- template = self.registerDummyTemplate('templates/show.pt')
- request = self.makeRequest()
- context = self.makeModel()
+ renderer = testing.registerTemplateRenderer('templates/show.pt')
+ context = testing.DummyModel()
+ request = testing.DummyRequest()
response = view_fn(context, request)
- self.assertEqual(template.say, 'Hello')
+ self.assertEqual(renderer.say, 'Hello')
def test_view_fn_submitted(self):
- template = self.registerDummyTemplate('templates/submitted.pt')
- request = self.makeRequest()
+ renderer = testing.registerTemplateRenderer('templates/submitted.pt')
+ context = testing.DummyModel()
+ request = testing.DummyRequest()
request.params['say'] = 'Yo'
- context = self.makeModel()
response = view_fn(context, request)
- self.assertEqual(template.say, 'Yo')
+ self.assertEqual(renderer.say, 'Yo')
In the above example, we create a ``MyTest`` test case that inherits
-from ``BFGTestCase``. It has two test methods. The first test
-method, ``test_view_fn_not_submitted`` tests the ``view_fn`` function
-in the case that no "form" values (represented by request.params) have
-been submitted. Its first line registers a "dummy template" named
-``templates/show.pt`` via the ``registerDummyTemplate`` method (a
-``BFGTestCase`` API); this function returns a DummyTemplate instance
-which we hang on to for later. We then call ``makeRequest`` to get a
-DummyRequest object, and ``makeModel`` to get a DummyModel object. We
-call the function being tested with the manufactured context and
-request. When the function is called, ``render_template_to_response``
-will call the "dummy" template object instead of the real template
+from ``unittest.TestCase``. It has two test methods.
+
+The first test method, ``test_view_fn_not_submitted`` tests the
+``view_fn`` function in the case that no "form" values (represented by
+request.params) have been submitted. Its first line registers a
+"dummy template renderer" named ``templates/show.pt`` via the
+``registerTemplateRenderer`` function (a ``repoze.bfg.testing`` API);
+this function returns a DummyTemplateRenderer instance which we hang
+on to for later. We then call ``DummyRequest`` to get a dummy request
+object, and ``DummyModel`` to get a dummy context object. We call the
+function being tested with the manufactured context and request. When
+the function is called, ``render_template_to_response`` will call the
+"dummy" template renderer object instead of the real template renderer
object. When it's 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. The second test, named
-``test_view_fn_submitted`` tests the alternate case, where the ``say``
-form value has already been set in the request and performs a similar
-template registration and assertion.
+this specific example.
+
+The second test method, named ``test_view_fn_submitted`` tests the
+alternate case, where the ``say`` form value has already been set in
+the request and performs a similar template registration and
+assertion.
+
+Note that the test calls the ``zope.testing.cleanup.cleanUp`` function
+in its ``setUp`` and ``tearDown`` functions. This is required to
+perform cleanup between the test runs. If you use any of the testing
+API, be sure to call this function at setup and teardown of individual
+tests.
See the :ref:`testing_module` chapter for the entire
:mod:`repoze.bfg` -specific testing API.
-The ``BFGTestCase`` class inherits from ``unittest.TestCase``, so it
-will be found by test finders.
-