|
1 # |
|
2 # Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 # All rights reserved. |
|
4 # This component and the accompanying materials are made available |
|
5 # under the terms of "Eclipse Public License v1.0" |
|
6 # which accompanies this distribution, and is available |
|
7 # at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 # |
|
9 # Initial Contributors: |
|
10 # Nokia Corporation - initial contribution. |
|
11 # |
|
12 # Contributors: |
|
13 # |
|
14 # Description: |
|
15 # |
|
16 |
|
17 import sys, os, re, unittest |
|
18 |
|
19 ROOT_PATH = os.path.abspath(os.path.dirname(__file__)) |
|
20 |
|
21 CONE_PATH = os.path.join(ROOT_PATH, 'cone') |
|
22 EGGS_PATH = os.path.join(ROOT_PATH, 'eggs') |
|
23 |
|
24 def add_eggs_to_path(egg_path): |
|
25 if not os.path.isdir(egg_path): |
|
26 return |
|
27 |
|
28 for name in os.listdir(egg_path): |
|
29 if name.endswith('.egg'): |
|
30 path = os.path.normpath(os.path.join(egg_path, name)) |
|
31 if path not in sys.path: |
|
32 print "Adding '%s' to path" % name |
|
33 sys.path.append(path) |
|
34 |
|
35 |
|
36 def collect_test_suite(base_dir, module_subdir, cone_cmd): |
|
37 # Check that the directory exists |
|
38 module_dir = os.path.join(base_dir, module_subdir) |
|
39 if not os.path.exists(module_dir): |
|
40 raise RuntimeError("'%s' does not exist!" % module_dir) |
|
41 |
|
42 # Collect the names of all test modules (of the form "unittest_*.py") |
|
43 test_modules_names = [] |
|
44 for name in os.listdir(module_dir): |
|
45 if re.match(r'^unittest_.*\.py$', name) != None: |
|
46 test_modules_names.append(name[:-3]) |
|
47 |
|
48 # Import the modules |
|
49 sys.path.insert(0, base_dir) |
|
50 try: |
|
51 suite = unittest.TestSuite() |
|
52 for modname in test_modules_names: |
|
53 # Load the test module dynamically and add it to the test suite |
|
54 top_module = __import__(module_subdir + '.' + modname) |
|
55 |
|
56 # top_module contains now actually e.g. mymodule.unittest_sometest, |
|
57 # so get the actual unit test module |
|
58 module = getattr(top_module, modname) |
|
59 |
|
60 module.CONE_CMD = cone_cmd |
|
61 suite.addTests(unittest.TestLoader().loadTestsFromModule(module)) |
|
62 return suite |
|
63 finally: |
|
64 del sys.path[0] |
|
65 |
|
66 def main(): |
|
67 CONE_CMD = os.path.join(CONE_PATH, 'cone.cmd') |
|
68 if not os.path.exists(CONE_CMD): |
|
69 print "cone.cmd not detected under '%s', exiting..." % CONE_PATH |
|
70 return 0 |
|
71 |
|
72 add_eggs_to_path(EGGS_PATH) |
|
73 |
|
74 suite = unittest.TestSuite() |
|
75 |
|
76 # Collect script test suite |
|
77 suite.addTest(collect_test_suite(ROOT_PATH, 'tests', CONE_CMD)) |
|
78 |
|
79 # Collect test suites from the plugin-tests/ directory |
|
80 PLUGIN_TEST_DIR = os.path.join(ROOT_PATH, 'plugin-tests') |
|
81 if os.path.exists(PLUGIN_TEST_DIR): |
|
82 for name in os.listdir(PLUGIN_TEST_DIR): |
|
83 plugin_suite = collect_test_suite(PLUGIN_TEST_DIR, name, CONE_CMD) |
|
84 suite.addTest(plugin_suite) |
|
85 |
|
86 # Run the tests |
|
87 unittest.TextTestRunner(verbosity=2).run(suite) |
|
88 return 0 |
|
89 |
|
90 if __name__ == "__main__": |
|
91 sys.exit(main()) |