11
sys.path.insert(0, os.path.abspath("."))
13
from distutils.spawn import find_executable
16
class TestRunner(object):
18
def _load_unittest(self, relpath):
19
"""Load unit tests from a Python module with the given relative path."""
20
assert relpath.endswith(".py"), (
21
"%s does not appear to be a Python module" % relpath)
22
modpath = relpath.replace(os.path.sep, ".")[:-3]
23
module = __import__(modpath, None, None, [""])
25
# If the module has a 'suite' or 'test_suite' function, use that
27
if hasattr(module, "suite"):
29
elif hasattr(module, "test_suite"):
30
return module.test_suite()
32
return unittest.defaultTestLoader.loadTestsFromModule(module)
34
def _collect_tests(self, testpath, test_pattern):
35
"""Return the set of unittests."""
36
suite = unittest.TestSuite()
38
pattern = re.compile('.*%s.*' % test_pattern)
43
module_suite = self._load_unittest(testpath)
45
for inner_suite in module_suite._tests:
46
for test in inner_suite._tests:
47
if pattern.match(test.id()):
50
suite.addTests(module_suite)
53
# We don't use the dirs variable, so ignore the warning
54
# pylint: disable-msg=W0612
55
for root, dirs, files in os.walk("tests"):
57
path = os.path.join(root, file)
58
if file.endswith(".py") and file.startswith("test_"):
59
module_suite = self._load_unittest(path)
61
for inner_suite in module_suite._tests:
62
for test in inner_suite._tests:
63
if pattern.match(test.id()):
66
suite.addTests(module_suite)
69
def run(self, testpath, test_pattern=None, loops=None):
71
# install the glib2reactor before any import of the reactor to avoid
72
# using the default SelectReactor and be able to run the dbus tests
73
from twisted.internet import glib2reactor
74
glib2reactor.install()
75
from twisted.internet import reactor
76
from twisted.trial.reporter import TreeReporter
77
from twisted.trial.runner import TrialRunner
79
from contrib.dbus_util import DBusRunner
80
dbus_runner = DBusRunner()
81
dbus_runner.startDBus()
83
workingDirectory = os.path.join(os.getcwd(), "_trial_temp", "tmp")
84
runner = TrialRunner(reporterFactory=TreeReporter, realTimeErrors=True,
85
workingDirectory=workingDirectory)
87
# setup a custom XDG_CACHE_HOME and create the logs directory
88
xdg_cache = os.path.join(os.getcwd(), "_trial_temp", "xdg_cache")
89
os.environ["XDG_CACHE_HOME"] = xdg_cache
90
# setup the ROOTDIR env var
91
os.environ['ROOTDIR'] = os.getcwd()
92
if not os.path.exists(xdg_cache):
93
os.makedirs(xdg_cache)
96
suite = self._collect_tests(testpath, test_pattern)
99
suite = unittest.TestSuite()
100
for x in xrange(loops):
101
suite.addTest(old_suite)
102
result = runner.run(suite)
103
success = result.wasSuccessful()
105
dbus_runner.stopDBus()
112
if __name__ == '__main__':
113
from optparse import OptionParser
114
usage = '%prog [options] path'
115
parser = OptionParser(usage=usage)
116
parser.add_option("-t", "--test", dest="test",
117
help = "run specific tests, e.g: className.methodName")
118
parser.add_option("-l", "--loop", dest="loops", type="int", default=1,
119
help = "loop selected tests LOOPS number of times",
122
(options, args) = parser.parse_args()
125
if not os.path.exists(testpath):
126
print "the path to test does not exists!"
130
TestRunner().run(testpath, options.test, options.loops)