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