[Checkins] SVN: Sandbox/ulif/grok-with-testing/src/grok/ First plain implementation of grok.testing with examples in grok.admin.

Uli Fouquet uli at gnufix.de
Mon Aug 13 11:10:04 EDT 2007


Log message for revision 78789:
  First plain implementation of grok.testing with examples in grok.admin.

Changed:
  U   Sandbox/ulif/grok-with-testing/src/grok/__init__.py
  A   Sandbox/ulif/grok-with-testing/src/grok/admin/Another.txt
  A   Sandbox/ulif/grok-with-testing/src/grok/admin/app.py
  A   Sandbox/ulif/grok-with-testing/src/grok/admin/ftests.py
  U   Sandbox/ulif/grok-with-testing/src/grok/meta.py
  A   Sandbox/ulif/grok-with-testing/src/grok/testing.py

-=-
Modified: Sandbox/ulif/grok-with-testing/src/grok/__init__.py
===================================================================
--- Sandbox/ulif/grok-with-testing/src/grok/__init__.py	2007-08-13 14:41:44 UTC (rev 78788)
+++ Sandbox/ulif/grok-with-testing/src/grok/__init__.py	2007-08-13 15:10:03 UTC (rev 78789)
@@ -38,6 +38,7 @@
 from grok.directive import (context, name, template, templatedir, provides,
                             baseclass, global_utility, local_utility,
                             define_permission, require, site)
+from grok.testing import (file,)
 from grok._grok import do_grok as grok  # Avoid name clash within _grok
 from grok._grok import grok_component
 from grok._grok import SubscribeDecorator as subscribe

Added: Sandbox/ulif/grok-with-testing/src/grok/admin/Another.txt
===================================================================
--- Sandbox/ulif/grok-with-testing/src/grok/admin/Another.txt	                        (rev 0)
+++ Sandbox/ulif/grok-with-testing/src/grok/admin/Another.txt	2007-08-13 15:10:03 UTC (rev 78789)
@@ -0,0 +1,13 @@
+This is just a silly example test. We make sure, the test fails.
+
+   >>> a = 1
+   >>> b = 1
+   >>> a + b
+   3
+
+Ooh, this was not the question.
+
+   >>> 5*7
+   42
+
+Ah! Found it!
\ No newline at end of file

Added: Sandbox/ulif/grok-with-testing/src/grok/admin/app.py
===================================================================
--- Sandbox/ulif/grok-with-testing/src/grok/admin/app.py	                        (rev 0)
+++ Sandbox/ulif/grok-with-testing/src/grok/admin/app.py	2007-08-13 15:10:03 UTC (rev 78789)
@@ -0,0 +1,23 @@
+"""Silly example code to show usage of grok.testing:
+"""
+
+import grok
+
+class SampleApp(grok.Application, grok.Container):
+    pass
+
+class MyTest0(grok.testing.FunctionalDocTest):
+    """A test without file."""
+
+class MyTest1(grok.testing.FunctionalDocTest):
+    """A test with one file."""
+    grok.testing.file('README.txt')
+
+class MyTest2(grok.testing.FunctionalDocTest):
+    """A test with two files."""
+    grok.testing.file('README.txt')
+    grok.testing.file('Another.txt')
+
+class MyTest3(grok.testing.FunctionalDocTest):
+    # This is currently not supported...
+    grok.context(SampleApp)

Added: Sandbox/ulif/grok-with-testing/src/grok/admin/ftests.py
===================================================================
--- Sandbox/ulif/grok-with-testing/src/grok/admin/ftests.py	                        (rev 0)
+++ Sandbox/ulif/grok-with-testing/src/grok/admin/ftests.py	2007-08-13 15:10:03 UTC (rev 78789)
@@ -0,0 +1,12 @@
+import grok
+import unittest
+
+def test_suite():
+    import grok.admin
+    # We must grok our package to get the test registrations...
+    grok.grok('grok.admin')
+    return grok.testing.test_suite()
+
+
+if __name__ == '__main__':
+    unittest.main(defaultTest='test_suite')

Modified: Sandbox/ulif/grok-with-testing/src/grok/meta.py
===================================================================
--- Sandbox/ulif/grok-with-testing/src/grok/meta.py	2007-08-13 14:41:44 UTC (rev 78788)
+++ Sandbox/ulif/grok-with-testing/src/grok/meta.py	2007-08-13 15:10:03 UTC (rev 78789)
@@ -630,3 +630,24 @@
         intids = IntIds()
         setupUtility(site, intids, IIntIds)
         return intids
