blob: 57820d64ad3f14aa72dfa86875b381115c05d22d [file] [log] [blame]
<html devsite><head>
<meta name="book_path" value="/_book.yaml"/>
<meta name="project_path" value="/_project.yaml"/>
</head>
<body>
<!--
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.
-->
<h1 id="test_mapping" class="page-title">Test Mapping</h1>
<p>本文简要介绍了 Test Mapping,并说明了如何在 Android 开源项目 (AOSP) 中轻松开始配置测试。</p>
<h2 id="what_is_test_mapping">什么是 Test Mapping?</h2>
<p>Test Mapping 是一种基于 Gerrit 的方法,让开发者能够直接在 Android 源代码树中创建提交前规则和提交后规则,并将要测试的分支和设备的决策留给测试基础架构本身。Test Mapping 定义是名为 TEST_MAPPING 的 JSON 文件,该文件可放置在任何源目录中。</p>
<p><a href="atest">Atest</a> 可以使用 TEST_MAPPING 文件在相关目录中运行提交前测试。借助 Test Mapping,您只需在 Android 源代码树中进行简单的更改,即可将同一组测试添加到提交前检查。</p>
<p>请参阅以下示例:</p>
<p><a href="https://android.googlesource.com/platform/frameworks/base/+/master/services/core/java/com/android/server/pm/dex/TEST_MAPPING">针对 services.core 将提交前测试添加到 TEST_MAPPING</a></p>
<p><a href="https://android.googlesource.com/platform/frameworks/base/+/master/startop/iorap/TEST_MAPPING">针对 startop/iorap 将提交前测试和提交后测试添加到 TEST_MAPPING</a></p>
<h2 id="defining_test_groups">定义测试组</h2>
<p>Test Mapping 组通过<strong>测试组</strong>进行测试。测试组的名称可以是任何字符串。例如,presubmit 可用于在验证更改时运行的测试组。<em></em>postsubmit 测试可在更改合并后用于验证构建。<em></em></p>
<h2 id="packaging_build_script_rules">打包构建脚本规则</h2>
<p>为了让 <a href="/devices/tech/test_infra/tradefed">Trade Federation 自动化测试框架</a>针对指定版本运行 Test Mapping 的测试模块,这些模块必须针对 <a href="blueprints">Soong</a><strong>test_suite</strong>(或针对 Make 将 <strong>LOCAL_COMPATIBILITY_SUITE</strong>)设为以下两个套件之一:</p>
<ul>
<li><strong>device-tests</strong> - 针对特定设备 CPU 构建</li>
<li><strong>general-tests</strong> - 针对任何应用二进制接口 (ABI) 构建</li>
</ul>
<p>如有疑问,请将 gtest 放入 device-test,并将 APK 测试放入 general-test。<em></em><em></em></p>
<p>示例:</p>
<pre class="prettyprint"><code>Android.bp: test_suites: ["device-tests"],
Android.mk: LOCAL_COMPATIBILITY_SUITE := device-tests
</code></pre>
<h2 id="creating_test_mapping_files">创建 Test Mapping 文件</h2>
<p>对于需要测试覆盖的目录,只需添加与以下示例类似的 TEST_MAPPING JSON 文件即可。这些规则可以确保当此目录或其子目录中的任何文件被访问时,相应测试会在提交前检查中运行。</p>
<h3 id="following_an_example">举个例子</h3>
<p>以下是一个示例 TEST_MAPPING 文件:</p>
<pre class="prettyprint"><code>{
"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"
}
]
}
</code></pre>
<h3 id="setting_attributes">设置属性</h3>
<p>在上面的示例中,<code>presubmit</code><code>postsubmit</code> 分别是每个<strong>测试组</strong>的名称。请注意,<code>postsubmit</code> 的测试运行将自动包含 <code>presubmit</code> 组中的所有测试。如需详细了解测试组,请参阅<a href="#defining_test_groups">定义测试组</a></p>
<p><strong>测试模块</strong><strong>名称</strong><strong>Trade Federation 集成测试名称</strong>(指向测试 XML 文件的资源路径,例如 <a href="https://android.googlesource.com/platform/tools/tradefederation/contrib/+/master/res/config/uiautomator/uiautomator-demo.xml">uiautomator/uiautomator-demo</a>)可在 <code>name</code> 属性的值中设置。请注意,<strong>name</strong> 字段不可使用类 <code>name</code> 或测试方法 <code>name</code>。要缩减所运行的测试,您可以在此处使用 <code>include-filter</code> 等选项请参阅(<a href="https://android.googlesource.com/platform/frameworks/base/+/master/services/core/java/com/android/server/pm/dex/TEST_MAPPING#7">include-filter</a> 使用示例)。</p>
<p>借助 <code>imports</code> 属性,您可以包含其他 TEST_MAPPING 文件中的测试,而无需复制相应内容。请注意,导入路径的父目录中的 TEST_MAPPING 文件也将包含在内。</p>
<p><code>options</code> 属性包含其他 TradeFed 命令行选项。在上面的示例中,只有带有注释 <code>Presubmit</code> 的测试才会在提交前测试中运行;所有测试都会在提交后测试中运行。</p>
<p>要获取指定测试可用选项的完整列表,请运行以下命令:</p>
<pre>
<code class="devsite-terminal">tradefed.sh run commandAndExit [test_module] --help</code>
</pre>
<p>如需详细了解各选项的工作原理,请参阅 <a href="/devices/tech/test_infra/tradefed/fundamentals/options">TradeFed 选项处理</a></p>
<h2 id="running_tests_with_atest">使用 Atest 运行测试</h2>
<p>要在本地执行提交前测试规则,请执行以下操作:</p>
<ol>
<li>转到包含 TEST_MAPPING 文件的目录。</li>
<li>运行以下命令:</li>
</ol>
<pre>
<code class="devsite-terminal">atest</code>
</pre>
<p>在当前目录及其父目录的 TEST_MAPPING 文件中配置的所有提交前测试都会运行。Atest 将针对提交前测试找到两个测试(A 和 B)并加以运行。</p>
<p>要运行当前工作目录 (CWD) 和父目录中的 TEST_MAPPING 文件中的提交前测试,这是最简单的方法。Atest 会找到 CWD 及其所有父目录中的 TEST_MAPPING 文件并使用这些文件,除非 TEST_MAPPING 文件将 <code>inherit_parent</code> 设为 false。</p>
<h3 id="structuring_source_code">构建源代码</h3>
<p>以下示例显示了如何在源代码树中配置 TEST_MAPPING 文件。</p>
<pre class="prettyprint"><code>src
├── project_1
│ └── TEST_MAPPING
├── project_2
│ └── TEST_MAPPING
└── TEST_MAPPING
</code></pre>
<p><code>src/TEST_MAPPING</code> 的内容:</p>
<pre class="prettyprint"><code>{
"presubmit": [
{
"name": "A"
}
]
}
</code></pre>
<p><code>src/project_1/TEST_MAPPING</code> 的内容:</p>
<pre class="prettyprint"><code>{
"presubmit": [
{
"name": "B"
}
],
"postsubmit": [
{
"name": "C"
}
],
"other_group": [
{
"name": "X"
}
]}
</code></pre>
<p><code>src/project_2/TEST_MAPPING</code> 的内容:</p>
<pre class="prettyprint"><code>{
"presubmit": [
{
"name": "D"
}
],
"import": [
{
"path": "src/project_1"
}
]}
</code></pre>
<h3 id="specifying_target_directories">指定目标目录</h3>
<p>您可以指定一个目标目录,以便在该目录中运行 TEST_MAPPING 文件中的测试。以下命令可运行两个测试(A、B)。</p>
<pre>
<code class="devsite-terminal">atest --test-mapping src/project_1</code>
</pre>
<h3 id="running_postsubmit_test_rules">运行提交后测试规则</h3>
<p>您还可以使用此命令运行在 <code>src_path</code>(默认为 CWD)及其父目录中的 TEST_MAPPING 中定义的提交后测试规则:</p>
<pre>
<code class="devsite-terminal">atest [--test-mapping] [src_path]:postsubmit</code>
</pre>
<h3 id="identifying_test_groups">识别测试组</h3>
<p>您可以在 Atest 命令中指定测试组。请注意,提交前测试也是提交后测试的一部分。以下命令可运行与 directory src/project_1 中的文件相关的所有<strong>提交后</strong>测试,即三个测试(A、B、C)。</p>
<p>或者,您也可以使用 <strong>:all</strong> 来运行所有测试(无论测试属于哪个组)。以下命令可运行四个测试(A、B、C、X):</p>
<pre>
<code class="devsite-terminal">atest --test-mapping src/project_1:all</code>
</pre>
<h3 id="including_subdirectories">包含子目录</h3>
<p>默认情况下,如果使用 Atest 运行 TEST_MAPPING 中的测试,则系统仅运行在 CWD(或指定目录)及其父目录中的 TEST_MAPPING 文件中配置的提交前测试。如果您想运行子目录中的所有 TEST_MAPPING 文件中的测试,请使用选项 <code>--include-subdir</code> 强制 Atest 将这些测试一并包含在内。</p>
<pre>
<code class="devsite-terminal">atest --include-subdir</code>
</pre>
<p>如果未使用 <code>--include-subdir</code> 选项,则 Atest 将仅运行测试 A。如果使用了 <code>--include-subdir</code> 选项,则 Atest 将运行两个测试(A、B)。</p>
</body></html>