Version 6.1.0.2, tag libreoffice-6.1.0.2
[LibreOffice.git] / uitest / test_main.py
blobec4f2071f1021244898ebab77c0b966e1fad43d1
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 def parseArgs(argv):
23 (optlist,args) = getopt.getopt(argv[1:], "hdr",
24 ["help", "debug", "soffice=", "userdir=", "dir=", "file=", "gdb"])
25 return (dict(optlist), args)
27 def usage():
28 message = """usage: {program} [option]... [task_file]..."
29 -h | --help: print usage information
30 {connection_params}
31 the 'task_file' parameters should be
32 full absolute pathnames, not URLs."""
33 print(message.format(program = os.path.basename(sys.argv[0]), \
34 connection_params = OfficeConnection.getHelpText()))
37 def find_test_files(dir_path):
38 valid_files = []
39 for f in os.listdir(dir_path):
40 file_path = os.path.join(dir_path, f)
42 # don't go through the sub-directories
43 if not os.path.isfile(file_path):
44 continue
46 # ignore any non .py files
47 if not os.path.splitext(file_path)[1] == ".py":
48 continue
50 # ignore the __init__.py file
51 # it is obviously not a test file
52 if f is "__init__.py":
53 continue
55 valid_files.append(file_path)
57 return valid_files
59 def get_classes_of_module(module):
60 md = module.__dict__
61 return [ md[c] for c in md if (
62 isinstance(md[c], type) and md[c].__module__ == module.__name__ ) ]
64 def get_test_case_classes_of_module(module):
65 classes = get_classes_of_module(module)
66 return [ c for c in classes if issubclass(c, UITestCase) ]
68 def add_tests_for_file(test_file, test_suite):
69 test_name_limit = os.environ.get('UITEST_TEST_NAME', '')
70 test_loader = unittest.TestLoader()
71 module_name = os.path.splitext(os.path.split(test_file)[1])[0]
73 loader = importlib.machinery.SourceFileLoader(module_name, test_file)
74 mod = loader.load_module()
75 classes = get_test_case_classes_of_module(mod)
76 for c in classes:
77 test_names = test_loader.getTestCaseNames(c)
78 for test_name in test_names:
79 full_name = ".".join([module_name, c.__name__, test_name])
80 if len(test_name_limit) > 0 and not test_name_limit.startswith(full_name):
81 continue
83 obj = c(test_name, opts)
84 test_suite.addTest(obj)
86 def get_test_suite_for_dir(opts):
87 test_suite = unittest.TestSuite()
89 valid_test_files = find_test_files(opts['--dir'])
90 for test_file in valid_test_files:
91 add_tests_for_file(test_file, test_suite)
92 return test_suite
95 if __name__ == '__main__':
96 (opts,args) = parseArgs(sys.argv)
97 if "-h" in opts or "--help" in opts:
98 usage()
99 sys.exit()
100 elif not "--soffice" in opts:
101 usage()
102 sys.exit(1)
103 elif "--dir" in opts:
104 test_suite = get_test_suite_for_dir(opts)
105 elif "--file" in opts:
106 test_suite = unittest.TestSuite()
107 add_tests_for_file(opts['--file'], test_suite)
108 else:
109 usage()
110 sys.exit()
112 if "-d" in opts or "--debug" in opts:
113 uitest.config.use_sleep = True
115 result = unittest.TextTestRunner(stream=sys.stdout, verbosity=2).run(test_suite)
116 print("Tests run: %d" % result.testsRun)
117 print("Tests failed: %d" % len(result.failures))
118 print("Tests errors: %d" % len(result.errors))
119 print("Tests skipped: %d" % len(result.skipped))
120 if not result.wasSuccessful():
121 sys.exit(1)
122 sys.exit(0)
124 # vim: set shiftwidth=4 softtabstop=4 expandtab: