File: tests.py

package info (click to toggle)
deejayd 0.9.0-4
  • links: PTS, VCS
  • area: main
  • in suites: squeeze, wheezy
  • size: 2,596 kB
  • ctags: 2,514
  • sloc: python: 11,904; xml: 594; sh: 338; makefile: 42
file content (149 lines) | stat: -rwxr-xr-x 5,393 bytes parent folder | download
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
#!/usr/bin/env python

# Deejayd, a media player daemon
# Copyright (C) 2007-2009 Mickael Royer <mickael.royer@gmail.com>
#                         Alexandre Rossi <alexandre.rossi@gmail.com>
#
# 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 xmlprocessing
        - 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 xmlprocessing/TestAnswerParser
              or ./tests.py xmlprocessing/TestAnswerParser.testAnswerParserError
    * 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 xmlprocessing client
      would list all the tests that are to be run from those test modules.
"""

import sys, os, glob
from optparse import OptionParser
import unittest

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("*"))]

usage = "usage: %prog [options] [tests-list]"
parser = OptionParser(usage=usage)
parser.add_option("-p","--profile",dest="profile",type="string",\
    help="testserver profile used for this test suite")
parser.set_defaults(profile="default")
(options, myargs) = parser.parse_args()

# update profiles
from testdeejayd import TestCaseWithServer
TestCaseWithServer.profiles = options.profile

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))


# vim: ts=4 sw=4 expandtab