tdf#104816 sw: if non-section content, let all sections hide.
[LibreOffice.git] / uitest / test_main.py
blobbbfb8ad570729e28c1bf19aa3d9d097d2ad48e42
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 # fail on any non .py files
49 if not os.path.splitext(file_path)[1] == ".py":
50 raise Exception("file with an extension which is not .py: " + file_path)
52 # ignore the __init__.py file
53 # it is obviously not a test file
54 if f is "__init__.py":
55 continue
57 valid_files.append(file_path)
59 return valid_files
61 def get_classes_of_module(module):
62 md = module.__dict__
63 return [ md[c] for c in md if (
64 isinstance(md[c], type) and md[c].__module__ == module.__name__ ) ]
66 def get_test_case_classes_of_module(module):
67 classes = get_classes_of_module(module)
68 return [ c for c in classes if issubclass(c, UITestCase) ]
70 def add_tests_for_file(test_file, test_suite):
71 test_name_limit = os.environ.get('UITEST_TEST_NAME', '')
72 test_loader = unittest.TestLoader()
73 module_name = os.path.splitext(os.path.split(test_file)[1])[0]
75 loader = importlib.machinery.SourceFileLoader(module_name, test_file)
76 mod = loader.load_module()
77 classes = get_test_case_classes_of_module(mod)
78 global test_name_limit_found
79 for c in classes:
80 test_names = test_loader.getTestCaseNames(c)
81 for test_name in test_names:
82 full_name = ".".join([module_name, c.__name__, test_name])
83 if len(test_name_limit) > 0:
84 if not test_name_limit.startswith(full_name):
85 continue
86 test_name_limit_found = True
88 obj = c(test_name, opts)
89 test_suite.addTest(obj)
91 def get_test_suite_for_dir(opts):
92 test_suite = unittest.TestSuite()
94 valid_test_files = find_test_files(opts['--dir'])
95 for test_file in valid_test_files:
96 add_tests_for_file(test_file, test_suite)
97 return test_suite
100 if __name__ == '__main__':
101 (opts,args) = parseArgs(sys.argv)
102 if "-h" in opts or "--help" in opts:
103 usage()
104 sys.exit()
105 elif not "--soffice" in opts:
106 usage()
107 sys.exit(1)
108 elif "--dir" in opts:
109 test_suite = get_test_suite_for_dir(opts)
110 test_name_limit = os.environ.get('UITEST_TEST_NAME', '')
111 print(test_name_limit_found)
112 if len(test_name_limit) > 0 and not test_name_limit_found:
113 print("UITEST_TEST_NAME '%s' does not match any test" % test_name_limit)
114 sys.exit(1)
115 elif "--file" in opts:
116 test_suite = unittest.TestSuite()
117 add_tests_for_file(opts['--file'], test_suite)
118 else:
119 usage()
120 sys.exit()
122 if "-d" in opts or "--debug" in opts:
123 uitest.config.use_sleep = True
125 result = unittest.TextTestRunner(stream=sys.stdout, verbosity=2).run(test_suite)
126 print("Tests run: %d" % result.testsRun)
127 print("Tests failed: %d" % len(result.failures))
128 print("Tests errors: %d" % len(result.errors))
129 print("Tests skipped: %d" % len(result.skipped))
130 if not result.wasSuccessful():
131 sys.exit(1)
132 sys.exit(0)
134 # vim: set shiftwidth=4 softtabstop=4 expandtab: