1
2
3
4
5
6
7
8
9
10
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
28 """
29 A modified TestSuite that provides hooks for startUp and tearDown methods.
30 """
31 - def run(self, result):
35
38
41
42
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
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
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
91
93 self.login()
94 self.globals['sync']()
95 testObject.globs.update(self.globals)
96
98 self.logout()
99 testObject.globs['abort']()
100 self.globals['sync']()
101
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
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
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