• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
19import logging
20import os
21import sys
22import unittest
23
24from importlib import import_module
25from unittest import mock
26
27# Setup logging to be silent so unittests can pass through TF.
28logging.disable(logging.ERROR)
29
30ENV = {
31    'ANDROID_BUILD_TOP': '/',
32    'ANDROID_PRODUCT_OUT': '/out/prod',
33    'ANDROID_TARGET_OUT_TESTCASES': '/out/prod/tcases',
34    'ANDROID_HOST_OUT': '/out/host',
35    'ANDROID_HOST_OUT_TESTCASES': '/out/host/tcases',
36    'TARGET_PRODUCT': 'aosp_cf_x86_64',
37    'TARGET_BUILD_VARIANT': 'userdebug',
38}
39
40def get_test_modules():
41    """Returns a list of testable modules.
42
43    Finds all the test files (*_unittest.py) and get their no-absolute
44    path (internal/lib/utils_test.py) and translate it to an import path and
45    strip the py ext (internal.lib.utils_test).
46
47    Returns:
48        List of strings (the testable module import path).
49    """
50    testable_modules = []
51    package = os.path.dirname(os.path.realpath(__file__))
52    base_path = os.path.dirname(package)
53
54    for dirpath, _, files in os.walk(package):
55        for f in files:
56            if f.endswith("_unittest.py"):
57                # Now transform it into a no-absolute import path.
58                full_file_path = os.path.join(dirpath, f)
59                rel_file_path = os.path.relpath(full_file_path, base_path)
60                rel_file_path, _ = os.path.splitext(rel_file_path)
61                rel_file_path = rel_file_path.replace(os.sep, ".")
62                testable_modules.append(rel_file_path)
63
64    return testable_modules
65
66def run_test_modules(test_modules):
67    """Main method of running unit tests.
68
69    Args:
70        test_modules; a list of module names.
71
72    Returns:
73        result: a namespace of unittest result.
74    """
75    for mod in test_modules:
76        import_module(mod)
77
78    loader = unittest.defaultTestLoader
79    test_suite = loader.loadTestsFromNames(test_modules)
80    runner = unittest.TextTestRunner(verbosity=2)
81    return runner.run(test_suite)
82
83
84if __name__ == '__main__':
85    print(sys.version_info)
86    with mock.patch.dict('os.environ', ENV):
87        result = run_test_modules(get_test_modules())
88        if not result.wasSuccessful():
89            sys.exit(not result.wasSuccessful())
90        sys.exit(0)
91