Personal tools
You are here: Home Documentation Tutorials Best Practices for Plone development Unit Testing
Support

Get Help

Join our chat rooms or support forums if you have more specific questions.

Plone Training
Learn how to design, build, and deploy a website in Plone through one of the numerous Plone training sessions around the world.
Find Plone training…
 
Document Actions

Unit Testing

One of the best investments you can make is to learn how unit testing works. Working without unit tests is at best risky, and in bigger projects - outright irresponsible.

Joel Burton

Based on the talk given by Joel Burton at the Plone Conference in Vienna, this Tutorial discusses some of the best practices for Plone development. If you are doing site development with Plone and want to keep your sanity, this is a must-read. Requires some familiarity with Zope and Plone.
Page 8 of 9.

Testing

  • PloneTestCase makes it easy
  • Trade off a bit of time for a lot of worry
    • Think broadly about the benefits

      For example, good tests will let you re-use more of your code, because you'll have the confidence to do so.

  • Look at a product with good existing tests &mdash such as ATContentTypes
  • Mechanize-driven testing for interface elements
  • Consider billing this separately to your customers

Sample Test Setup

  • Sample test setup for PloneHelpCenter:
      class PHCSiteTestCase(ArchetypesTestCase.ArcheSiteTestCase):
        """Test structure for PHC"""
    
        def afterSetUp(self):
            ArchetypesTestCase.ArcheSiteTestCase.afterSetUp(self)
    
            self._portal = self.app.portal
            # login as manager
            user = self.getManagerUser()
            newSecurityManager(None, user)
    
            self._portal.invokeFactory(type_name='HelpCenter',
              id='helpctr')
            self._hc=getattr(self._portal, 'helpctr')
    
        def beforeTearDown(self):
            ArchetypesTestCase.ArcheSiteTestCase.beforeTearDown(self)
            noSecurityManager()
            del self._hc
    

Sample Test

  • Sample test for PloneHelpCenter:
      class TestFAQ(PHCTestCase):
        """Test those parts of FAQ and FAQ Folder that don't 
        require a real site framework. Allows tests to run faster
        """
    
        def afterSetUp(self):
            PHCTestCase.afterSetUp(self)
            self._dummy = FAQ.HelpCenterFAQ(oid='dummy')
            self._dummy.initializeArchetype()
    
        def test_answerSTX(self):
            dummy = self._dummy
    
            dummy.setAnswer(example_stx, mimetype='text/structured')
            answer=no_whitespace.sub('',dummy.getAnswer())
            self.failUnless(answer==example_html, 'Value is %s' % answer)
    

More information on unit testing

  • Lots of material and tutorials available on the web
  • Get Stefan Holek's "Unit Testing Zope for Fun and Profit" presentation from EuroPython
  • A very good book is Kent Beck's Unit Testing book
 
by Joel Burton last modified December 10, 2005 - 21:43 All content is copyright Plone Foundation and the individual contributors.

For any issues with the web site functionality, please file a ticket.

Please consult the policy on plone.org content if you want your content published on this site.

Servers and hosting by