Пример #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
Пример #2
0
 def test_get_marker_from_file_non_utf8(self):
     # even files incompatible with UTF-8 can be parsed
     path = os.path.join(self.workdir, "myfile")
     content = b"Line1\n :some-layer:   foo \n Strange: \xff\xfeW[ \n"
     with open(path, "wb") as fd:
         fd.write(content)
     result = get_marker_from_file("some-layer", path)
     self.assertEqual(result, "foo")
Пример #3
0
 def test_get_marker_from_file(self):
     # we can find markers in files
     path = os.path.join(self.workdir, "myfile")
     content = "Some text\n :TeSt-lAyEr:   foo \n\nSome other text\n"
     with open(path, "w") as fd:
         fd.write(content)
     result = get_marker_from_file("test-layer", path)
     self.assertEqual(result, "foo")
Пример #4
0
 def test_get_marker_from_file_utf8(self):
     #  we can get markers from files with non-ascii encodings
     path = os.path.join(self.workdir, "myfile")
     content = u"Line1\n :some-layer:   foo \n Umlauts: äöü\n"
     content = content.encode("utf-8")  # transform to binary stream
     with open(path, "wb") as fd:
         fd.write(content)
     result = get_marker_from_file("some-layer", path)
     self.assertEqual(result, "foo")
Пример #5
0
 def isTestFile(self, filepath):
     """Return ``True`` if a file matches our expectations for a
     doctest file.
     """
     if os.path.splitext(filepath)[1].lower() not in self.extensions:
         return False
     if os.path.basename(filepath).startswith('.'):
         # Ignore *nix hidden files
         return False
     if get_marker_from_file('doctest', filepath) is None:
         return False
     return True
Пример #6
0
 def suiteFromFile(self, name):
     suite = unittest.TestSuite()
     layer = get_marker_from_file('Test-Layerdef', name)
     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):]
     test = FunctionalDocFileSuite(
         name, package=self.package,
         setUp=self.setUp, tearDown=self.tearDown,
         globs=self.globs,
         optionflags=self.optionflags,
         encoding=self.encoding,
         checker=self.checker,
         **self.additional_options
         )
     test.layer = self.layer
     if layer is not None:
         test.layer = layer
     suite.addTest(test)
     return suite
Пример #7
0
 def getTestSuite(self):
     docfiles = self.getDocTestFiles(package=self.package)
     suite = unittest.TestSuite()
     for name in docfiles:
         layerdef = get_marker_from_file("Test-Layerdef", name)
         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) :]
         test = doctest.DocFileSuite(
             name,
             package=self.package,
             setUp=self.setUp,
             tearDown=self.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
Пример #8
0
    def isTestModule(self, module_info):
        """Return ``True`` if a module matches our expectations for a
        test file.

        This is the case if it got a module docstring which matches
        each of our regular expressions.
        """
        # Do not even try to load modules, that have no marker string.
        if not self.fileContains(module_info.path):
            # No ":test-layer: python" marker, so check for :unittest:.
            if get_marker_from_file('unittest', module_info.path) is None:
                # Neither the old nor the new marker: this is no test module.
                return False
        module = None
        try:
            module = module_info.getModule()
        except ImportError:
            # Broken module that is probably a test.  We absolutely have to
            # warn about this!
            print("Import error in %s" % module_info.path)
        docstr = getattr(module, '__doc__', '')
        if not self.docstrContains(docstr):
            return False
        return True
Пример #9
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