betabug... Sascha Welter

home english | home deutsch | Site Map | Sascha | Kontakt | Pro | Weblog | Wiki

23 December 2007

Fix Functional Tests in Zope 2.10

Copy + Paste, my friend

A few weeks ago I introduced a new bug into the Zwiki code. A bug which made it past our tests. So I made up my mind to finally add some high level functional tests to our arsenal. Been there, done that, it's not so difficult. Then I hit the error TraversalError('No traversable adapter found', obj). It seems that the Zope 3 Page Template implementation in Zope 2.10 made it impossible to run the same functional tests on Zope 2.9 and 2.10. Which is kind of unfortunate if you happen to work on a Product that should run under multiple Zope versions. The solution is to copy+paste about 10 lines of code...

Searching the web I discovered a couple of hints at what I had to do:

In the end I had to combine philiKON's basic line, with the imports stolen from Łukasz, then I brewed it up into the form of Martin's setup (but changed to a more "normal" test setup).

Before I go through the solution in detail, I want to deliver a little rant to the Zope developers: I'm all for building modern stuff, modern programming paradigms, less boilerplate code, code reuse, etc. But (you knew there was a "but" coming) if the new stuff breaks existing code and the solution is just to copy and paste some boilerplate code that I don't even have to understand and that has not a snowball-in-hell chance to ever need to be changed / adjusted / customized, then... this new code stuff of yours just [choose your prefered expletive here] and should be considered broken.

The copy+pasted fix I'm applying here should probably be put into 2.10's ZopeTestCase itself, because then I wouldn't have the situation that my code works with only either 2.9 or 2.10 - I could leave my code alone and it would work on both Zopes (well, if I hadn't chosen to go with zope.testbrowser which isn't in 2.9's Five). But is this really the result of shiny new Interfaces, and IThis and IThat, that I have to copy+paste some blabla-code somewhere? End of rant.

So, let's go through the setup:

import unittest
import doctest

from Testing import ZopeTestCase

# ================== add these ========================
# imports copied from Lukasz mostly
    from zope import traversing, component, interface
except ImportError:
    print '--------------------------------------------'
    print 'Functional tests will only run in Zope 2.10+'
    print '--------------------------------------------'
from zope.traversing.adapters import DefaultTraversable
from zope.traversing.interfaces import ITraversable
from zope.component import provideAdapter
from zope import interface
from zope.interface import implements
# ================== back to normal setup =============

class TestZWikiFunctional(ZopeTestCase.FunctionalTestCase):
    Testing browser paths through ZWiki.
    # ================== add these ========================

    def beforeSetUp(self):
        super(ZopeTestCase.FunctionalTestCase, self).beforeSetUp()
        component.provideAdapter( \
            traversing.adapters.DefaultTraversable, \
    # ================== till here ========================

    def testSomething(self):
        # This is here, because otherwise beforeSetUp wouldn't be run
        # it's only necessary because all my tests are in the
        # FunctionalDocFileSuite - which is added afterwards

    # old-style functional tests would go here

def test_suite():
    suite = unittest.makeSuite(TestZWikiFunctional)
    # the following is only when you go with zope.testbrowser tests:
            'functional.txt', package='Products.ZWiki',
            optionflags=doctest.REPORT_ONLY_FIRST_FAILURE |
                        doctest.NORMALIZE_WHITESPACE | doctest.ELLIPSIS))
    return suite

if __name__ == "__main__":

So, if I count this right, I have to add about 10 lines of boilerplate, never to be changed code to get Functional tests running on Zope 2.10 again. This seems to be only if your Functional tests depend on Page Templates - but of course which Functional tests wouldn't depend on Page Templates?

The end result (the complete tests with the files functional.txt and will hopefully soon turn up in ZWiki source code, for the moment these links point to my private repo.

As a final note I want to say that I really enjoyed working with zope.testbrowser. It's fun to write tests like that, even though mine aren't perfect yet.

Posted by betabug at 13:01 | Comments (0) | Trackbacks (0)
ch athens
Life in Athens (Greece) for a foreigner from the other side of the mountains. And with an interest in digital life and the feeling of change in a big city. Multilingual English - German - Greek.
Main blog page
Recent Entries
Best of
Some of the most sought after posts, judging from access logs and search engine queries.

Apple & Macintosh:
Security & Privacy:
Misc technical:
Athens for tourists and visitors:
Life in general:
<< The Software Equivalent of Duct Tape + Swiss Army Knife | Main | Kneel before me, for I am the net! >>
There are no comments.
You can trackback to:
There are no trackbacks.