+
+class FunctionalDocTestGrokker(martian.ClassGrokker):
+    component_class = grok.testing.FunctionalDocTest
+
+    def grok(self, name, factory, context, module_info, templates):
+        docfilelist = getattr(factory, '__grok_testing_file__', [])
+        for docfile in docfilelist:
+            docfilepath = module_info.getResourcePath(docfile)
+            if not os.path.isfile(docfilepath):
+                raise GrokError(
+                    "Doctest file '%r' declared in %r does not exist "
+                    "in %r." %
+                    (docfile, module_info.dotted_name,
+                     module_info.getResourcePath('')),
+                    None)
+            if not hasattr(factory, '__grok_testing_filepath__'):
+                factory.__grok_testing_filepath__ = []
+            factory.__grok_testing_filepath__.append(docfilepath)
+            grok.testing.add_functional_doctest(factory)
+        return True
+

Added: Sandbox/ulif/grok-with-testing/src/grok/testing.py
===================================================================
--- Sandbox/ulif/grok-with-testing/src/grok/testing.py	                        (rev 0)
+++ Sandbox/ulif/grok-with-testing/src/grok/testing.py	2007-08-13 15:10:03 UTC (rev 78789)
@@ -0,0 +1,100 @@
+import grok
+import unittest
+import os.path
+
+from martian.error import GrokImportError
+from martian.directive import (MultipleTimesDirective, BaseTextDirective,
+                               SingleValue, SingleTextDirective,
+                               MultipleTextDirective,
+                               MarkerDirective,
+                               InterfaceDirective,
+                               InterfaceOrClassDirective,
+                               ModuleDirectiveContext,
+                               ClassDirectiveContext,
+                               ClassOrModuleDirectiveContext)
+from martian import util
+#from martian.scan import ModuleInfo
+
+from pkg_resources import resource_listdir
+from zope.testing import doctest
+from zope.app.testing.functional import (HTTPCaller, getRootFolder,
+                                         FunctionalTestSetup, sync, ZCMLLayer,
+                                         FunctionalDocFileSuite)
+
+
+
+
+class FunctionalDocTest(object):
+    """A functional doc test, that will automatically executed.
+
+    """
+
+    ftesting_zcml = os.path.join(os.path.dirname(grok.__file__),
+                                 'ftesting.zcml')
+    FunctionalLayer = ZCMLLayer(ftesting_zcml, __name__,
+                                'FunctionalLayer')
+    
+    def __init__(self):
+        # print "FUNCDOCTEST CREATED: ", self
+        pass
+    
+    def setUp(self, test):
+        FunctionalTestSetup().setUp()
+
+    def tearDown(self, test):
+        return True
+        FunctionalTestSetup().tearDown()
+
+    def suiteFromFile(self, filepath):
+        suite = unittest.TestSuite()
+        # We must get the pkg of the target test...
+        pkg = self.__module__.rsplit('.', 1)[0].replace('.', '/')
+            
+        test = FunctionalDocFileSuite(
+            filepath, setUp=self.setUp, tearDown=self.tearDown,
+            module_relative = True, package = pkg,
+            globs = dict(http=HTTPCaller(),
+                   getRootFolder=getRootFolder,
+                   sync=sync
+                   ),
+            optionflags = (doctest.ELLIPSIS+
+                           doctest.NORMALIZE_WHITESPACE+
+                           doctest.REPORT_NDIFF)
+            )
+        test.layer = self.FunctionalLayer
+        suite.addTest(test)
+        return suite
+        
+    def test_suite(self):
+        suite = unittest.TestSuite()
+        for name in getattr(self, '__grok_testing_file__', []):
+            suite.addTest(self.suiteFromFile(name))
+        return suite
+        
+
+# Setup for the grok.testing.file directive...
+all_func_doc_tests = []
+
+def add_functional_doctest(doctest):
+    all_func_doc_tests.append(doctest)
+
+class TestingFileDirective(MultipleTextDirective):
+    def check_arguments(self, filename = None):
+        if filename is None or filename == '':
+            raise GrokImportError("You must give a valid filename when using "
+                                  "grok.testing.file() (invalid: %s)." % self.filename)
+
+file = TestingFileDirective('grok.testing.file',
+                            ClassOrModuleDirectiveContext())
+
+
+def test_suite():
+
+    suite = unittest.TestSuite()
+    for klass in all_func_doc_tests:
+        ftest = klass()
+        suite.addTest(ftest.test_suite())
+    return suite
+
+if __name__ == '__main__':
+    unittest.main(defaultTest='test_suite')



More information about the Checkins mailing list