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

Source Code for Module 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   
 21  from AccessControl.SecurityManagement import newSecurityManager 
 22  from AccessControl.SecurityManagement import noSecurityManager 
 23   
 24  from Products.ZenUtils.ZeoConn import ZeoConn 
 25   
 26   
27 -class TestSuiteWithHooks(unittest.TestSuite):
28 """ 29 A modified TestSuite that provides hooks for startUp and tearDown methods. 30 """
31 - def run(self, result):
32 self.startUp() 33 unittest.TestSuite.run(self, result) 34 self.tearDown()
35
36 - def startUp(self):
37 pass
38
39 - def tearDown(self):
40 pass
41 42
43 -class ZenDocTestRunner(object):
44 """ 45 Extracts doctests from the docstrings of a Zenoss module 46 and runs them in an environment similar to that of zendmd. 47 48 Example usage: 49 zdtr = ZenDocTestRunner() 50 zdtr.add_modules("Products.ZenModel.ZenModelBase") 51 zdtr.run() 52 """ 53 54 modules = [] 55 conn = None 56
57 - def setUp(self):
58 if not self.conn: self.conn = ZeoConn() 59 self.app = self.conn.app 60 self.login() 61 self.dmd = self.app.zport.dmd 62 find = self.dmd.Devices.findDevice 63 self.globals = dict( 64 app = self.app, 65 zport = self.app.zport, 66 dmd = self.dmd, 67 find = find, 68 devices = self.dmd.Devices, 69 sync = self.dmd._p_jar.sync, 70 commit = transaction.commit, 71 abort = transaction.abort, 72 me = find(socket.getfqdn()) 73 )
74
75 - def tearDown(self):
76 self.logout() 77 self.conn.closedb()
78
79 - def login(self, name='admin', userfolder=None):
80 '''Logs in.''' 81 if userfolder is None: 82 userfolder = self.app.acl_users 83 user = userfolder.getUserById(name) 84 if user is None: return 85 if not hasattr(user, 'aq_base'): 86 user = user.__of__(userfolder) 87 newSecurityManager(None, user)
88
89 - def logout(self):
90 noSecurityManager()
91
92 - def doctest_setUp(self, testObject):
93 self.login() 94 self.globals['sync']() 95 testObject.globs.update(self.globals)
96
97 - def doctest_tearDown(self, testObject):
98 self.logout() 99 testObject.globs['abort']() 100 self.globals['sync']()
101
102 - def add_modules(self, mods):
103 """ 104 Add Zenoss modules to be tested. 105 106 @param mods: One or more module objects or dotted names. 107 @type mods: module or list 108 """ 109 if type(mods)!=type([]): mods = [mods] 110 self.modules.extend(mods)
111
112 - def get_suites(self):
113 """ 114 Returns a doctest.DocTestSuite for each module 115 in self.modules. 116 117 Provided for integration with existing unittest framework. 118 """ 119 self.setUp() 120 doctest.DocTestFinder(exclude_empty=True) 121 suites = [] 122 for mod in self.modules: 123 try: 124 dtsuite = doctest.DocTestSuite( 125 mod, 126 optionflags=doctest.NORMALIZE_WHITESPACE, 127 setUp = self.doctest_setUp, 128 tearDown = self.doctest_tearDown 129 ) 130 except ValueError: 131 pass 132 else: 133 suites.append(dtsuite) 134 return suites
135
136 - def run(self):
137 """ 138 Run the doctests found in the modules added to this instance. 139 140 This method sets up the zendmd global variables, creates a 141 test suite for each module that has been added, and runs 142 all suites. 143 """ 144 suite = unittest.TestSuite() 145 for dtsuite in self.get_suites(): 146 suite.addTest(dtsuite) 147 runner = unittest.TextTestRunner() 148 runner.run(suite) 149 self.tearDown()
150