Testing with zope.componentΒΆ

zope.component is a fantastic aspect-oriented library for Python, however its unit testing support is somewhat convoluted. If you need to test code that registers adapters, utilities and the like then you may need to provide a sterile component registry. For historical reasons, component registries are known as Site Managers in zope.component.

TestFixtures provides the a TestComponents helper which provides just such a sterile registry. It should be instantiated in your TestCase‘s setUp() method. It’s uninstall() method should be called in the test’s tearDown() method.

Normally, zope.component.getSiteManager() returns whatever the current registry is. This may be influenced by frameworks that use zope.component which can means that unit tests have no baseline to start with:

>>> original = getSiteManager()
>>> print(original)
<BaseGlobalComponents base>

Once we’ve got a TestComponents in place, we know what we’re getting:

>>> components = TestComponents()
>>> getSiteManager()
<Components Testing>

The registry that getSiteManager() returns is now also available as an attribute of the TestComponents instance:

>>> getSiteManager() is components.registry
True

It’s also empty:

>>> tuple(components.registry.registeredUtilities())
()
>>> tuple(components.registry.registeredAdapters())
()
>>> tuple(components.registry.registeredHandlers())
()

You can do whatever you like with this registry. When you’re done, just call the uninstall() method:

>>> components.uninstall()

Now you’ll have the original registy back in place:

>>> getSiteManager() is original
True