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/.
14 import importlib
.machinery
18 from uitest
.framework
import UITestCase
20 from libreoffice
.connection
import OfficeConnection
22 test_name_limit_found
= False
25 (optlist
,args
) = getopt
.getopt(argv
[1:], "hdr",
26 ["help", "debug", "soffice=", "userdir=", "dir=", "file=", "gdb"])
27 return (dict(optlist
), args
)
30 message
= """usage: {program} [option]... [task_file]..."
31 -h | --help: print usage information
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
):
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
):
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":
57 valid_files
.append(file_path
)
61 def get_classes_of_module(module
):
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
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
):
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
)
100 if __name__
== '__main__':
101 (opts
,args
) = parseArgs(sys
.argv
)
102 if "-h" in opts
or "--help" in opts
:
105 elif not "--soffice" in opts
:
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
)
115 elif "--file" in opts
:
116 test_suite
= unittest
.TestSuite()
117 add_tests_for_file(opts
['--file'], test_suite
)
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():
134 # vim: set shiftwidth=4 softtabstop=4 expandtab: