Part 2: Plone Testing Setup

testing.py

# -*- coding: utf-8 -*-
"""Base module for unittesting."""

from plone.app.robotframework.testing import REMOTE_LIBRARY_BUNDLE_FIXTURE
from plone.app.testing import applyProfile
from plone.app.testing import FunctionalTesting
from plone.app.testing import IntegrationTesting
from plone.app.testing import PLONE_FIXTURE
from plone.app.testing import PloneSandboxLayer
from plone.testing import z2
from zope.configuration import xmlconfig

import plonetraining.testing


class PlonetrainingTestingLayer(PloneSandboxLayer):

    defaultBases = (PLONE_FIXTURE,)

    def setUpZope(self, app, configurationContext):
        print('\n ---> setUpZope \n')
        xmlconfig.file(
            'configure.zcml',
            plonetraining.testing,
            context=configurationContext
        )

    def setUpPloneSite(self, portal):
        print('\n ---> setUpPloneSite \n')
        applyProfile(portal, 'plonetraining.testing:default')

    def tearDownZope(self, app):
        print('\n ---> tearDownZope \n')

    def tearDownPloneSite(self, portal):
        print('\n ---> tearDownPloneSite \n')


PLONETRAINING_TESTING_FIXTURE = PlonetrainingTestingLayer()

PLONETRAINING_TESTING_INTEGRATION_TESTING = IntegrationTesting(
    bases=(PLONETRAINING_TESTING_FIXTURE,),
    name='PlonetrainingTestingLayer:IntegrationTesting'
)

PLONETRAINING_TESTING_FUNCTIONAL_TESTING = FunctionalTesting(
    bases=(PLONETRAINING_TESTING_FIXTURE,),
    name='PlonetrainingTestingLayer:FunctionalTesting'
)

PLONETRAINING_TESTING_ACCEPTANCE_TESTING = FunctionalTesting(
    bases=(
        PLONETRAINING_TESTING_FIXTURE,
        REMOTE_LIBRARY_BUNDLE_FIXTURE,
        z2.ZSERVER_FIXTURE
    ),
    name='PlonetrainingTestingLayer:AcceptanceTesting'
)

Testing Layers

  1. testing.py: setUpZope(self, app, configurationContext)
  2. testing.py: setUpPloneSite(self, portal)
  3. test_setup.py: setUp
  4. test_setup.py: test_product_installed
  5. test_setup.py: tearDown
  6. tearDownPloneSite(self, portal)
  7. tearDownZope(self, app)

tests/test_setup.py

# -*- coding: utf-8 -*-
"""Setup/installation tests for this package."""
from plonetraining.testing.testing import PLONETRAINING_TESTING_INTEGRATION_TESTING  # noqa
from plone import api

import unittest2 as unittest


class TestInstall(unittest.TestCase):
    """Test installation of plonetraining.testing into Plone."""

    layer = PLONETRAINING_TESTING_INTEGRATION_TESTING

    def setUp(self):
        """Custom shared utility setup for tests."""
        self.portal = self.layer['portal']
        self.installer = api.portal.get_tool('portal_quickinstaller')

    def test_product_installed(self):
        """Test if plonetraining.testing is installed with portal_quickinstaller."""
        self.assertTrue(self.installer.isProductInstalled('plonetraining.testing'))

    def test_uninstall(self):
        """Test if plonetraining.testing is cleanly uninstalled."""
        self.installer.uninstallProducts(['plonetraining.testing'])
        self.assertFalse(self.installer.isProductInstalled('plonetraining.testing'))

    # browserlayer.xml
    def test_browserlayer(self):
        """Test that IPlonetrainingTestingLayer is registered."""
        from plonetraining.testing.interfaces import IPlonetrainingTestingLayer
        from plone.browserlayer import utils
        self.assertIn(IPlonetrainingTestingLayer, utils.registered_layers())