blob: 6264734328510ab96279ecd17b605d891d033d3a [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="simple_test_configuration" class="page-title">简单的测试配置</h1>
<p>每个新的测试模块都必须具有配置文件,以使用模块元数据、编译时依赖项和打包指令来指引编译系统。</p>
<p><a href="https://android.googlesource.com/platform/build/soong/">Soong 编译系统</a>在 Android 8.0 (Oreo) 中引入,在 Android Q 中实现了对 <code>android_test</code> 的支持,现在可在 Android 开源项目 (AOSP) master 分支中找到它。</p>
<p>Soong 使用 Blueprint 文件或 <code>.bp</code> 文件,这些文件与 JSON 类似,用于对要编译的模块进行简单的声明性描述。此格式取代了以前的版本中使用的基于 Make 的系统。要适应自定义测试或使用 Android <a href="compatibility/cts">兼容性测试套件</a> (CTS),请改为按照<a href="/compatibility/tests/development/test-config">复杂的测试配置</a>操作。</p>
<h2 id="example">示例</h2>
<p>以下条目均来自这一示例 Blueprint 配置文件:<a href="https://android.googlesource.com/platform/platform_testing/+/master/tests/example/instrumentation/Android.bp">/platform_testing/tests/example/instrumentation/Android.bp</a></p>
<p>为方便起见,下面附上快照:</p>
<pre class="prettyprint"><code>android_test {
name: "HelloWorldTests",
srcs: ["src/**/*.java"],
sdk_version: "current",
static_libs: ["android-support-test"],
certificate: "platform",
test_suites: ["device-tests"],
}
</code></pre>
<p>请注意,开头的 <code>android_test</code> 声明表示这是一个测试。相反,如果开头为 <code>android_app</code>,则表示这是一个编译软件包。</p>
<h2 id="settings">设置</h2>
<p>下面对各项设置进行了解释:</p>
<pre class="prettyprint"><code> name: "HelloWorldTests",
</code></pre>
<p>如果指定了 <code>android_test</code> 模块类型(在代码块的开头),则需要 <code>name</code> 设置。此设置会为模块命名,生成的 APK 将与模块名称相同,不过带有 <code>.apk</code> 后缀。例如,在本例中,生成的测试 apk 将命名为 <code>HelloWorldTests.apk</code>。此外,此设置还可以为模块定义 make 目标名称,以便您可以使用 <code>make [options]
&lt;HelloWorldTests&gt;</code> 编译测试模块及其所有依赖项。</p>
<pre class="prettyprint"><code> static_libs: ["android-support-test"],
</code></pre>
<p><code>static_libs</code> 设置指示编译系统将已命名模块的内容合并到当前模块生成的 apk 中。这意味着,每个已命名模块都会生成 <code>.jar</code> 文件,其内容将用于在编译期间解析类路径引用,以及合并到生成的 apk 中。</p>
<p>在本例中,可能对测试普遍有用的内容如下:</p>
<p><code>android-support-test</code> 是 Android 测试支持库的预编译项,包括新的测试运行器 <code>AndroidJUnitRunner</code>:它替代了现已弃用的内置 <code>InstrumentationTestRunner</code>,并且支持 JUnit4 测试框架。如需了解详情,请参阅<a href="https://developer.android.com/training/testing/">在 Android 平台上测试应用</a></p>
<p>如果要编译一个新的插桩模块,则开始时应始终将 <code>android-support-test</code> 库作为测试运行器。平台源代码树还包括其他有用的测试框架,如 <code>ub-uiautomator</code><code>mockito-target</code><code>easymock</code> 等等。</p>
<pre class="prettyprint"><code> certificate: "platform",
</code></pre>
<p><code>certificate</code> 设置指示编译系统使用与核心平台相同的证书对 apk 进行签名。如果您的测试使用受签名保护的权限或 API,则需要此设置。请注意,此设置适合平台连续测试,但不应该用于 CTS 测试模块。<em></em>另请注意,本例使用此证书设置只是为了进行说明:示例中的测试代码实际上不需要使用特殊平台证书对测试 apk 进行签名。</p>
<p>如果您要为系统服务器之外的组件(也就是说,它的打包方式多少有些类似于常规应用 apk,只不过它内置到系统映像中并且可能是特权应用)编写插桩,那么插桩可能会以组件的应用软件包(请参阅下面关于清单的部分)为目标。在这种情况下,应用 makefile 可能具有自己的 <code>certificate</code> 设置,并且插桩模块应保留相同的设置。这是因为,要以受测应用上的插桩为目标,必须使用同一证书对测试 apk 和应用 apk 进行签名。</p>
<p>在其他情况下,根本不需要此设置:编译系统将直接使用默认的内置证书(基于编译变体)对其进行签名,并且它通常称为 <code>dev-keys</code></p>
<pre class="prettyprint"><code> test_suites: ["device-tests"],
</code></pre>
<p><code>test_suites</code> 设置使 Trade Federation 自动化测试框架很容易发现测试。可以在此处添加其他套件(如 CTS),以便共享此测试。</p>
<pre class="prettyprint"><code>${ANDROID_PRODUCT_OUT}/testcases/HelloWorldTests/HelloWorldTests.apk
</code></pre>
</body></html>