-
Notifications
You must be signed in to change notification settings - Fork 1
/
tests.py
executable file
·166 lines (145 loc) · 5.84 KB
/
tests.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
#!/usr/bin/env python3
# Deejayd, a media player daemon
# Copyright (C) 2007-2009 Mickael Royer <[email protected]>
# Alexandre Rossi <[email protected]>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
"""
This is the test suite launcher :
* Without arguments, it runs the whole test suite.
* It accepts a list of arguments which can be :
- a test module name without the 'test_' prefix.
e.g. : ./tests.py jsonrpc
- a test module name without the 'test_' prefix, a slash, and a test
name in unittest dotted notation (See the documentation of
loadTestsFromName at
http://docs.python.org/lib/testloader-objects.html)
e.g. : ./tests.py jsonrpc/TestJSONRPCBuilders
or ./tests.py jsonrpc/TestJSONRPCBuilders.test_response_builder
* If the fist argument is 'list', list all the possibles tests that can be
combined on the command line restricted by the same arguments.
e.g. : ./tests.py list jsonrpc library
would list all the tests that are to be run from those test modules.
* Test suite has several options :
* --player to select media backend : vlc(default) or gstreamer
"""
import sys, os, glob
python_version = sys.version_info
if python_version[0] < 3 and python_version[1] < 7:
try:
import unittest2 as unittest
except ImportError:
sys.exit("For python version < 2.7, deejayd tests require unittest2 mpdule")
else:
import unittest
from optparse import OptionParser
from copy import copy
from optparse import Option, OptionValueError
usage = "usage: %prog [options] [tests-list]"
parser = OptionParser(usage=usage)
parser.add_option("-p","--player",dest="player",type="string", \
help="media backend used for this test suite")
parser.set_defaults(player="vlc")
(options, myargs) = parser.parse_args()
if options.player == "gstreamer":
# Install GI reactor
from twisted.internet import gireactor
gireactor.install()
import testdeejayd
TEST_NAMESPACE = 'testdeejayd'
test_suites_dir = os.path.join(os.path.dirname(__file__), TEST_NAMESPACE)
# Workaround for __import__ behavior, see
# http://docs.python.org/lib/built-in-funcs.html
def my_import(name):
mod = __import__(name)
components = name.split('.')
for comp in components[1:]:
mod = getattr(mod, comp)
return mod
def get_testfile_from_id(id):
return os.path.join(test_suites_dir, "test_%s.py" % id)
def get_id_from_module(module):
return module.__name__[len(TEST_NAMESPACE+'.'+'test_'):]
def get_all_tests():
return [(x, None) for x in glob.glob(get_testfile_from_id("*"))]
# update options
from testdeejayd import _DeejaydTest
_DeejaydTest.media_backend = options.player
tests_to_run = None
list_only = False
args = None
if len(myargs) > 0:
if myargs[0] == 'list':
list_only = True
args = myargs[1:]
else:
args = myargs
if args:
tests_to_consider = []
for test_id in args:
try:
test_module, test_name = test_id.split('/')
except ValueError:
test_module = test_id
test_name = None
tests_to_consider.append((get_testfile_from_id(test_module), test_name))
else:
tests_to_consider = get_all_tests()
def get_test_suite(test_module, test_name=None):
test_suite = None
if test_name:
test_suite = unittest.defaultTestLoader.loadTestsFromName(test_name,
test_module)
else:
test_suite = unittest.defaultTestLoader.loadTestsFromModule(test_module)
return test_suite
def get_module_and_name(test_id):
fn, test_name = test_id
module_path = '.'.join([TEST_NAMESPACE, os.path.basename(fn[:-3])])
test_module = my_import(module_path)
return test_module, test_name
def print_tests(class_name, test_name=None):
if class_name.startswith('Test'):
has_tests = False
for fun_name in dir(getattr(test_module, class_name)):
if fun_name.startswith('test')\
and (not test_name or fun_name == test_name):
has_tests = True
print("%s/%s.%s" % (get_id_from_module(test_module),
class_name, fun_name))
if has_tests:
print("%s/%s" % (get_id_from_module(test_module), class_name))
if list_only:
for test_id in tests_to_consider:
test_module, test_name = get_module_and_name(test_id)
if test_name:
splitted_test_name = test_name.split('.')
if len(splitted_test_name) > 1:
class_name, test_name = splitted_test_name
else:
class_name, test_name = splitted_test_name[0], None
print_tests(class_name, test_name)
else:
for class_name in dir(test_module):
print_tests(class_name)
else:
suitelist = []
runner = unittest.TextTestRunner(verbosity = 2)
for test_id in tests_to_consider:
test_module, test_name = get_module_and_name(test_id)
suitelist.append(get_test_suite(test_module, test_name))
runner.run(unittest.TestSuite(suitelist))
testdeejayd.utils.twreactor.stop_twisted_reactor()
# vim: ts=4 sw=4 expandtab