1#!/usr/bin/env python3 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 17"""Main entrypoint for all of atest's unittest.""" 18 19# pylint: disable=line-too-long 20 21import logging 22import os 23import sys 24import unittest 25 26from importlib import import_module 27 28# Setup logging to be silent so unittests can pass through TF. 29logging.disable(logging.ERROR) 30 31def get_test_modules(): 32 """Returns a list of testable modules. 33 34 Finds all the test files (*_unittest.py) and get their no-absolute 35 path (internal/lib/utils_test.py) and translate it to an import path and 36 strip the py ext (internal.lib.utils_test). 37 38 Returns: 39 List of strings (the testable module import path). 40 """ 41 testable_modules = [] 42 base_path = os.path.dirname(os.path.realpath(__file__)) 43 44 for dirpath, _, files in os.walk(base_path): 45 for f in files: 46 if f.endswith("_unittest.py"): 47 # Now transform it into a no-absolute import path. 48 full_file_path = os.path.join(dirpath, f) 49 rel_file_path = os.path.relpath(full_file_path, base_path) 50 rel_file_path, _ = os.path.splitext(rel_file_path) 51 rel_file_path = rel_file_path.replace(os.sep, ".") 52 testable_modules.append(rel_file_path) 53 54 return testable_modules 55 56def main(_): 57 """Main unittest entry. 58 59 Args: 60 argv: A list of system arguments. (unused) 61 62 Returns: 63 0 if success. None-zero if fails. 64 """ 65 test_modules = get_test_modules() 66 for mod in test_modules: 67 import_module(mod) 68 69 loader = unittest.defaultTestLoader 70 test_suite = loader.loadTestsFromNames(test_modules) 71 runner = unittest.TextTestRunner(verbosity=2) 72 result = runner.run(test_suite) 73 sys.exit(not result.wasSuccessful()) 74 75 76if __name__ == '__main__': 77 main(sys.argv[1:]) 78