aboutsummaryrefslogtreecommitdiff
path: root/en/compatibility/tests/development/test-mapping.md
blob: 5f843199cbbabdc3c78819c5224a90dd02547be2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
Project: /_project.yaml
Book: /_book.yaml

{% include "_versions.html" %}

<!--
  Copyright 2018 The Android Open Source Project

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->

# Test Mapping

This is a brief introduction of Test Mapping and an explanation of how to get
started configuring tests easily in the Android Open Source Project (AOSP).

## What is Test Mapping?

Test Mapping is a Gerrit-based approach that allows developers to create pre-
and post-submit test rules directly in the Android source tree and leave the
decisions of branches and devices to be tested to the test infrastructure
itself. Test Mapping definitions are JSON files named TEST_MAPPING that can be
placed in any source directory.

[Atest](atest) can use the TEST_MAPPING files to run presubmit tests in the
associated directories. With Test Mapping, you can add the same set of tests to
presubmit checks with a simple change inside the Android source tree.

See these examples:

[Add presubmit tests to TEST_MAPPING for services.core](https://android.googlesource.com/platform/frameworks/base/+/master/services/core/java/com/android/server/pm/dex/TEST_MAPPING)

[Add presubmit and postsubmit tests to TEST_MAPPING for startop/iorap](https://android.googlesource.com/platform/frameworks/base/+/master/startop/iorap/TEST_MAPPING)

## Defining test groups

Test Mapping groups tests via a **test group**. The name of a test group can be
any string. For example, *presubmit* can be for a group of tests to run when
validating changes. And *postsubmit* tests can be used to validate the
builds after changes are merged.

## Packaging build script rules

In order for the [Trade Federation Test Harness](/devices/tech/test_infra/tradefed)
to run Test Mapping's test modules for a given build, these modules must have
**test_suite** set for [Soong](blueprints) or **LOCAL_COMPATIBILITY_SUITE** set
for Make to one of these two suites:

*   **device-tests** - built against a specific device CPU
*   **general-tests** - built against any application binary interface (ABI)

When in doubt, put gtests in _device-tests_ and APK tests in _general-tests_.

Examples:

```
Android.bp: test_suites: ["device-tests"],
Android.mk: LOCAL_COMPATIBILITY_SUITE := device-tests
```


## Creating Test Mapping files

For the directory requiring test coverage, simply add a TEST_MAPPING JSON file
resembling the example below. These rules will ensure the tests run in presubmit
checks when any files are touched in that directory or any of its subdirectories.

### Following an example

Here is a sample TEST_MAPPING file:

```
{
  "presubmit": [
    {
      "name": "CtsWindowManagerDeviceTestCases",
      "options": [
        {
          "include-annotation": "android.platform.test.annotations.RequiresDevice"
        }
      ]
    }
  ],
  "postsubmit": [
    {
      "name": "CtsWindowManagerDeviceTestCases"
    }
  ],
  "imports": [
    {
      "path": "frameworks/base/services/core/java/com/android/server/am"
    }
  ]
}
```

### Setting attributes

In the above example, `presubmit` and `postsubmit` are the names of each **test
group**. Note that a test run for `postsubmit` will automatically include all
tests in the `presubmit` group. See
[Defining test groups](#defining_test_groups) for more information about test
groups.

The **name** of the **test module** or **Trade Federation integration test
name** (resource path to the test XML file, e.g.,
[uiautomator/uiautomator-demo](https://android.googlesource.com/platform/tools/tradefederation/contrib/+/master/res/config/uiautomator/uiautomator-demo.xml))
can be set in the value of the `name` attribute. Note the **name** field cannot
use class `name` or test method `name`. To narrow down the tests to run, you can
use options such as `include-filter` here. See
([include-filter sample usage](https://android.googlesource.com/platform/frameworks/base/+/master/services/core/java/com/android/server/pm/dex/TEST_MAPPING#7)).

The `imports` attribute allows you to include tests in other TEST_MAPPING files
without copying the content. Note that the TEST_MAPPING files in the parent
directories of the imported path will also be included.

The `options` attribute contains additional TradeFed command line options. In
the above example, only tests with annotation `Presubmit` will run in presubmit;
all tests will run in postsubmit.

To get a complete list of available options for a given test, run:

<pre>
<code class="devsite-terminal">tradefed.sh run commandAndExit [test_module] --help</code>
</pre>

Refer to
[TradeFed Option Handling ](/devices/tech/test_infra/tradefed/fundamentals/options)
for more details about how options work.

## Running tests with Atest

To execute the presubmit test rules locally:

1.  Go to the directory containing the TEST_MAPPING file.
1.  Run the command:

<pre>
<code class="devsite-terminal">atest</code>
</pre>

All presubmit tests configured in the TEST_MAPPING files of the current
directory and its parent directories are run. Atest will locate and run two tests
for presubmit (A and B).

This is the simplest way to run presubmit tests in TEST_MAPPING files in the
current working directory (CWD) and parent directories. Atest will locate and
use the TEST_MAPPING file in CWD and all of its parent directories, unless a
TEST_MAPPING file has `inherit_parent` set to false.

### Structuring source code

The following example shows how TEST_MAPPING files can be configured across the
source tree.

```
src
├── project_1
│   └── TEST_MAPPING
├── project_2
│   └── TEST_MAPPING
└── TEST_MAPPING
```

Content of `src/TEST_MAPPING`:

```
{
  "presubmit": [
    {
      "name": "A"
    }
  ]
}
```

Content of `src/project_1/TEST_MAPPING`:

```
{
  "presubmit": [
    {
      "name": "B"
    }
  ],
  "postsubmit": [
    {
      "name": "C"
    }
  ],
  "other_group": [
    {
      "name": "X"
    }
  ]}
```

Content of `src/project_2/TEST_MAPPING`:

```
{
  "presubmit": [
    {
      "name": "D"
    }
  ],
  "import": [
    {
      "path": "src/project_1"
    }
  ]}
```

### Specifying target directories

You can specify a target directory to run tests in TEST_MAPPING files in that
directory. The following command will run two tests (A, B).

<pre>
<code class="devsite-terminal">atest --test-mapping src/project_1</code>
</pre>

### Running postsubmit test rules

You can also use this command to run the postsubmit test rules defined in
TEST_MAPPING in `src_path` (default to CWD)
and its parent directories:

<pre>
<code class="devsite-terminal">atest [--test-mapping] [src_path]:postsubmit</code>
</pre>

### Identifying test groups

You can specify test groups in the Atest command. Note that presubmit tests are
part of postsubmit tests, as well. The following command will run all
**postsubmit** tests related to files in directory src/project_1, which are
three tests (A, B, C).

Or you can use **:all** to run all tests regardless of group. The following
command runs four tests (A, B, C, X):

<pre>
<code class="devsite-terminal">atest --test-mapping src/project_1:all</code>
</pre>

### Including subdirectories

By default, running tests in TEST_MAPPING with Atest will run only presubmit
tests configured in the TEST_MAPPING file in CWD (or
given directory) and its parent directories. If you want to run tests in all
TEST_MAPPING files in the sub-directories, use the option `--include-subdir` to
force Atest to include those tests too.

<pre>
<code class="devsite-terminal">atest --include-subdir</code>
</pre>

Without the `--include-subdir` option, Atest will run only test A. With the
`--include-subdir` option, Atest will run two tests (A, B).