blob: c2736746649b1c083eedffa2de41d0a8651a88e5 [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="adding_a_new_native_test_example" class="page-title">添加新的原生测试示例</h1>
<p>如果您不熟悉 Android 平台开发,那么您可能会发现,这个从头开始添加全新原生测试的完整示例很有用,可以帮助您了解所涉及的典型工作流。此外,如果您也不熟悉 C++ 的 gtest 框架,请访问 <a href="https://github.com/google/googletest">gtest 项目网站</a>以查看其他文档。</p>
<p>本指南使用以下测试作为示例:</p>
<p><a href="https://android.googlesource.com/platform/platform_testing/+/master/tests/example/native/">Hello World 原生测试</a></p>
<p>建议您先浏览代码以获得粗略的印象,然后再继续。</p>
<h2 id="deciding_on_a_source_location">确定源代码所在的位置</h2>
<p>通常,您的团队已有既定的放置模式,在既定的位置检入代码,并且在既定的位置添加测试。大多数团队拥有单个 git 代码库,或与其他团队共享一个代码库,但有一个包含组件源代码的专用子目录。</p>
<p>假设组件源代码的根位置是在 <code>&lt;component source
root&gt;</code>,大多数组件在该位置下具有 <code>src</code><code>tests</code> 文件夹,以及一些其他文件,如 <code>Android.mk</code>(或拆分为额外的 <code>.bp</code> 文件)。</p>
<p>由于您要添加全新的测试,因此或许需要在组件 <code>src</code> 旁边创建 <code>tests</code> 目录,并为其填充内容。</p>
<p>在某些情况下,您的团队可能会在 <code>tests</code> 下设置更深的目录结构,因为需要将不同的测试套件打包到单独的二进制文件中。在这种情况下,您需要在 <code>tests</code> 下创建一个新的子目录。</p>
<p>为了进行说明,下面给出了具有单个 <code>tests</code> 文件夹的组件的典型目录大纲:</p>
<pre class="prettyprint"><code>\
&lt;component source root&gt;
\-- Android.bp (component makefile)
\-- AndroidTest.bp (test config file)
\-- src (component source)
| \-- foo.cpp
| \-- ...
\-- tests (test source root)
\-- Android.bp (test makefile)
\-- src (test source)
\-- foo_test.cpp
\-- ...
</code></pre>
<p>此外,下面还给出了具有多个测试源文件目录的组件的典型目录大纲:</p>
<pre class="prettyprint"><code>\
&lt;component source root&gt;
\-- Android.bp (component makefile)
\-- AndroidTest.bp (test config file)
\-- src (component source)
| \-- foo.cpp
| \-- ...
\-- tests (test source root)
\-- Android.bp (test makefile)
\-- testFoo (sub test source root)
| \-- Android.bp (sub test makefile)
| \-- src (sub test source)
| \-- test_foo.cpp
| \-- ...
\-- testBar
| \-- Android.bp
| \-- src
| \-- test_bar.cpp
| \-- ...
\-- ...
</code></pre>
<p>不管是什么样的结构,您最终都需要在 <code>tests</code> 目录或新建子目录中添加文件,并且文件应类似于示例 gerrit 更改中的 <code>native</code> 目录中的文件。下面几部分将进一步详细说明各个文件。</p>
<h2 id="source_code">源代码</h2>
<p>有关示例,请参阅 <a href="https://android.googlesource.com/platform/platform_testing/+/master/tests/example/native/HelloWorldTest.cpp">Hello World 原生测试</a></p>
<p>带注解的源代码如下所示:</p>
<pre class="prettyprint lang-c++"><code>#include &lt;gtest/gtest.h&gt;
</code></pre>
<p>gtest 的头文件。请注意,使用 makefile 中的 <code>BUILD_NATIVE_TEST</code> 会自动解析头文件依赖项</p>
<pre class="prettyprint lang-c++"><code>#include &lt;stdio.h&gt;
TEST(HelloWorldTest, PrintHelloWorld) {
printf("Hello, World!");
}
</code></pre>
<p>gtest 使用 <code>TEST</code> 宏编写而成:第一个参数是测试用例名称,第二个是测试名称;在结果信息中心查看时,它们与测试二进制文件名称一起形成下面的层次结构:</p>
<pre class="prettyprint"><code>&lt;test binary 1&gt;
| \-- &lt;test case 1&gt;
| | \-- &lt;test 1&gt;
| | \-- &lt;test 2&gt;
| | \-- ...
| \-- &lt;test case 2&gt;
| | \-- &lt;test 1&gt;
| | \-- ...
| \-- ...
&lt;test binary 2&gt;
|
...
</code></pre>
<p>有关使用 gtest 编写测试的更多信息,请参阅其文档:</p>
<ul>
<li>https://github.com/google/googletest/blob/master/googletest/docs/Primer.md</li>
</ul>
<h2 id="simple_configuration_file">简单配置文件</h2>
<p>每个新的测试模块都必须具有配置文件,以使用模块元数据、编译时依赖项和打包指令来指引编译系统。在大多数情况下,基于 Soong 的 Blueprint 文件选项就足够了。如需了解详情,请参阅<a href="blueprints.md">简单的测试配置</a></p>
<h2 id="complex_configuration_file">复杂配置文件</h2>
<aside class="special"><strong>重要提示</strong><span>只有 CTS 测试或需要特殊设置(如停用蓝牙或收集示例数据)的测试需要遵循本部分中的说明。其他所有用例均可通过<a href="blueprints.md">简单的测试配置</a>来涵盖。如需了解适用于本部分的详细信息,请参阅<a href="compatibility/tests/development/test-config">复杂的测试配置</a></span></aside>
<p>要改用 Trade Federation,请为 Android 的自动化测试框架 <a href="/devices/tech/test_infra/tradefed/">Trade Federation</a> 编写测试配置文件。</p>
<p>测试配置可以指定特殊的设备设置选项和默认参数来提供测试类。</p>
<h2 id="build_and_test_locally">在本地编译和测试</h2>
<p>对于最常见的用例,请使用 <a href="/compatibility/tests/development/atest">Atest</a></p>
<p>对于需要更繁琐自定义设置的更复杂用例,请遵循<a href="instrumentation.md">插桩说明</a></p>
</body></html>