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