Package Products :: Package ZenUtils :: Module ZenDocTest
[hide private]
[frames] | no frames]

Source Code for Module Products.ZenUtils.ZenDocTest

  1  ########################################################################### 
  2  # 
  3  # This program is part of Zenoss Core, an open source monitoring platform. 
  4  # Copyright (C) 2007, Zenoss Inc. 
  5  # 
  6  # This program is free software; you can redistribute it and/or modify it 
  7  # under the terms of the GNU General Public License version 2 as published by 
  8  # the Free Software Foundation. 
  9  # 
 10  # For complete information please visit: http://www.zenoss.com/oss/ 
 11  # 
 12  ########################################################################### 
 13   
 14  import unittest 
 15  import doctest 
 16  import transaction 
 17  import socket 
 18  import Globals 
 19   
 20  import zope.component 
 21  from zope.traversing.adapters import DefaultTraversable 
 22   
 23  from AccessControl.SecurityManagement import newSecurityManager 
 24  from AccessControl.SecurityManagement import noSecurityManager 
 25   
 26  from Products.ZenUtils.ZeoConn import ZeoConn 
 27   
 28   
29 -class TestSuiteWithHooks(unittest.TestSuite):
30 """ 31 A modified TestSuite that provides hooks for startUp and tearDown methods. 32 """
33 - def run(self, result):
34 self.startUp() 35 unittest.TestSuite.run(self, result) 36 self.tearDown()
37
38 - def startUp(self):
39 pass
40
41 - def tearDown(self):
42 pass
43 44
45 -class ZenDocTestRunner(object):
46 """ 47 Extracts doctests from the docstrings of a Zenoss module 48 and runs them in an environment similar to that of zendmd. 49 50 Example usage: 51 zdtr = ZenDocTestRunner() 52 zdtr.add_modules("Products.ZenModel.ZenModelBase") 53 zdtr.run() 54 """ 55 56 modules = [] 57 conn = None 58
59 - def setUp(self):
60 zope.component.provideAdapter(DefaultTraversable, (None,)) 61 if not self.conn: self.conn = ZeoConn() 62 self.app = self.conn.app 63 self.login() 64 self.dmd = self.app.zport.dmd 65 find = self.dmd.Devices.findDevice 66 self.globals = dict( 67 app = self.app, 68 zport = self.app.zport, 69 dmd = self.dmd, 70 find = find, 71 devices = self.dmd.Devices, 72 sync = self.dmd._p_jar.sync, 73 commit = transaction.commit, 74 abort = transaction.abort, 75 me = find(socket.getfqdn()) 76 )
77
78 - def tearDown(self):
79 self.logout() 80 self.conn.closedb()
81
82 - def login(self, name='admin', userfolder=None):
83 '''Logs in.''' 84 if userfolder is None: 85 userfolder = self.app.acl_users 86 user = userfolder.getUserById(name) 87 if user is None: return 88 if not hasattr(user, 'aq_base'): 89 user = user.__of__(userfolder) 90 newSecurityManager(None, user)
91
92 - def logout(self):
93 noSecurityManager()
94
95 - def doctest_setUp(self, testObject):
96 self.login() 97 self.globals['sync']() 98 testObject.globs.update(self.globals)
99
100 - def doctest_tearDown(self, testObject):
101 self.logout() 102 testObject.globs['abort']() 103 self.globals['sync']()
104
105 - def add_modules(self, mods):
106 """ 107 Add Zenoss modules to be tested. 108 109 @param mods: One or more module objects or dotted names. 110 @type mods: module or list 111 """ 112 if type(mods)!=type([]): mods = [mods] 113 self.modules.extend(mods)
114
115 - def get_suites(self):
116 """ 117 Returns a doctest.DocTestSuite for each module 118 in self.modules. 119 120 Provided for integration with existing unittest framework. 121 """ 122 self.setUp() 123 doctest.DocTestFinder(exclude_empty=True) 124 suites = [] 125 for mod in self.modules: 126 try: 127 dtsuite = doctest.DocTestSuite( 128 mod, 129 optionflags=doctest.NORMALIZE_WHITESPACE, 130 setUp = self.doctest_setUp, 131 tearDown = self.doctest_tearDown 132 ) 133 except ValueError: 134 pass 135 else: 136 suites.append(dtsuite) 137 return suites
138
139 - def run(self):
140 """ 141 Run the doctests found in the modules added to this instance. 142 143 This method sets up the zendmd global variables, creates a 144 test suite for each module that has been added, and runs 145 all suites. 146 """ 147 suite = unittest.TestSuite() 148 for dtsuite in self.get_suites(): 149 suite.addTest(dtsuite) 150 runner = unittest.TextTestRunner() 151 runner.run(suite) 152 self.tearDown()
153