This repository has been archived by the owner on May 15, 2020. It is now read-only.
/
test_testsetup.py
192 lines (157 loc) · 5.9 KB
/
test_testsetup.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import doctest
import os
import re
import sys
import gc
import unittest
from zope.testing import cleanup, renormalizing
import zope.component.eventtesting
from z3c.testsetup.util import get_package
TESTFILES = ['basicsetup.txt',
os.path.join('functional', 'functionaldoctestsetup.txt'),
'pythontestsetup.txt',
'unitdoctestsetup.txt',
'util.txt',
'unittestsetup.txt',
os.path.join('tests', 'setupininit.txt'),
os.path.join('tests', 'util.txt'),
]
checker = renormalizing.RENormalizing([
# Relevant normalizers from zope.testing.testrunner.tests:
(re.compile(r'(\d minutes )?\d+[.]\d\d\d seconds'), 'N.NNN seconds'),
# zope.testrunner changed its output format in 4.4.0:
(re.compile(r'(Ran .*), (\d) errors and 0 skipped'), r'\1 and \2 errors'),
(re.compile(r'(Total: .*), (\d) errors and 0 skipped'), r'\1, \2 errors'),
# Our own one to work around
# http://reinout.vanrees.org/weblog/2009/07/16/invisible-test-diff.html:
(re.compile(r'.*1034h'), ''),
#windows line endings:
(re.compile('\r\n'), '\n'),
#windows drive root:
(re.compile(r'[a-zA-Z]:\\'), '/'),
#windows path separator:
(re.compile(r'\\\\'), '/'),
(re.compile(r'\\'), '/'),
])
def get_testcases_from_suite(suite):
result=[]
for elem in list(suite):
if isinstance(elem, unittest.TestCase):
result.append(elem)
if isinstance(elem, unittest.TestSuite):
result.extend(
get_testcases_from_suite(elem))
return result
def get_filenames_from_suite(suite):
testcases = get_testcases_from_suite(suite)
result = []
for testcase in testcases:
filename = str(testcase)
if ' ' in filename:
filename = str(get_package(testcase.__module__).__file__)
filename = os.path.splitext(filename)[0] + '.py'
result.append(filename)
result.sort()
return result
def get_basenames_from_suite(suite):
basenames = [os.path.basename(x) for x in get_filenames_from_suite(suite)]
basenames.sort()
return basenames
def print_file(path):
"""Prints file contents with leading bar on each line.
This way we prevent the testrunner to test the output.
"""
contents = open(path, 'r').read()
print '| ' + '\n| '.join(contents.split('\n'))
return
def setUpZope(test):
zope.component.eventtesting.setUp(test)
def cleanUpZope(test):
cleanup.cleanUp()
def testrunner_suite():
def setUp(test):
test.globs['saved-sys-info'] = (
sys.path[:],
sys.argv[:],
sys.modules.copy(),
gc.get_threshold(),
)
test.globs['this_directory'] = os.path.split(__file__)[0]
test.globs['testrunner_script'] = __file__
test.globs['get_basenames_from_suite'] = get_basenames_from_suite
test.globs['print_file'] = print_file
def tearDown(test):
sys.path[:], sys.argv[:] = test.globs['saved-sys-info'][:2]
gc.set_threshold(*test.globs['saved-sys-info'][3])
sys.modules.clear()
sys.modules.update(test.globs['saved-sys-info'][2])
suites = [
doctest.DocFileSuite(
'tests/README_OLD.txt', 'testgetter.txt',
'testrunner.txt', 'README.txt',
package='z3c.testsetup',
setUp=setUp, tearDown=tearDown,
optionflags=(doctest.ELLIPSIS|
doctest.NORMALIZE_WHITESPACE|
doctest.REPORT_NDIFF),
checker=checker),
]
suite = unittest.TestSuite(suites)
return suite
def zopeapptestingless_suite():
def setUp(test):
test.globs['saved-sys-info'] = (
sys.path[:],
sys.argv[:],
sys.modules.copy(),
gc.get_threshold(),
)
mlist = [x for x in sys.modules.keys()
if 'zope.app' in x or 'z3c.testsetup' in x]
for m in mlist:
del sys.modules[m]
plist = [x for x in sys.path if 'zope.app' in x]
for p in plist:
del sys.path[sys.path.index(p)]
test.globs['this_directory'] = os.path.split(__file__)[0]
test.globs['testrunner_script'] = __file__
test.globs['get_basenames_from_suite'] = get_basenames_from_suite
def tearDown(test):
sys.path[:], sys.argv[:] = test.globs['saved-sys-info'][:2]
gc.set_threshold(*test.globs['saved-sys-info'][3])
sys.modules.clear()
sys.modules.update(test.globs['saved-sys-info'][2])
suites = [
doctest.DocFileSuite(
'nozopeapptesting.txt',
package='z3c.testsetup',
setUp=setUp, tearDown=tearDown,
optionflags=(doctest.ELLIPSIS|
doctest.NORMALIZE_WHITESPACE|
doctest.REPORT_NDIFF),
checker=checker),
]
suite = unittest.TestSuite(suites)
return suite
def suiteFromFile(filename):
suite = unittest.TestSuite()
test = doctest.DocFileSuite(filename,
package = 'z3c.testsetup',
setUp=setUpZope,
tearDown=cleanUpZope,
globs={'get_basenames_from_suite':
get_basenames_from_suite,
'print_file': print_file},
checker=checker,
optionflags=(doctest.ELLIPSIS|
doctest.NORMALIZE_WHITESPACE|
doctest.REPORT_NDIFF))
suite.addTest(test)
return suite
def test_suite():
suite = unittest.TestSuite()
for name in TESTFILES:
suite.addTest(suiteFromFile(name))
suite.addTest(testrunner_suite())
suite.addTest(zopeapptestingless_suite())
return suite