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.

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 — 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