[z3-five] testings event firing

Philipp von Weitershausen philipp at weitershausen.de
Wed Feb 7 17:39:35 CET 2007


Chris Withers wrote:
> This starts of with me wanting to test that I've inserted the correct 
> calls to event.notify in my code.
> 
> So, the natural way would be to have a dummy subscriber, and then 
> exercise the code where event.notify should be present, right?

Sure, or simply use an already existing setup from 
zope.component.eventtesting. There are a few tests (also in Five, I 
think) that demonstrate how to use it.

> Okay, but if I just do:
> 
> class mySubscriber:
> 
>    def __init__(self):
>      self.events = []
> 
>    def __call__(self,*args,**kw):
>      self.events.append((args,kw))
> 
> s = mySubscriber()
> provideHandler(s,IWhatever)
> 
> ....then that handler will get called for every event in the test run, 
> right?

Yup.

> How do I get rid of it once I'm done with a particular test?
> 
> I tried playing with subclassing zope.component.testing.PlacelessSetup 
> but that trashed all my normal zcml setup.

Well, yeah, of course it does. PlacelessSetup is a bad name and it's 
actually nowadays just a small shim for zope.testing.cleanup.CleanUp. 
"CleanUp" says it all: it cleans up after each test. Completely. No 
compromises. That's important for unit tests.

So, the question is, are you writing a fucntional test or a unit test? 
In a unit test, you might not actually want to (or have to!) load all of 
your ZCML, but only register those components necessary for the test. 
And then it's not so much of a problem that everything gets torn down 
after each test (in fact, you'd rather like that in a unit test).

In a functional test, you load the ZCML once for a whole bunch of tests 
and then execute all your tests within that setup. Individual tests 
shouldn't really modify the registries then. The common way to do this 
is to use zope.testing's test layer support. You put those tests that 
require common setup routines that may take long to load (e.g. booting 
up all of the ZCML you have) into one layer and let the layer do that 
setup (and tear down). zope.testing has some doctests on layers.

> I guess a secondary question then becomes how do you unit test that the 
> assignments in your .zcml files are correct?

That's not a unit test, that's an integration / functional test.

> Is this the realm of functional tests? If so, how do you run functional 
> and unit tests in the same run? Are there any docs or examples of all this?

Five has unit tests and funtional tests, though they are sometimes hard 
to distinguish because

a) even some unit tests do their setup by calling ZCML, which is mostly 
overkill and slows them down

b) functional tests do their own setup and don't rely on a common setup 
layer like Zope 3 functional tests do.

For Zope 2.11 I'd like to refactor Five's functional tests to use a 
common setup layer (one that simply bootstraps Five's ZCML) and cleanup 
unit tests not to use ZCML to make them even more light-weight.


-- 
http://worldcookery.com -- Professional Zope documentation and training
Next Zope 3 training at Camp5: http://trizpug.org/boot-camp/camp5


More information about the z3-five mailing list