svx: adjust the values for impress/draw ruler to work with LOKit
[libreoffice.git] / uitest / test_main.py
blob1957f54dc3734f56110db61d5b1f408f74e4da7b
1 # -*- tab-width: 4; indent-tabs-mode: nil; py-indent-offset: 4 -*-
3 # This Source Code Form is subject to the terms of the Mozilla Public
4 # License, v. 2.0. If a copy of the MPL was not distributed with this
5 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 import sys
9 import getopt
10 import os
11 import unittest
12 import importlib
13 import importlib.machinery
14 import types
16 from uitest.framework import UITestCase
18 from libreoffice.connection import OfficeConnection
19 from libreoffice.connection import PersistentConnection
21 test_name_limit_found = False
23 def parseArgs(argv):
24 (optlist,args) = getopt.getopt(argv[1:], "hr",
25 ["help", "soffice=", "oneprocess", "userdir=", "dir=", "file=", "gdb"])
26 return (dict(optlist), args)
28 def usage():
29 message = """usage: {program} [option]... [task_file]..."
30 -h | --help: print usage information
31 {connection_params}
32 the 'task_file' parameters should be
33 full absolute pathnames, not URLs."""
34 print(message.format(program = os.path.basename(sys.argv[0]), \
35 connection_params = OfficeConnection.getHelpText()))
38 def find_test_files(dir_path):
39 valid_files = []
40 for f in sorted(os.listdir(dir_path)):
41 file_path = os.path.join(dir_path, f)
43 # don't go through the sub-directories
44 if not os.path.isfile(file_path):
45 continue
47 if os.path.splitext(file_path)[1] == ".swp":
48 continue # ignore VIM swap files
50 if file_path[-1:] == "~":
51 continue # ignore backup files
53 # fail on any non .py files
54 if not os.path.splitext(file_path)[1] == ".py":
55 raise Exception("file with an extension which is not .py: " + file_path)
57 # ignore the __init__.py file
58 # it is obviously not a test file
59 if f == "__init__.py":
60 continue
62 valid_files.append(file_path)
64 return valid_files
66 def get_classes_of_module(module):
67 md = module.__dict__
68 return [ md[c] for c in md if (
69 isinstance(md[c], type) and md[c].__module__ == module.__name__ ) ]
71 def get_test_case_classes_of_module(module):
72 classes = get_classes_of_module(module)
73 return [ c for c in classes if issubclass(c, UITestCase) ]
75 def add_tests_for_file(test_file, test_suite):
76 test_name_limit = os.environ.get('UITEST_TEST_NAME', '')
77 test_loader = unittest.TestLoader()
78 module_name = os.path.splitext(os.path.split(test_file)[1])[0]
80 loader = importlib.machinery.SourceFileLoader(module_name, test_file)
81 # exec_module was only introduced in 3.4
82 if sys.version_info < (3,4):
83 mod = loader.load_module()
84 else:
85 mod = types.ModuleType(loader.name)
86 loader.exec_module(mod)
87 classes = get_test_case_classes_of_module(mod)
88 global test_name_limit_found
89 for c in classes:
90 test_names = test_loader.getTestCaseNames(c)
91 for test_name in test_names:
92 full_name = ".".join([module_name, c.__name__, test_name])
93 if len(test_name_limit) > 0:
94 if test_name_limit != full_name:
95 continue
96 test_name_limit_found = True
98 obj = c(test_name, opts, connection)
99 test_suite.addTest(obj)
101 def get_test_suite_for_dir(opts):
102 test_suite = unittest.TestSuite()
104 valid_test_files = find_test_files(opts['--dir'])
105 for test_file in valid_test_files:
106 add_tests_for_file(test_file, test_suite)
107 return test_suite
110 if __name__ == '__main__':
111 (opts,args) = parseArgs(sys.argv)
112 connection = None
113 if "--oneprocess" in opts:
114 connection = PersistentConnection(opts)
115 connection.setUp()
116 if "-h" in opts or "--help" in opts:
117 usage()
118 sys.exit()
119 elif not "--soffice" in opts:
120 usage()
121 sys.exit(1)
122 elif "--dir" in opts:
123 test_suite = get_test_suite_for_dir(opts)
124 test_name_limit = os.environ.get('UITEST_TEST_NAME', '')
125 if len(test_name_limit) > 0:
126 if not test_name_limit_found:
127 print("UITEST_TEST_NAME '%s' does not match any test" % test_name_limit)
128 sys.exit(1)
129 else:
130 print("UITEST_TEST_NAME '%s' active" % test_name_limit)
131 elif "--file" in opts:
132 test_suite = unittest.TestSuite()
133 add_tests_for_file(opts['--file'], test_suite)
134 else:
135 usage()
136 sys.exit()
138 result = unittest.TextTestRunner(stream=sys.stdout, verbosity=2).run(test_suite)
139 print("Tests run: %d" % result.testsRun)
140 print("Tests failed: %d" % len(result.failures))
141 print("Tests errors: %d" % len(result.errors))
142 print("Tests skipped: %d" % len(result.skipped))
143 if connection:
144 connection.tearDown()
145 connection.kill()
146 if not result.wasSuccessful():
147 sys.exit(1)
148 sys.exit(0)
150 # vim: set shiftwidth=4 softtabstop=4 expandtab: