[Zope-CMF] Getting started with Unittests for BrowserViews

Charlie Clark charlie at begeistert.org
Tue Feb 24 11:18:38 EST 2009


I've finally got some time to work on my browser view for folders.  
Based on Yuppie's doctest I've started writing a unittest testsuite so  
that I can write the tests for the missing functionality and learn  
more about unittest as I go along but I'm stumbling at the first  
hurdle: what's the best way to call my view? Directly from the view  
class or as Multiadapter? If it is the latter what's the correct call  
for this as I don't seem to be able to get it right in the zope shell:

 >>> folder = site.subfolder
 >>> folder
<PortalFolder at /CMFDefault/subfolder>
 >>> getMultiAdapter((folder, request), IBrowserPublisher,  
Traceback (most recent call last):
   File "<stdin>", line 1, in ?
   File "/Users/charlieclark/CMF-Sandbox/cmf-trunk/eggs/ 
zope.component-3.4.0-py2.4.egg/zope/component/_api.py", line 103, in  
     raise ComponentLookupError(objects, interface, name)
zope.component.interfaces.ComponentLookupError: ((<PortalFolder at / 
CMFDefault/subfolder>, <zope.publisher.browser.TestRequest instance  
URL=>), <InterfaceClass  
zope.publisher.interfaces.browser.IBrowserPublisher>, u'contents.html')


 >>> from Products.CMFDefault.browser.new_folder import ContentsView
 >>> view = ContentsView(folder, request)
 >>> IBrowserPublisher.providedBy(viw)

I've noticed that many of the tests use a DummyRequest dictionary but  
using an instance of this doesn't work either.

This is the stub of my testsuite:

import unittest

from zope.component import getSiteManager
from zope.component import getMultiAdapter
from zope.publisher.browser import TestRequest

from Products.CMFCore.PortalFolder import PortalFolder
from Products.CMFCore.tests.base.dummy import DummySite
from Products.CMFCore.tests.base.dummy import DummyTool
from Products.CMFCore.interfaces import IPropertiesTool

class FolderBrowserViewTests(unittest.TestCase):

     def setUp(self):
         """Setup a site"""
         # maybe there is a base class for this?
         self.site = site = DummySite('site')
         self.sm = getSiteManager()
         mtool = site._setObject('portal_membership', DummyTool())
         ptool = site._setObject('portal_properties', DummyTool())
         self.sm.registerUtility(ptool, IPropertiesTool)
         ttool = site._setObject('portal_types', DummyTool())
         utool = site._setObject('portal_url', DummyTool())
         folder = PortalFolder('test')
         site._setObject('test', folder)
         self.context = folder

     def test_view():
         request = TestRequest()
         view = getMultiAdapter((self.context, request),  
'contents.html') # will fail

def test_suite():
     suite = unittest.TestSuite)
     return suite

I'm very grateful for any suggestions.

Charlie Clark
Helmholtzstr. 20
D- 40215
Tel: +49-211-938-5360
GSM: +49-178-782-6226

More information about the Zope-CMF mailing list