|
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 # Script for running all ConE unit tests (cone, plug-ins and scripts). |
|
16 # |
|
17 |
|
18 import os, sys, re, imp |
|
19 import unittest |
|
20 |
|
21 # Path to the directory where this file is located |
|
22 ROOT_PATH = os.path.dirname(os.path.abspath(__file__)) |
|
23 |
|
24 # For module 'testautomation' |
|
25 sys.path.append(os.path.join(ROOT_PATH, 'testautomation')) |
|
26 |
|
27 from testautomation import testcli |
|
28 import cone |
|
29 import cone.public.plugin |
|
30 |
|
31 def _load_module(path): |
|
32 if not path.endswith('.py'): |
|
33 raise ValueError("Given parameter ('%s') is not a .py file" % path) |
|
34 |
|
35 dir = os.path.dirname(path) |
|
36 sys.path.insert(0, dir) |
|
37 try: |
|
38 modname = path.replace('.', '_') |
|
39 return imp.load_source(modname, path) |
|
40 finally: |
|
41 del sys.path[0] |
|
42 # Since the module name __init__ is needed in many places, |
|
43 # but its contents may differ, remove it from sys.modules |
|
44 # in order to force reloading every time |
|
45 if '__init__' in sys.modules: |
|
46 del sys.modules['__init__'] |
|
47 |
|
48 def _collect_unittest_suite_from_file(file_path): |
|
49 suite = unittest.TestSuite() |
|
50 module = _load_module(file_path) |
|
51 suite.addTests(unittest.TestLoader().loadTestsFromModule(module)) |
|
52 return suite |
|
53 |
|
54 |
|
55 def _find_unittest_files(path, recursive=True): |
|
56 """ |
|
57 Find all unittest_*.py files under the given directory. |
|
58 """ |
|
59 pattern = re.compile(r'^unittest_.*\.py$') |
|
60 unittest_files = [] |
|
61 if recursive: |
|
62 for root, dirs, files in os.walk(path, topdown=True): |
|
63 for filename in files: |
|
64 if pattern.match(filename) != None: |
|
65 filepath = os.path.abspath(os.path.join(root, filename)) |
|
66 unittest_files.append(filepath) |
|
67 else: |
|
68 for filename in os.listdir(path): |
|
69 if pattern.match(filename) != None: |
|
70 filepath = os.path.abspath(os.path.join(path, filename)) |
|
71 unittest_files.append(filepath) |
|
72 |
|
73 return unittest_files |
|
74 |
|
75 def _collect_unittest_suite_from_path(path, recursive=True): |
|
76 """ |
|
77 Collect a test suite containing all test cases loaded from |
|
78 unittest_*.py files in the given directory. |
|
79 """ |
|
80 path = os.path.abspath(path) |
|
81 |
|
82 # Collect the list of .py files containing unit tests |
|
83 unittest_files = _find_unittest_files(path, recursive) |
|
84 |
|
85 # Load the files as modules and load tests from them |
|
86 suite = unittest.TestSuite() |
|
87 for file in unittest_files: |
|
88 module = _load_module(file) |
|
89 suite.addTests(unittest.TestLoader().loadTestsFromModule(module)) |
|
90 return suite |
|
91 |
|
92 def _collect_suite(): |
|
93 def get_suite(path): |
|
94 return _collect_unittest_suite_from_path(os.path.join(ROOT_PATH, path)) |
|
95 def get_suite_from_file(file_path): |
|
96 return _collect_unittest_suite_from_file(os.path.join(ROOT_PATH, file_path)) |
|
97 |
|
98 suite = unittest.TestSuite() |
|
99 suite.addTests(get_suite('cone')) |
|
100 suite.addTests(get_suite('scripts/tests')) |
|
101 suite.addTests(get_suite('plugins')) |
|
102 suite.addTests(get_suite('testautomation')) |
|
103 |
|
104 # Tests can also be loaded from a file: |
|
105 #suite.addTests(get_suite_from_file('cone/public/tests/unittest_rules_on_configuration.py')) |
|
106 |
|
107 |
|
108 # Force-reload all ConE plug-in reader classes, since the __init__.py |
|
109 # files in the imported test cases have added plug-in sources to sys.path |
|
110 cone.public.plugin.ImplFactory.force_reload_reader_classes() |
|
111 |
|
112 return suite |
|
113 |
|
114 def _run_without_nose(): |
|
115 suite = _collect_suite() |
|
116 unittest.TextTestRunner(verbosity=2).run(suite) |
|
117 |
|
118 def _run_with_nose(): |
|
119 # Call plugin_utils.init_all() so that all plug-ins are loaded |
|
120 # (otherwise script tests, plug-in unit tests and plug-in integration |
|
121 # tests would not work) |
|
122 PLUGIN_SOURCE_ROOT = os.path.normpath(os.path.join(ROOT_PATH, 'plugins')) |
|
123 assert os.path.isdir(PLUGIN_SOURCE_ROOT) |
|
124 sys.path.append(PLUGIN_SOURCE_ROOT) |
|
125 import plugin_utils |
|
126 plugin_utils.init_all() |
|
127 |
|
128 # Find all unittest_*.py files |
|
129 test_files = [] |
|
130 def add_tests(path): |
|
131 test_files.extend(_find_unittest_files(os.path.join(ROOT_PATH, path))) |
|
132 add_tests('cone') |
|
133 add_tests('scripts/tests') |
|
134 add_tests('plugins') |
|
135 add_tests('testautomation') |
|
136 |
|
137 |
|
138 # Configure nose |
|
139 import nose |
|
140 plugins = nose.plugins.manager.DefaultPluginManager() |
|
141 conf = nose.config.Config(plugins=plugins, testNames=test_files) |
|
142 |
|
143 # Run the tests |
|
144 args = ['--verbosity=3', |
|
145 '--with-xunit', |
|
146 '--xunit-file=cone-alltests.xml', |
|
147 #'--collect-only', |
|
148 ] |
|
149 nose.run(config=conf, argv=[sys.argv[0]] + args) |
|
150 |
|
151 def main(): |
|
152 if '--with-nose' in sys.argv: |
|
153 _run_with_nose() |
|
154 else: |
|
155 _run_without_nose() |
|
156 |
|
157 if __name__ == '__main__': |
|
158 main() |