blob: 1bf9fe214a4032a08eb22fffcf72158b8ee2e6a9 [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="complex_test_configuration" class="page-title">复杂的测试配置</h1>
<aside class="special"><strong>重要提示</strong><span>只有 Android <a href="compatibility/cts">兼容性测试套件</a> (CTS) 测试或需要特殊设置(如停用蓝牙或收集示例数据)的测试需要遵循此页面上的说明。其他所有用例均可通过使用 Blueprint 的基于 Soong 的<a href="blueprints.md">简单测试配置</a>来涵盖,这些 Blueprint 能够自动处理以前手动执行的大部分配置操作。</span></aside>
<p>某些测试模块可能需要执行在测试用例本身内无法执行的自定义设置和拆解步骤。典型的示例可能包括:</p>
<ul>
<li>安装其他 apk(除了测试 apk 之外)</li>
<li>将某些文件推送到设备</li>
<li>运行命令(例如 adb shell pm …)</li>
</ul>
<p>过去,组件团队通常依靠编写主机端测试来执行此类任务,这需要了解 Trade Federation 自动化测试框架,并且通常会提高测试模块的复杂性。</p>
<p>我们引入了测试模块配置的概念(借鉴于 CTS)来支持此类任务,只需几行配置即可完成上面列出的常见任务。为了达到最大的灵活性,您甚至可以实现自己的目标准备器(由 <a href="/reference/com/android/tradefed/targetprep/ITargetPreparer.html">ITargetPreparer</a><a href="/reference/com/android/tradefed/targetprep/ITargetCleaner.html">ITargetCleaner</a> 定义),并对其进行配置以在您自己的测试模块配置中使用。</p>
<p>测试模块的测试模块配置是添加到顶级模块源文件夹的必需 XML 文件,名为“AndroidTest.xml”。该 XML 文件遵循 Trade Federation 自动化测试框架使用的配置文件的格式。目前,通过测试模块配置处理的主要标记是“target_preparer”和“test”标记。</p>
<h2 id="target_preparers">目标准备器</h2>
<p>顾名思义,“target_preparer”标记会定义目标准备器(请参阅 <a href="/reference/com/android/tradefed/targetprep/ITargetPreparer.html">ITargetPreparer</a>),该目标准备器提供了一种设置方法,在执行测试模块以进行测试之前会调用该方法;如果“target_preparer”标记中引用的类也实现了 <a href="/reference/com/android/tradefed/targetprep/ITargetCleaner.html">ITargetCleaner</a>,则在测试模块完成后将调用其拆解方法。</p>
<p>要使用内置的通用模块配置,请在测试模块的顶级文件夹中添加一个新文件“AndroidTest.xml”,并在该文件里填充以下内容:</p>
<pre class="prettyprint lang-xml"><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;!-- [insert standard AOSP copyright here] --&gt;
&lt;configuration description="Test module config for Foo"&gt;
&lt;!-- insert options here --&gt;
&lt;/configuration&gt;
</code></pre>
<p>例如,我们可以添加以下选项标记(在上面的“insert”注释处):</p>
<pre class="prettyprint lang-xml"><code> &lt;target_preparer class="com.android.tradefed.targetprep.RunCommandTargetPreparer"&gt;
&lt;option name="run-command" value="settings put secure accessibility_enabled 1" /&gt;
&lt;option name="teardown-command" value="settings put secure accessibility_enabled 0" /&gt;
&lt;/target_preparer&gt;
</code></pre>
<p>这些选项会将自动化测试框架配置为:</p>
<ol>
<li>在调用测试模块之前,在设备上执行 shell 命令“settings put secure accessibility_enabled 1”</li>
<li>在测试模块完成之后,执行 shell 命令“settings put secure accessibility_enabled 0”</li>
</ol>
<p>在此特定示例中,分别在测试模块执行之前/之后启用/停用可访问性。通过一个简单的示例演示,可以详细介绍如何使用“option”标记。如上所示,该标记可以有两个属性:名称和值。名称属性指示选项的名称,并进一步分解成由冒号分隔的两部分:一部分是准备器的简称,另一部分是准备器提供的实际选项名称。</p>
<p>值字段的确切用途取决于准备器如何定义选项:它可以是字符串、数字、布尔值,甚至可以是文件路径等等。在上面的示例中,名称“run-command:run-command”表示我们正在为由简称为“run-command”的目标准备器定义的选项“run-command”设置值;名称“run-command:teardown-command”表示我们正在为也是由简称为“run-command”的同一目标准备器定义的选项“teardown-command”设置值。下面总结了三个常见的目标准备器:</p>
<ul>
<li><p>类名:<a href="https://android.googlesource.com/platform/tools/tradefederation/+/master/src/com/android/tradefed/targetprep/PushFilePreparer.java">PushFilePreparer</a></p>
<ul>
<li><strong>简称</strong>:push-file</li>
<li><strong>功能</strong>:将测试用例文件夹下的任意文件推送到设备上的目标</li>
<li><strong>注意</strong><ul>
<li>此准备器可以从文件夹推送到文件夹,或者从文件推送到文件;也就是说,您无法将文件推送到设备上的文件夹下,您还必须指定该文件夹下的目标文件名</li>
</ul></li>
<li><strong>选项</strong><ul>
<li><strong>push</strong>:一种推送规范,格式为“<code>/path/to/srcfile.txt-&gt;/path/to/destfile.txt</code>”或“<code>/path/to/srcfile.txt-&gt;/path/to/destdir/</code>”。可以重复
此路径可能相对于测试模块目录或输出目录本身。</li>
<li>**post-push**:尝试完所有推送后在设备上运行的命令(使用 `<code>adb shell
&lt;your command&gt;</code>`)。典型用例是使用 chmod 命令添加权限</li>
</ul></li>
</ul></li>
<li><p>类名:<a href="https://android.googlesource.com/platform/tools/tradefederation/+/master/src/com/android/tradefed/targetprep/InstallApkSetup.java">InstallApkSetup</a></p>
<ul>
<li><strong>简称</strong>:install-apk</li>
<li><strong>功能</strong>:将任意 apk 文件推送到设备上的目标</li>
<li><strong>选项:</strong>
<ul>
<li><strong>test-file-name</strong>:要安装到设备上的 apk 的名称。</li>
<li><strong>install-arg</strong>:要传递给 pm install 命令的其他参数,包括前导短划线,例如“-d”。可以重复</li>
</ul></li>
</ul></li>
<li><p>类名:<a href="https://android.googlesource.com/platform/tools/tradefederation/+/master/src/com/android/tradefed/targetprep/RunCommandTargetPreparer.java">RunCommandTargetPreparer</a></p>
<ul>
<li><strong>简称</strong>:run-command</li>
<li><strong>功能</strong>:在测试模块执行之前或之后执行任意 shell 命令</li>
<li><strong>选项:</strong>
<ul>
<li><strong>run-command</strong>:要运行的 adb shell 命令。可以重复</li>
<li><strong>teardown-command</strong>:要在拆解阶段运行的 adb shell 命令。可以重复</li>
</ul></li>
</ul></li>
</ul>
<h2 id="test_class">测试类</h2>
<p>测试类是用于执行测试的 Trade Federation 类。</p>
<pre class="prettyprint lang-xml"><code>&lt;test class="com.android.tradefed.testtype.AndroidJUnitTest"&gt;
&lt;option name="package" value="android.test.example.helloworld"/&gt;
&lt;option name="runner" value="android.support.test.runner.AndroidJUnitRunner"/&gt;
&lt;/test&gt;
</code></pre>
<p>下面是三个常见的测试类:</p>
<ul>
<li><p>类名:<a href="https://android.googlesource.com/platform/tools/tradefederation/+/master/src/com/android/tradefed/testtype/GTest.java">GTest</a></p>
<ul>
<li><strong>简称</strong>:gtest</li>
<li><strong>功能</strong>:在给定设备上运行原生测试软件包的测试。</li>
<li><strong>选项:</strong>
<ul>
<li><strong>native-test-device-path</strong>:原生测试在设备上所在的路径。</li>
</ul></li>
</ul></li>
<li><p>类名:<a href="https://android.googlesource.com/platform/tools/tradefederation/+/master/src/com/android/tradefed/testtype/InstrumentationTest.java">InstrumentationTest</a></p>
<ul>
<li><strong>简称</strong>:instrumentation</li>
<li><strong>功能</strong>:在给定设备上运行插桩测试软件包的测试</li>
<li><strong>选项:</strong>
<ul>
<li><strong>package</strong>:要运行的 Android 测试应用的清单软件包名称。</li>
<li><strong>class</strong>:要运行的测试类名称。</li>
<li><strong>method</strong>:要运行的测试方法名称。</li>
</ul></li>
</ul></li>
<li><p>类名:<a href="https://android.googlesource.com/platform/tools/tradefederation/+/master/src/com/android/tradefed/testtype/AndroidJUnitTest.java">AndroidJUnitTest</a></p>
<ul>
<li><strong>功能</strong>:使用 android.support.test.runner.AndroidJUnitRunner 在给定设备上运行插桩测试软件包的测试。这是执行插桩测试的主要方法。</li>
</ul></li>
</ul>
</body></html>