35
34
@param testpaths: If provided, only tests in the given sequence will
36
35
be considered. If not provided, all tests are
38
@return: (unittests, doctests) tuple, with lists of unittests and
39
doctests found, respectively.
37
@return: a test suite containing the requested tests.
39
suite = unittest.TestSuite()
41
40
topdir = os.path.abspath(os.path.dirname(__file__))
42
41
testdir = os.path.dirname(tests.__file__)
43
42
testpaths = set(testpaths)
46
43
for root, dirnames, filenames in os.walk(testdir):
47
44
for filename in filenames:
48
45
filepath = os.path.join(root, filename)
64
61
if filename.endswith(".py"):
65
unittests.append(relpath)
66
elif relpath == os.path.join("tests", "zope", "README.txt"):
67
# Special case the inclusion of the Zope-dependent
69
from tests.zope import has_zope
71
doctests.append(relpath)
62
modpath = relpath.replace(os.path.sep, ".")[:-3]
63
module = __import__(modpath, None, None, [""])
65
unittest.defaultTestLoader.loadTestsFromModule(module))
72
66
elif filename.endswith(".txt"):
73
doctests.append(relpath)
68
if relpath == os.path.join("tests", "zope", "README.txt"):
69
# Special case the inclusion of the Zope-dependent
71
from tests.zope import has_zope
74
parent_path = os.path.dirname(relpath).replace(
76
parent_module = __import__(parent_path, None, None, [""])
77
suite.addTest(doctest.DocFileSuite(
78
os.path.basename(relpath),
80
package=parent_module,
81
optionflags=doctest.ELLIPSIS))
75
return unittests, doctests
78
86
def parse_sys_argv():
89
def test_with_trial():
90
from twisted.scripts import trial
91
unittests, doctests = find_tests(parse_sys_argv())
92
sys.argv.extend(unittests)
96
def test_with_py_test():
98
dirname = os.path.dirname(__file__)
99
unittests, doctests = find_tests(parse_sys_argv())
100
sys.argv.extend(unittests)
101
sys.argv.extend(doctests)
102
# For timestamp checking when looping:
103
sys.argv.append(os.path.join(os.path.dirname(__file__), "storm/"))
104
py.test.cmdline.main()
107
def test_with_unittest():
96
def test_with_runner(runner):
109
97
usage = "test.py [options] [<test filename>, ...]"
111
99
parser = optparse.OptionParser(usage=usage)
113
101
parser.add_option('--verbose', action='store_true')
114
102
opts, args = parser.parse_args()
116
runner = unittest.TextTestRunner()
119
105
runner.verbosity = 2
121
loader = unittest.TestLoader()
123
unittests, doctests = find_tests(args)
127
self.total_failures = 0
128
self.total_errors = 0
130
def __call__(self, tests, failures, errors):
131
self.total_tests += tests
132
self.total_failures += failures
133
self.total_errors += errors
134
sys.stderr.write("(tests=%d, failures=%d, errors=%d)\n" %
135
(tests, failures, errors))
137
unittest_summary = Summary()
138
doctest_summary = Summary()
141
sys.stderr.write("Running unittests...\n")
142
for relpath in unittests:
143
sys.stderr.write("[%s]\n" % relpath)
144
modpath = relpath.replace(os.path.sep, '.')[:-3]
145
module = __import__(modpath, None, None, [""])
146
test = loader.loadTestsFromModule(module)
147
result = runner.run(test)
148
unittest_summary(test.countTestCases(),
149
len(result.failures), len(result.errors))
150
sys.stderr.write("\n")
153
sys.stderr.write("Running doctests...\n")
154
doctest_flags = doctest.ELLIPSIS
155
for relpath in doctests:
156
sys.stderr.write("[%s]\n" % relpath)
157
failures, total = doctest.testfile(relpath,
158
optionflags=doctest_flags)
159
doctest_summary(total, failures, 0)
160
sys.stderr.write("\n")
162
sys.stderr.write("Total test cases: %d\n" % unittest_summary.total_tests)
163
sys.stderr.write("Total doctests: %d\n" % doctest_summary.total_tests)
164
sys.stderr.write("Total failures: %d\n" %
165
(unittest_summary.total_failures +
166
doctest_summary.total_failures))
167
sys.stderr.write("Total errors: %d\n" % (unittest_summary.total_errors +
168
doctest_summary.total_errors))
170
failed = bool(unittest_summary.total_failures or
171
unittest_summary.total_errors or
172
doctest_summary.total_failures or
173
doctest_summary.total_errors)
107
suite = find_tests(args)
108
result = runner.run(suite)
109
return not result.wasSuccessful()
112
def test_with_trial():
113
from twisted.trial.reporter import TreeReporter
114
from twisted.trial.runner import TrialRunner
115
runner = TrialRunner(reporterFactory=TreeReporter, realTimeErrors=True)
116
return test_with_runner(runner)
119
def test_with_unittest():
120
runner = unittest.TextTestRunner()
121
return test_with_runner(runner)
177
124
if __name__ == "__main__":
178
125
runner = os.environ.get("STORM_TEST_RUNNER")