Ejemplo n.º 1
0
    def getZCMLLayer(self, filepath, marker):
        """Create a ZCML layer out of a test marker.
        """
        zcml_file = get_marker_from_file(marker, filepath)
        if zcml_file is None:
            return
        try:
            # Late import. Some environments don't have
            # ``zope.app.testing`` available.
            from z3c.testsetup.functional.layer import DefaultZCMLLayer
        except ImportError:
            warn(
                """You specified `%s` in
    %s
but there seems to be no `zope.app.testing` package available.
Please include `zope.app.testing` in your project setup to run this testfile.
"""
                % (marker, name)
            )
        layer = DefaultZCMLLayer(
            os.path.join(os.path.dirname(filepath), zcml_file),
            DefaultZCMLLayer.__module__,
            "%s [%s]" % (DefaultZCMLLayer.__name__, os.path.join(os.path.dirname(filepath), zcml_file)),
            allow_teardown=self.allow_teardown,
        )
        return layer
Ejemplo n.º 2
0
    def getTestSuite(self):
        docfiles = self.getDocTestFiles(package=self.package)
        suite = unittest.TestSuite()
        for name in docfiles:
            layerdef = get_marker_from_file('layer', name)
            if layerdef is not None:
                layerdef = get_attribute(layerdef)

            zcml_layer = self.getZCMLLayer(name, 'zcml-layer')
            if zcml_layer is not None:
                layerdef = zcml_layer

            functional_zcml_layer = self.getZCMLLayer(
                name, 'functional-zcml-layer')
            if functional_zcml_layer is not None:
                layerdef = functional_zcml_layer

            setup = get_marker_from_file('setup', name) or self.setUp
            if setup is not None and isinstance(setup, string_types):
                setup = get_attribute(setup)

            teardown = get_marker_from_file('teardown', name) or self.tearDown
            if teardown is not None and isinstance(teardown, string_types):
                teardown = get_attribute(teardown)

            if os.path.isabs(name):
                # We get absolute pathnames, but we need relative ones...
                common_prefix = os.path.commonprefix([self.package.__file__,
                                                      name])
                name = name[len(common_prefix):]

            suite_creator = doctest.DocFileSuite
            if functional_zcml_layer is not None:
                try:
                    from zope.app.testing.functional import (
                        FunctionalDocFileSuite)
                except ImportError:
                    warn("""You specified `:functional-zcml-layer:` in
    %s
but there seems to be no `zope.app.testing` package available.
Please include `zope.app.testing` in your project setup to run this testfile.
""" % (os.path.join(common_prefix, name),))
                    continue
                suite_creator = FunctionalDocFileSuite

            # If the defined layer is a ZCMLLayer, we also enable the
            # functional test setup.
            if layerdef is not None:
                try:
                    from zope.app.testing.functional import (
                        ZCMLLayer, FunctionalDocFileSuite)
                    if isinstance(layerdef, ZCMLLayer):
                        suite_creator = FunctionalDocFileSuite
                except ImportError:
                    # If zope.app.testing is not available, the layer
                    # cannot be a ZCML layer.
                    pass

            test = suite_creator(
                name,
                package=self.package,
                setUp=setup,
                tearDown=teardown,
                globs=self.globs,
                optionflags=self.optionflags,
                checker=self.checker,
                **self.additional_options
                )
            if layerdef is not None:
                test.layer = layerdef
            suite.addTest(test)
        return suite