Package: infrae.testing
infrae.testingdefines sanes tests layers for testing in Zope 2, to
use in conjunction withzope.testing.ContentsAPIExamplesZCMLLayerZope2LayerCodeChanges1.3 (2012-09-03)1.2 (2011-11-07)1.1 (2010-10-07)1.0 (2010-07-14)APIIt defines:AZMLLayerwhich load a ZCML file before your test, and clean
up after,AZope2Layerwhich setup a Zope 2 test instance in a layer.You can customize the layer setup to add your tested application
(like to create a Portal instance).If you define the shell environment variableSETUP_CACHEwhile
running the tests, this last setup step will be cached in a
FileSystem Storage. This makes it reusable between different testing
sessions, and let you save a considerable amount of time while
running your tests. Of course don’t use the cache if you changed how
you installed your application.By default it provides a environment which is exactly equivalent to
the one provided by a real running Zope 2 instance (with all VHM,
session support, temp_folder …).If you compare it to the code of theTestingmodule in Zope 2,
it does the same thing, but it is simpler (and so readable), provides
an environment which match more a running one and is faster (as you can
cache the setup operation).Every test is run in a separate DemoStorage (installed on top of the
storage providing the installed application).A TestCase base class that provides a bit more of assert methods:assertTriggersEvents(*events)Context manager that will assert that the events with the given
names have been triggered.assertNotTriggersEvents(*events)Context manager that will assert that the events with the given
names have not been triggered.assertEventsAre(events, interface=None)Will assert that the event tiggered up until now are the one
with the names in the given list. You can if you want restrict
those events to the one implementing the given interface.assertContentEqual(c1, c2)Will assert that both Zope object are the same.assertContentItemsEqual(l1, l2)Will assert that both list contains the same Zope objects. The
order is not important.assertHashEqual(s1, s2)Will assert that both md5 hash of the given string are
equal. This is useful when comparing two large string, and that
you wish to have readable output error message.assertStringEqual(s1, s2)Will assert that stripped version of both given strings are the same.assertEventsAre(events, interface=None)Will assert that the triggered events are the ones whose names
are specified in the given list argument. You can restrict events
to match to those who implements at least the given interface.assertXMLEqual(xml1, xml2)Will assert that both given XML strings have the same structure
and data. The XML strings will be indented and after compared
between them. In case of differences, the error will only report
a diff between the indented XML strings.
Of course don’t use this method if you wish to compare two XML
strings where spaces matters.Anxmlindentscript can be installed out of this package
that indent the XML code exactly like theassertXMLEqualmethod does.ATestMethodsclass that will provide you access to the assert
methods without the need of a test case. This gives you access to
all the default assert methods of theunittest.TestCaseand the
ones documented above.Thesuite_from_packagefunction will construct aunittest.TestSuiteout of files found in a package. A*.txtfile will createDocFileSuite, and a*.pywill create aDocTestSuite. Other type of test files can be plugged in.If you use theZCMLLayerorZope2Layer, you have two different
callbacks registry you can use:testCleanUpthat is called after each test,layerCleanUpthat is called after each layer.Those callbacks registry have anaddmethod that can be used in
order to register a new cleanup function:from infrae.testing import testCleanUp

testCleanUp.add(my_cleanup_function)ExamplesZCMLLayerA example to test a package calledcorp.testpackage. The package
as aftesting.zcmlfile which should mainly load its ownconfigure.zcml:import unittest

from infrae.testing import ZCMLLayer
import corp.testpackage

layer = ZCMLLayer(corp.testpackage)


class MyTestCase(unittest.TestCase):

    layer = layerThe layer provides you agrok()method that can be used to grok a
module in your test later on.Zope2LayerNow ourcorp.testpackageis a Zope 2 extension providing some
content. We want to test it installed:import unittest

from infrae.testing import ZCMLLayer
import corp.testpackage


class CorpLayer(Zope2Layer):
    """Add some installation tasks to the Zope2Layer.
    """
   default_products = Zope2Layer.default_products + ['CMFCore']
   default_packages = Zope2Layer.default_packages + ['corp.testpackage']
   default_users = Zope2Layer.default_users + {'corp_user': 'CorpRole'}

   def _install_application(self, app):
      factory = app.manage_addProduct['corp.testpackage']
      factory.manage_addCorpPortal('portal')

layer = CorpLayer(corp.testpackage)


class MyTestCase(unittest.TestCase):

    layer = layer

    def setUp(self):
        self.root = self.layer.get_application()
        self.layer.login('corp_user')Of course your ZCMLmustinclude the ZCML of your package
dependencies. As well we don’t recommend to addFivein the layer
as adefault_products, it goes crazy (but include it in your ZCML
!).The layer provides you the following useful methods:login(username)Log as a the given username.logout()Logout.get_root_folder()Return the root of the database,notwrapped in a request.get_application()Return the root of the database, wrapped in a request.CodeThe code is available through a Mercurial repository at:https://hg.infrae.com/infrae.testingChanges1.3 (2012-09-03)suite_from_packageis now more generic. Other test file types can
be plugged in to this discovery function.Add two different sets of callback for cleanup: one after each test,
one after each layer. This add more possibilities than the default
set of callbacks provided by zope.testing.Refactor Zope events helpers, and test methods, to be more generic.Add a class with all the test methods defined in a testcase. This
makes possible to use the test methods without having a testcase.1.2 (2011-11-07)Update for Python 2.7 and Zope 2 2.13.1.1 (2010-10-07)Add two context managers: assertTriggersEvents and
assertNotTriggersEvents for event testing.In Zope2 request container, default hostname islocalhost, notfoo.Fix the optionSETUP_CACHEwhen reloading databases (didn’t work
because of the Zope2 Session container setup code).1.0 (2010-07-14)Initial release.