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