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 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
29 """
30 A modified TestSuite that provides hooks for startUp and tearDown methods.
31 """
32 - def run(self, result):
36
39
42
43
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
75
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
92
94 self.login()
95 self.globals['sync']()
96 testObject.globs.update(self.globals)
97
99 self.logout()
100 testObject.globs['abort']()
101 self.globals['sync']()
102
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
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
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