12
def disable_conftest():
13
"""Install an empty module on py.test's tests.conftest
15
When other runners are stepping by the suite of tests, they shouldn't
16
find the py.test specific conftest.py file.
18
conftest = new.module("conftest")
19
conftest.__file__ = "tests/conftest.py"
20
sys.modules["tests.conftest"] = tests.conftest = conftest
22
def test_with_trial():
23
from twisted.scripts import trial
25
if len(sys.argv) == 1:
26
for dir, dirs, files in os.walk('tests'):
28
if file.endswith('.py'):
29
sys.argv.append(os.path.join(dir, file))
32
def test_with_py_test():
34
if not [x for x in sys.argv[1:] if not x.startswith("-")]:
35
tests_dir = os.path.join(os.path.dirname(__file__), "tests/")
36
# For timestamp checking when looping:
37
landscape_dir = os.path.join(lib_dir, "landscape")
38
sys.argv.extend([tests_dir, landscape_dir])
39
py.test.cmdline.main()
41
def test_with_unittest():
43
usage = "test.py [options] [<test filename>, ...]"
44
description = ("If the environment variables %s and %s are present,\n"
45
"unittests will be run against the specified databases."
46
% (MAIN_DB_URI, RESOURCE_DB_URI))
48
parser = optparse.OptionParser(description=description, usage=usage)
50
parser.add_option('--verbose', action='store_true')
51
opts, args = parser.parse_args()
56
runner = unittest.TextTestRunner()
61
loader = unittest.TestLoader()
62
topdir = os.path.abspath(os.path.dirname(__file__))
63
testdir = os.path.dirname(tests.__file__)
64
doctest_flags = doctest.ELLIPSIS
67
for root, dirnames, filenames in os.walk(testdir):
68
for filename in filenames:
69
filepath = os.path.join(root, filename)
70
relpath = filepath[len(topdir)+1:]
71
if (filename == "__init__.py" or filename.endswith(".pyc") or
72
opts.args and relpath not in opts.args):
74
elif filename.endswith(".py"):
75
unittests.append(relpath)
76
elif filename.endswith(".txt"):
77
doctests.append(relpath)
81
self.total_failures = 0
84
def __call__(self, tests, failures, errors):
85
self.total_tests += tests
86
self.total_failures += failures
87
self.total_errors += errors
88
print "(tests=%d, failures=%d, errors=%d)" % \
89
(tests, failures, errors)
91
unittest_summary = Summary()
92
doctest_summary = Summary()
95
print "Running unittests..."
96
for relpath in unittests:
97
print "[%s]" % relpath
98
modpath = relpath.replace('/', '.')[:-3]
99
module = __import__(modpath, None, None, [""])
100
test = loader.loadTestsFromModule(module)
101
result = runner.run(test)
102
unittest_summary(test.countTestCases(),
103
len(result.failures), len(result.errors))
107
print "Running doctests..."
108
for relpath in doctests:
109
print "[%s]" % relpath
110
failures, total = doctest.testfile(relpath,
111
optionflags=doctest_flags)
112
doctest_summary(total, failures, 0)
115
print "Total test cases: %d" % unittest_summary.total_tests
116
print "Total doctests: %d" % doctest_summary.total_tests
117
print "Total failures: %d" % (unittest_summary.total_failures +
118
doctest_summary.total_failures)
119
print "Total errors: %d" % (unittest_summary.total_errors +
120
doctest_summary.total_errors)
122
failed = bool(unittest_summary.total_failures or
123
unittest_summary.total_errors or
124
doctest_summary.total_failures or
125
doctest_summary.total_errors)
129
if __name__ == "__main__":
130
runner = os.environ.get("STORM_TEST_RUNNER")
133
runner_func = globals().get("test_with_%s" % runner.replace(".", "_"))
135
sys.exit("Test runner not found: %s" % runner)