blob: fb8bf0575927498997ff671cbab633ecc635ff18 [file] [log] [blame]
Kevin Chengda4f07a2018-06-26 10:25:05 -07001#!/usr/bin/python
2#
3# Copyright 2018 The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16"""Main entry point for all of acloud's unittest."""
17
18from importlib import import_module
19import os
20import sys
21import unittest
22
23
24def GetTestModules():
25 """Return list of testable modules.
26
27 We need to find all the test files (*_test.py) and get their relative
28 path (internal/lib/utils_test.py) and translate it to an import path and
29 strip the py ext (internal.lib.utils_test).
30
31 Returns:
32 List of strings (the testable module import path).
33 """
34 testable_modules = []
35 base_path = os.path.dirname(os.path.realpath(__file__))
36
37 # Get list of all python files that end in _test.py (except for __file__).
38 for dirpath, _, files in os.walk(base_path):
39 for f in files:
40 if f.endswith("_test.py") and f != os.path.basename(__file__):
41 # Now transform it into a relative import path.
42 full_file_path = os.path.join(dirpath, f)
43 rel_file_path = os.path.relpath(full_file_path, base_path)
44 rel_file_path, _ = os.path.splitext(rel_file_path)
45 rel_file_path = rel_file_path.replace(os.sep, ".")
46 testable_modules.append(rel_file_path)
47
48 return testable_modules
49
50
51def main(_):
52 """Main unittest entry.
53
54 Args:
55 argv: A list of system arguments. (unused)
56
57 Returns:
58 0 if success. None-zero if fails.
59 """
60 test_modules = GetTestModules()
61 for mod in test_modules:
62 import_module(mod)
63
64 loader = unittest.defaultTestLoader
65 test_suite = loader.loadTestsFromNames(test_modules)
66 runner = unittest.TextTestRunner(verbosity=2)
67 result = runner.run(test_suite)
68 sys.exit(not result.wasSuccessful())
69
70
71if __name__ == '__main__':
72 main(sys.argv[1:])