blob: 0f63584ab271ad904a316373e0a931c38bec9f1a [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="jar_java_host_tests" class="page-title">JAR (Java) 主机测试</h1>
<p>为了实现软件代码的完全覆盖率,应执行 JAR 主机测试。请按照相关说明<a href="https://developer.android.com/training/testing/unit-testing/local-unit-tests" class="external">构建本地单元测试</a>。要验证某项特定功能,只需编写小型单元测试即可。</p>
<h2 id="example">示例</h2>
<p>以下蓝图文件可提供符合您需求且可供模仿的简单 Hello World JAR 主机测试示例:
<a href="https://android.googlesource.com/platform/platform_testing/+/master/tests/example/jarhosttest/Android.bp">platform_testing/tests/example/jarhosttest/Android.bp</a></p>
<p>该示例对应以下位置的实际测试代码:
<a href="https://android.googlesource.com/platform/platform_testing/+/master/tests/example/jarhosttest/test/android/test/example/helloworld/HelloWorldTest.java">platform_testing/tests/example/jarhosttest/test/android/test/example/helloworld/HelloWorldTest.java</a></p>
<p>为了方便起见,我们在此处附上了该蓝图文件的快照:</p>
<pre class="prettyprint"><code> java_test_host {
name: "HelloWorldHostTest",
test_suites: ["general-tests"],
srcs: ["test/**/*.java"],
static_libs: [
"junit",
"mockito",
],
}
</code></pre>
<p>请注意,开头的 <code>java_host_test</code> 声明表示这是一项 JAR 主机测试。</p>
<h2 id="settings">设置</h2>
<p>下面对各项设置进行了说明:</p>
<pre class="prettyprint"><code> name: "HelloWorldHostTest",
</code></pre>
<p>如果指定了 <code>java_test_host</code> 模块类型(在块的开头),则需要进行 <code>name</code> 设置。这项设置可以为您的模块命名,而且生成的 JAR 将与模块命名相同,不过其名称会带有 <code>.jar</code> 后缀。例如,在本例中,生成的测试 JAR 将命名为 <code>HelloWorldHostTest.jar</code>。此外,此设置还可以为您的模块定义 make 目标名称,以便您可以使用 <code>make [options]
&lt;HelloWorldHostTest&gt;</code> 来编译测试模块及其所有依赖项。</p>
<pre class="prettyprint"><code> test_suites: ["general-tests"],
</code></pre>
<p><code>test_suites</code> 设置可让 Trade Federation 自动化测试框架轻松发现测试。可在此处添加其他套件(如 CTS),以便共享此测试。</p>
<pre class="prettyprint"><code> static_libs: [
"junit",
],
</code></pre>
<p><code>static_libs</code> 设置会指示编译系统将已命名模块的内容合并到当前模块的已生成的 APK 中。这意味着,每个已命名模块都会生成 <code>.jar</code> 文件,其内容将用于在编译期间解析类路径引用,以及合并到生成的 apk 中。</p>
</body></html>