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
23 (optlist
,args
) = getopt
.getopt(argv
[1:], "hdr",
24 ["help", "debug", "soffice=", "userdir=", "dir=", "file=", "gdb"])
25 return (dict(optlist
), args
)
28 message
= """usage: {program} [option]... [task_file]..."
29 -h | --help: print usage information
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
):
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
):
46 # ignore any non .py files
47 if not os
.path
.splitext(file_path
)[1] == ".py":
50 # ignore the __init__.py file
51 # it is obviously not a test file
52 if f
is "__init__.py":
55 valid_files
.append(file_path
)
59 def get_classes_of_module(module
):
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
)
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
):
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
)
95 if __name__
== '__main__':
96 (opts
,args
) = parseArgs(sys
.argv
)
97 if "-h" in opts
or "--help" in opts
:
100 elif not "--soffice" in opts
:
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
)
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():
124 # vim: set shiftwidth=4 softtabstop=4 expandtab: