xref: /aosp_15_r20/build/bazel/rules/android/android_test.bzl (revision 7594170e27e0732bc44b93d1440d87a54b6ffe7c)
1# Copyright (C) 2023 The Android Open Source Project
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#     http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15"""android_test macro for building and running Android device tests with Bazel."""
16
17load("//build/bazel/rules/android:android_binary.bzl", "android_binary")
18load(
19    "//build/bazel/rules/tradefed:tradefed.bzl",
20    "FILTER_GENERATOR_SUFFIX",
21    "LANGUAGE_ANDROID",
22    "TEST_DEP_SUFFIX",
23    "java_test_filter_generator",
24    "tradefed_test_suite",
25)
26
27def android_test(
28        name,
29        srcs,
30        tags = [],
31        optimize = False,
32        visibility = ["//visibility:private"],
33        **kwargs):
34    """android_test macro for building and running Android device tests with Bazel.
35
36    Args:
37      name: The name of this target.
38      srcs: The list of source files that are processed to create the target.
39      tags: Tags for the test binary target and test suite target.
40      optimize: Boolean, whether optimize the build process.
41        android_test disables optimize by default.
42      visibility: Bazel visibility declarations for this target.
43      **kwargs: map, additional args to pass to android_binary.
44    """
45    test_dep_name = name + TEST_DEP_SUFFIX
46    android_binary(
47        name = test_dep_name,
48        srcs = srcs,
49        optimize = optimize,
50        tags = tags,
51        visibility = ["//visibility:private"],
52        testonly = True,
53        **kwargs
54    )
55
56    test_filter_generator_name = name + FILTER_GENERATOR_SUFFIX
57    java_test_filter_generator(
58        name = test_filter_generator_name,
59        srcs = srcs,
60        module_name = name,
61    )
62
63    tradefed_test_suite(
64        name = name,
65        test_dep = test_dep_name,
66        test_config = None,
67        template_test_config = None,
68        template_configs = None,
69        template_install_base = None,
70        device_driven_test_config = "//build/make/core:instrumentation_test_config_template.xml",
71        runs_on = ["device"],
72        test_filter_generator = test_filter_generator_name,
73        tags = tags,
74        visibility = visibility,
75        test_language = LANGUAGE_ANDROID,
76    )
77