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 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":
60 valid_files
.append(file_path
)
64 def get_classes_of_module(module
):
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
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
):
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
)
103 if __name__
== '__main__':
104 (opts
,args
) = parseArgs(sys
.argv
)
105 if "-h" in opts
or "--help" in opts
:
108 elif not "--soffice" in opts
:
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
)
118 elif "--file" in opts
:
119 test_suite
= unittest
.TestSuite()
120 add_tests_for_file(opts
['--file'], test_suite
)
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():
137 # vim: set shiftwidth=4 softtabstop=4 expandtab: