2025 年 3 月 27 日より、AOSP のビルドとコントリビューションには aosp-main
ではなく android-latest-release
を使用することをおすすめします。詳細については、AOSP の変更をご覧ください。
Trade Federation でホストドリブン テストを作成する
コレクションでコンテンツを整理
必要に応じて、コンテンツの保存と分類を行います。
このページでは、ホストによって行われる JUnit4 スタイルのデバイステストを作成する方法について説明します。つまり、ハーネスのホスト側がデバイスに対してアクションをトリガーします。
なお、「ホスト側」テストと「ホストドリブン」テストは若干異なります。
- ホストドリブン テスト: 1 つまたは複数のデバイスと通信するホストで実行されるテスト。テスト中のシステム(SUT)はホスト自体には存在しませんが、ホストからテストされています。
- ホスト側テスト: 純粋にホスト上で実行されるテスト。テスト対象もホスト上のもののみになります(単体テストなど)。
インストゥルメンテーション テストではなくホストドリブン テストを作成する理由
テストによっては、再起動コマンドの発行など、デバイスの全体的な状態に影響を与える場合があります。インストゥルメンテーション テストの場合、再起動するとインストゥルメンテーションが終了し、テストを続行できず、結果も得られません。
ホストドリブン テストでは、テストが依存している外部デバイスとの通信を必要とする追加のセットアップ手順も実施できます。
ホストドリブン テストはこのようなユースケースを取り扱うことができ、より多くのシナリオでデバイスの高度なテストを実施できます。同じような状況であれば、ホストドリブン テストを作成する方が理にかなっています。
TF でホストドリブン テストを作成する方法
サンプルは次のとおりです。
@RunWith(DeviceJUnit4ClassRunner.class)
public class SampleHostJUnit4DeviceTest extends BaseHostJUnit4Test {
@Before
public void setUp() throws Exception {
// Some setup
}
@Test
public void testCheckWeHaveDevice() throws Exception {
Assert.assertNotNull(getDevice());
}
}
Trade Federation のホストドリブン テストは、DeviceJUnit4ClassRunner JUnit4 テストランナーによって行われます。テストクラスの全体的な構造は、通常の JUnit4 テストと同じです。
@BeforeClass
@Before
@Test
@After
@AfterClass
Assume
、Assert
BaseHostJunit4Test を拡張すると、次のような便利なテスト ユーティリティ API を継承できます。
installPackage
: ターゲット デバイスで APK をインストールできます。
installPackageAsUser
: ターゲット デバイスでユーザーとして APK をインストールできます。
uninstallPackage
: APK をアンインストールできます。
isPackageInstalled
: パッケージがインストールされているかどうかを確認します。
hasDeviceFeature
: デバイスが機能をサポートしているかどうかを確認します。
(pm list features
)
runDeviceTests(DeviceTestRunOptions options)
: DeviceTestRunOptions を使用して、ターゲット デバイスに対してインストゥルメンテーション テストを実行し、可能なオプションをすべて処理します。
Tradefed デバイス オブジェクトへのアクセスもできるようになります。
getDevice()
: デバイスを操作する TF デバイス オブジェクトを返します。
getBuild()
: ビルドに関する情報を取得するビルド情報 TF オブジェクトを返します。
getAbi()
: テストの実行対象である ABI を返します。
Tradefed のサポート: クラスごとのデバイスの準備とクリーンアップ
JUnit4 の @BeforeClass
と @AfterClass
は静的メソッドにのみ適用されるため、#getDevice()
ハンドラを使用してデバイス固有で 1 回限りのクラスごとの設定やクリーンアップを実行することはできません。この問題を解決するには、Tradefed アノテーションを使用します。
- @BeforeClassWithInfo: @BeforeClass アノテーションの前に実行
- @AfterClassWithInfo: @AfterClass アノテーションの後に実行
@BeforeClassWithInfo
public static void beforeClassWithDevice(TestInformation testInfo) {
assertNotNull(testInfo.getDevice());
testInfo.properties().put("mytest:test-prop", "test");
}
@AfterClassWithInfo
public static void afterClassWithDevice(TestInformation testInfo) {
assertNotNull(testInfo.getDevice());
testInfo.properties().put("mytest:test-prop", "test");
}
TestInformation
を使用すると、静的スコープと非静的スコープで使用できるデバイス プロパティとストア プロパティを使用できます。BaseHostJUnit4Test
は、#getTestInformation()
による非静的スコープでの TestInformation
の取得をサポートしています。
BaseHostJUnit4Test
を拡張していない場合、ITestInformationReceiver
を実装すると TestInformation
オブジェクトを受け取ることができます。
Tradefed XML 設定ファイルでは、HostTest ランナーをによりホストドリブン テストが実行されます。
<test class="com.android.tradefed.testtype.HostTest" >
<option name="class" value="android.sample.cts.SampleHostJUnit4DeviceTest" />
</test>
このページのコンテンツやコードサンプルは、コンテンツ ライセンスに記載のライセンスに従います。Java および OpenJDK は Oracle および関連会社の商標または登録商標です。
最終更新日 2025-03-26 UTC。
[[["わかりやすい","easyToUnderstand","thumb-up"],["問題の解決に役立った","solvedMyProblem","thumb-up"],["その他","otherUp","thumb-up"]],[["必要な情報がない","missingTheInformationINeed","thumb-down"],["複雑すぎる / 手順が多すぎる","tooComplicatedTooManySteps","thumb-down"],["最新ではない","outOfDate","thumb-down"],["翻訳に関する問題","translationIssue","thumb-down"],["サンプル / コードに問題がある","samplesCodeIssue","thumb-down"],["その他","otherDown","thumb-down"]],["最終更新日 2025-03-26 UTC。"],[],[],null,["# Write a host-driven test in Trade Federation\n\nThis page describes how to write a JUnit4-style device test driven by the host.\nThis means that the host side of the harness is going to trigger actions against\nthe device.\n\nNote that we consider \"host-side\" tests and \"host-driven\" tests to be slightly\ndifferent:\n\n- host-driven test: Is a test running on the host that interacts with one or more devices. The system under test (SUT) is not on the host itself but is being tested from the host.\n- host-side test: Is a test purely running on the host and testing something only on the host, for example unit tests.\n\nWhy create a host-driven test rather than an instrumentation test?\n------------------------------------------------------------------\n\nSome tests might require you to affect the device overall state, like issuing a\nreboot command. In the instrumentation test case, a reboot would kill the\ninstrumentation, the test could not continue, and no results would be available.\n\nHost-driven tests can also drive additional setup steps that require interaction\nwith external devices on which the test depends on.\n\nA host-driven test can handle these use cases and allow for advanced testing of\nthe device with more scenarios. If you are in that situation, writing a\nhost-driven test makes the most sense.\n\nHow are host-driven tests written in TF?\n----------------------------------------\n\nHere is a sample: \n\n @RunWith(DeviceJUnit4ClassRunner.class)\n public class SampleHostJUnit4DeviceTest extends BaseHostJUnit4Test {\n @Before\n public void setUp() throws Exception {\n // Some setup\n }\n\n @Test\n public void testCheckWeHaveDevice() throws Exception {\n Assert.assertNotNull(getDevice());\n }\n }\n\nHost-driven tests in Trade Federation are driven by the [DeviceJUnit4ClassRunner](https://android.googlesource.com/platform/tools/tradefederation/+/refs/heads/android16-release/test_framework/com/android/tradefed/testtype/DeviceJUnit4ClassRunner.java)\nJUnit4 test runner. The overall structure of the test class is the same as a\nregular JUnit4 test:\n\n- `@BeforeClass`\n- `@Before`\n- `@Test`\n- `@After`\n- `@AfterClass`\n- `Assume`, `Assert`\n\nExtending [BaseHostJunit4Test](https://android.googlesource.com/platform/tools/tradefederation/+/refs/heads/android16-release/test_framework/com/android/tradefed/testtype/junit4/BaseHostJUnit4Test.java)\nis a way to inherit useful testing utilities API such as:\n\n- `installPackage`: Allows to install an APK on the target device.\n- `installPackageAsUser`: Allows to install an APK as a user on the target device.\n- `uninstallPackage`: Allows to uninstall an APK.\n- `isPackageInstalled`: Check whether a package is installed or not.\n- `hasDeviceFeature`: Check whether device supports a feature or not. (`pm list features`)\n- `runDeviceTests(DeviceTestRunOptions options)`: Run an instrumentation test against a target device using [DeviceTestRunOptions](https://android.googlesource.com/platform/tools/tradefederation/+/refs/heads/android16-release/test_framework/com/android/tradefed/testtype/junit4/DeviceTestRunOptions.java) to handle all the possible options.\n\nAlso provide access to the Tradefed device object:\n\n- `getDevice()`: Returns a TF device object for manipulating the device.\n- `getBuild()`: Returns a build info TF object to get information about the build.\n- `getAbi()`: Returns the ABI the test is running against.\n\nTradefed support: Per-class device preparation and clean up\n-----------------------------------------------------------\n\nJUnit4 `@BeforeClass` and `@AfterClass` are only applicable to static methods,\nwhich makes it impossible to use the `#getDevice()` handler to do some\ndevice-specific, one-time, per-class setup or clean up. To solve this issue, use\nthe Tradefed annotation.\n\n- @BeforeClassWithInfo: Runs before @BeforeClass annotations\n- @AfterClassWithInfo: Runs after @AfterClass annotations\n\n @BeforeClassWithInfo\n public static void beforeClassWithDevice(TestInformation testInfo) {\n assertNotNull(testInfo.getDevice());\n testInfo.properties().put(\"mytest:test-prop\", \"test\");\n }\n\n @AfterClassWithInfo\n public static void afterClassWithDevice(TestInformation testInfo) {\n assertNotNull(testInfo.getDevice());\n testInfo.properties().put(\"mytest:test-prop\", \"test\");\n }\n\n`TestInformation` allows you to use the device and store properties that can be\nused either in the static or non-static scope. `BaseHostJUnit4Test` supports\ngetting the `TestInformation` in a non-static scope via `#getTestInformation()`.\n\nIf you are not extending `BaseHostJUnit4Test`, you can implement\n`ITestInformationReceiver` in order to receive the `TestInformation` object.\n\nHow to configure a host-driven test in Tradefed?\n------------------------------------------------\n\nIn Tradefed XML configuration file, host-driven tests are run through the\n[HostTest](https://android.googlesource.com/platform/tools/tradefederation/+/refs/heads/android16-release/test_framework/com/android/tradefed/testtype/HostTest.java)\nrunner. \n\n \u003ctest class=\"com.android.tradefed.testtype.HostTest\" \u003e\n \u003coption name=\"class\" value=\"android.sample.cts.SampleHostJUnit4DeviceTest\" /\u003e\n \u003c/test\u003e"]]