1
1
#!/usr/bin/env python
11
lib_dir = os.path.abspath(os.path.dirname(__file__))
12
sys.path.insert(0, lib_dir)
14
# This is needed for DBUS listeners to work.
15
from twisted.internet.glib2reactor import install
21
def find_tests(testpaths=()):
22
"""Find all test paths, or test paths contained in the provided sequence.
24
@param testpaths: If provided, only tests in the given sequence will
25
be considered. If not provided, all tests are
27
@return: (unittests, doctests) tuple, with lists of unittests and
28
doctests found, respectively.
30
topdir = os.path.abspath(os.path.dirname(__file__))
31
testdir = os.path.dirname(landscape.__file__)
32
testpaths = set(testpaths)
35
for root, dirnames, filenames in os.walk(testdir):
36
for filename in filenames:
37
filepath = os.path.join(root, filename)
38
relpath = filepath[len(topdir)+1:]
40
if (filename == "__init__.py" or filename.endswith(".pyc") or
41
relpath == "landscape/conftest.py" or "/tests/" not in relpath):
46
# Skip any tests not in testpaths.
47
for testpath in testpaths:
48
if relpath.startswith(testpath):
53
if filename.endswith(".py"):
54
unittests.append(relpath)
55
elif filename.endswith(".txt"):
56
doctests.append(relpath)
58
return unittests, doctests
62
"""Extract any arguments not starting with '-' from sys.argv."""
64
for i in range(len(sys.argv)-1,0,-1):
66
if not arg.startswith("-"):
72
def test_with_trial():
73
from twisted.scripts import trial
74
unittests, doctests = find_tests(parse_sys_argv())
75
sys.argv.extend(unittests)
79
def test_with_py_test():
81
dirname = os.path.dirname(__file__)
82
unittests, doctests = find_tests(parse_sys_argv())
83
sys.argv.extend(unittests)
84
sys.argv.extend(doctests)
85
# For timestamp checking when looping:
86
sys.argv.append(os.path.join(os.path.dirname(__file__), "landscape/"))
87
py.test.cmdline.main()
90
def test_with_unittest():
92
usage = "test.py [options] [<test filename>, ...]"
94
parser = optparse.OptionParser(usage=usage)
96
parser.add_option('--verbose', action='store_true')
97
opts, args = parser.parse_args()
99
runner = unittest.TextTestRunner()
8
if arg.startswith("landscape"):
11
args.append("landscape")
12
os.execvp("trial", ["trial", "-r", "glib2"] + args)
104
loader = unittest.TestLoader()
106
unittests, doctests = find_tests(args)
110
self.total_failures = 0
111
self.total_errors = 0
113
def __call__(self, tests, failures, errors):
114
self.total_tests += tests
115
self.total_failures += failures
116
self.total_errors += errors
117
print "(tests=%d, failures=%d, errors=%d)" % \
118
(tests, failures, errors)
120
unittest_summary = Summary()
121
doctest_summary = Summary()
124
print "Running unittests..."
125
for relpath in unittests:
126
print "[%s]" % relpath
127
modpath = relpath.replace('/', '.')[:-3]
128
module = __import__(modpath, None, None, [""])
129
test = loader.loadTestsFromModule(module)
130
result = runner.run(test)
131
unittest_summary(test.countTestCases(),
132
len(result.failures), len(result.errors))
136
print "Running doctests..."
137
doctest_flags = doctest.ELLIPSIS
138
for relpath in doctests:
139
print "[%s]" % relpath
140
failures, total = doctest.testfile(relpath,
141
optionflags=doctest_flags)
142
doctest_summary(total, failures, 0)
145
print "Total test cases: %d" % unittest_summary.total_tests
146
print "Total doctests: %d" % doctest_summary.total_tests
147
print "Total failures: %d" % (unittest_summary.total_failures +
148
doctest_summary.total_failures)
149
print "Total errors: %d" % (unittest_summary.total_errors +
150
doctest_summary.total_errors)
152
failed = bool(unittest_summary.total_failures or
153
unittest_summary.total_errors or
154
doctest_summary.total_failures or
155
doctest_summary.total_errors)
159
15
if __name__ == "__main__":
160
runner = os.environ.get("LANDSCAPE_TEST_RUNNER")
163
runner_func = globals().get("test_with_%s" % runner.replace(".", "_"))
165
sys.exit("Test runner not found: %s" % runner)
168
19
# vim:ts=4:sw=4:et