Version 6.4.0.0.beta1, tag libreoffice-6.4.0.0.beta1
[LibreOffice.git] / uitest / test_main.py
blob851a44f8e92995b94fa97139be27dd906be312dc
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 calc_tests
13 import importlib
14 import importlib.machinery
16 import uitest.config
18 from uitest.framework import UITestCase
20 from libreoffice.connection import OfficeConnection
22 test_name_limit_found = False
24 def parseArgs(argv):
25 (optlist,args) = getopt.getopt(argv[1:], "hdr",
26 ["help", "debug", "soffice=", "userdir=", "dir=", "file=", "gdb"])
27 return (dict(optlist), args)
29 def usage():
30 message = """usage: {program} [option]... [task_file]..."
31 -h | --help: print usage information
32 {connection_params}
33 the 'task_file' parameters should be
34 full absolute pathnames, not URLs."""
35 print(message.format(program = os.path.basename(sys.argv[0]), \
36 connection_params = OfficeConnection.getHelpText()))
39 def find_test_files(dir_path):
40 valid_files = []
41 for f in os.listdir(dir_path):
42 file_path = os.path.join(dir_path, f)
44 # don't go through the sub-directories
45 if not os.path.isfile(file_path):
46 continue
48 if os.path.splitext(file_path)[1] == ".swp":
49 continue # ignore VIM swap files
51 # fail on any non .py files
52 if not os.path.splitext(file_path)[1] == ".py":
53 raise Exception("file with an extension which is not .py: " + file_path)
55 # ignore the __init__.py file
56 # it is obviously not a test file
57 if f is "__init__.py":
58 continue
60 valid_files.append(file_path)
62 return valid_files
64 def get_classes_of_module(module):
65 md = module.__dict__
66 return [ md[c] for c in md if (
67 isinstance(md[c], type) and md[c].__module__ == module.__name__ ) ]
69 def get_test_case_classes_of_module(module):
70 classes = get_classes_of_module(module)
71 return [ c for c in classes if issubclass(c, UITestCase) ]
73 def add_tests_for_file(test_file, test_suite):
74 test_name_limit = os.environ.get('UITEST_TEST_NAME', '')
75 test_loader = unittest.TestLoader()
76 module_name = os.path.splitext(os.path.split(test_file)[1])[0]
78 loader = importlib.machinery.SourceFileLoader(module_name, test_file)
79 mod = loader.load_module()
80 classes = get_test_case_classes_of_module(mod)
81 global test_name_limit_found
82 for c in classes:
83 test_names = test_loader.getTestCaseNames(c)
84 for test_name in test_names:
85 full_name = ".".join([module_name, c.__name__, test_name])
86 if len(test_name_limit) > 0:
87 if not test_name_limit.startswith(full_name):
88 continue
89 test_name_limit_found = True
91 obj = c(test_name, opts)
92 test_suite.addTest(obj)
94 def get_test_suite_for_dir(opts):
95 test_suite = unittest.TestSuite()
97 valid_test_files = find_test_files(opts['--dir'])
98 for test_file in valid_test_files:
99 add_tests_for_file(test_file, test_suite)
100 return test_suite
103 if __name__ == '__main__':
104 (opts,args) = parseArgs(sys.argv)
105 if "-h" in opts or "--help" in opts:
106 usage()
107 sys.exit()
108 elif not "--soffice" in opts:
109 usage()
110 sys.exit(1)
111 elif "--dir" in opts:
112 test_suite = get_test_suite_for_dir(opts)
113 test_name_limit = os.environ.get('UITEST_TEST_NAME', '')
114 print(test_name_limit_found)
115 if len(test_name_limit) > 0 and not test_name_limit_found:
116 print("UITEST_TEST_NAME '%s' does not match any test" % test_name_limit)
117 sys.exit(1)
118 elif "--file" in opts:
119 test_suite = unittest.TestSuite()
120 add_tests_for_file(opts['--file'], test_suite)
121 else:
122 usage()
123 sys.exit()
125 if "-d" in opts or "--debug" in opts:
126 uitest.config.use_sleep = True
128 result = unittest.TextTestRunner(stream=sys.stdout, verbosity=2).run(test_suite)
129 print("Tests run: %d" % result.testsRun)
130 print("Tests failed: %d" % len(result.failures))
131 print("Tests errors: %d" % len(result.errors))
132 print("Tests skipped: %d" % len(result.skipped))
133 if not result.wasSuccessful():
134 sys.exit(1)
135 sys.exit(0)
137 # vim: set shiftwidth=4 softtabstop=4 expandtab: