Back to index

plone3  3.1.7
doctestcase.py
Go to the documentation of this file.
00001 ################################################################################
00002 #
00003 # Copyright (c) 2002-2005, Benjamin Saller <bcsaller@ideasuite.com>, and
00004 #                              the respective authors. All rights reserved.
00005 # For a list of Archetypes contributors see docs/CREDITS.txt.
00006 #
00007 # Redistribution and use in source and binary forms, with or without
00008 # modification, are permitted provided that the following conditions are met:
00009 #
00010 # * Redistributions of source code must retain the above copyright notice, this
00011 #   list of conditions and the following disclaimer.
00012 # * Redistributions in binary form must reproduce the above copyright notice,
00013 #   this list of conditions and the following disclaimer in the documentation
00014 #   and/or other materials provided with the distribution.
00015 # * Neither the name of the author nor the names of its contributors may be used
00016 #   to endorse or promote products derived from this software without specific
00017 #   prior written permission.
00018 #
00019 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
00020 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00021 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
00022 # FOR A PARTICULAR PURPOSE.
00023 #
00024 ################################################################################
00025 """Doc test suite for doc tests inside Zope 2 or Portal
00026 
00027 Example:
00028 DOCTESTS = (
00029     'dotted.path.to.a.module.as.string',
00030     'another.module.path',
00031     )
00032 
00033 def test_suite():
00034     suite = ZopeDocTestSuite(test_class=TestClass,
00035                              extraglobs={},
00036                              *DOCTESTS
00037                              )
00038     return suite
00039 
00040 
00041 """
00042 __author__ = 'Christian Heimes'
00043 __docformat__ = 'restructuredtext'
00044 
00045 ##
00046 # ZopeDocTestSuite
00047 ##
00048 import warnings
00049 import unittest
00050 
00051 from Testing.ZopeTestCase import TestCase
00052 from Testing.ZopeTestCase import ZopeTestCase
00053 from Testing.ZopeTestCase import doctest
00054 
00055 # assign __module__ var to ExtensionClass - otherwise doctest import may fail
00056 import ExtensionClass
00057 try:
00058     ExtensionClass.Base.__module__ = ExtensionClass
00059     ExtensionClass.ExtensionClass.__module__ = ExtensionClass
00060 except TypeError:
00061     # fails with Zope 2.8 on. Probably also not really needed then.
00062     pass
00063 
00064 def ZopeDocTestSuite(*modules, **kw):
00065     """Based on Sid's FunctionalDocFileSuite
00066     """
00067     test_class = kw.get('test_class', ZopeTestCase)
00068     suite = kw.get('suite', None)
00069     suite_class = kw.get('suite_class', unittest.TestSuite)
00070 
00071     for var in ('test_class', 'suite', 'suite_class',):
00072         if var in kw:
00073             del kw[var]
00074 
00075     # Fix for http://zope.org/Collectors/Zope/2178
00076     if hasattr(test_class, 'layer'):
00077         layer = test_class.layer
00078     else:
00079         layer = None
00080 
00081     # If the passed-in test_class doesn't subclass base.TestCase,
00082     # we mix it in for you, but we will issue a warning.
00083     if not issubclass(test_class, TestCase):
00084         name = test_class.__name__
00085         warnings.warn(("The test_class you are using doesn't "
00086                        "subclass from base.TestCase. "
00087                        "Please fix that."), UserWarning, 2)
00088         if not 'ZDT' in name:
00089             name = 'ZDT%s' % name
00090         test_class = type(name, (TestCase, test_class), {})
00091 
00092     # If the test_class does not have a runTest attribute, 
00093     # we add one.
00094     #if not hasattr(test_class, 'runTest'):
00095     setattr(test_class, 'runTest', None)
00096 
00097     # Create a TestCase instance which will be used
00098     # to execute the setUp and tearDown methods, as well as
00099     # be passed into the test globals as 'self'.
00100     test_instance = test_class()
00101 
00102     # setUp
00103     kwsetUp = kw.get('setUp')
00104     def setUp(test):
00105         test_instance.setUp()
00106         test.globs['test'] = test
00107         test.globs['self'] = test_instance
00108         test.globs['app'] = test_instance.app
00109         if hasattr(test_instance, 'portal'):
00110             test.globs['portal'] = test_instance.portal
00111         if kwsetUp is not None:
00112             kwsetUp(test_instance)
00113 
00114     kw['setUp'] = setUp
00115     
00116     # tearDown
00117     kwtearDown = kw.get('tearDown')
00118     def tearDown(test):
00119         if kwtearDown is not None:
00120             kwtearDown(test_instance)
00121         test_instance.tearDown()
00122 
00123     kw['tearDown'] = tearDown
00124     
00125     # other options
00126     if 'optionflags' not in kw:
00127         kw['optionflags'] = (doctest.ELLIPSIS
00128                              | doctest.NORMALIZE_WHITESPACE)
00129 
00130     if suite is None:
00131         suite = suite_class()
00132 
00133     for module in modules:
00134         suite.addTest(doctest.DocTestSuite(module, **kw))
00135 
00136     if layer is not None:
00137         suite.layer = layer
00138 
00139     return suite
00140 
00141 __all__ = ('ZopeDocTestSuite',)