I like to propose the following addition to 1.0/Testing (http://docs.turbogears.org/1.0/Testing). I think the described problems may affect many beginners when they try to use unit tests. As the mentioned wiki page is marked as "official", I can not change the text myself.

===============================================================================
Issues with CherryPy filters
----------------------------

When testing your application you should be aware of some issues which can bite
you. The main problem is that ``testutil.call(..., ...)`` won't trigger any
CherryPy filters. These filters modify the originally requested object or the
request parameters. The main difference between filters and function decorators
is that filters will be executed on all exposed methods while decorators are
specific for the decorated functionality.

Some important functionality in TurboGears is implemented as a CherryPy filter,
especially visit tracking and identity authentication. Therefore you can not
test your login page easily. In order to test other methods which rely on
identity for user authorization, there is a special method ``set_identity_user``
in ``turbogears.testutil`` which can be used to set a user before using
``testutil.call(...)``.

Another catch may be the
`NestedVariablesFilter <http://www.formencode.org/Validator.html#http-html-form-input>`_
which is implemented as a filter too. You can either pass the nested dictionary
to you method under test or use ``NestedVariables.to_python(...)`` manually
before actually calling the real method.

::

      from formencode.variabledecode import NestedVariables
      from turbogears import testutil

      # first method
      args = {'phone': [{'nr': '12345', 'area_code': '042'},
                        {'nr': '6789', 'area_code': '021'}]
              'name': 'Foo'}
      result = testutil.call(self.root.save, **args)

      # second method
      args = {'phone-1.nr': '12345', 'phone-1.area_code': '042',
              'phone-2.nr': '6789', 'phone-2.area_code': '021',
              'name': 'Foo'}
      args = NestedVariables.to_python(args)
      result =  testutil.call(self.root.save, **args)


Note you can avoid these problems by using ``testutil.create_request(...)``
because this will create a faked request which goes through all stages
of CherryPy's request processing as mentioned above. The disadvantage is that
you will get only the generated output from your template engine so you can't
easily extract all variables passed to the template.
===============================================================================

Attachment: smime.p7s
Description: S/MIME Cryptographic Signature

Reply via email to