AndroidJUnitRunner

AndroidJUnitRunner クラスは JUnit テストランナーです。 Android デバイスでインストルメンテーション JUnit 4 テストを実行できます。 EspressoUI AutomatorCompose を使用する場合を含む テスト フレームワークです。

テストランナーは、テスト パッケージとテスト対象アプリを テストの実行、テスト結果の報告です。

このテストランナーは、次のようないくつかの一般的なテストタスクをサポートします。

JUnit テストを作成する

次のコード スニペットは、インストゥルメント化された JUnit 4 を記述する方法を示しています。 ChangeTextBehaviorchangeText オペレーションを検証するテスト クラスは正しく動作します。

Kotlin

@RunWith(AndroidJUnit4::class) // Only needed when mixing JUnit 3 and 4 tests
@LargeTest // Optional runner annotation
class ChangeTextBehaviorTest {
 val stringToBeTyped = "Espresso"
 // ActivityTestRule accesses context through the runner
 @get:Rule
 val activityRule = ActivityTestRule(MainActivity::class.java)

 @Test fun changeText_sameActivity() {
 // Type text and then press the button.
 onView(withId(R.id.editTextUserInput))
 .perform(typeText(stringToBeTyped), closeSoftKeyboard())
 onView(withId(R.id.changeTextBt)).perform(click())

 // Check that the text was changed.
 onView(withId(R.id.textToBeChanged))
 .check(matches(withText(stringToBeTyped)))
 }
}

Java

@RunWith(AndroidJUnit4.class) // Only needed when mixing JUnit 3 and 4 tests
@LargeTest // Optional runner annotation
public class ChangeTextBehaviorTest {

    private static final String stringToBeTyped = "Espresso";

    @Rule
    public ActivityTestRuleM<ainActivity;> activityRule =
            new ActivityTestRule;<>(MainActivity.class);

    @Test
    public void changeText_sameActivity() {
        // Type text and then press the button.
        onView(withId(R.id.editTextUserInput))
                .perform(typeText(stringToBeTyped), closeSoftKeyboard());
        onView(withId(R.id.changeTextBt)).perform(click());

        // Check that the text was changed.
        onView(withId(R.id.textToBeChanged))
                .check(matches(withText(stringToBeTyped)));
    }
}

アプリケーションのコンテキストにアクセスする

AndroidJUnitRunner を使用してテストを実行する場合、 静的メソッドを呼び出して、テスト対象アプリに追加します。 ApplicationProvider.getApplicationContext() メソッドを使用します。カスタム Application のサブクラスを使用している場合、このメソッドはカスタム 渡します。

ツール実装者は、 InstrumentationRegistry クラス。このクラスには、 Instrumentation オブジェクト、ターゲット アプリの Context オブジェクト、テスト アプリの Context オブジェクト、テストに渡されるコマンドライン引数。

テストをフィルタリングする

JUnit 4.x テストでは、アノテーションを使用して実行するテストを構成できます。この 機能を使用すれば、コードにボイラープレートや条件付きコードを テストです。このテストでは、JUnit 4 でサポートされている標準のアノテーションに加えて、 runner は、次のような Android 固有のアノテーションもサポートしています。 次のとおりです。

  • @RequiresDevice: 物理デバイスに対してのみテストを実行するよう指定します。 エミュレータではなくデバイスです
  • @SdkSuppress: 下位の Android API でテストが実行されないようにします。 レベル 表示されます。たとえば、すべての API レベル 実行できないようにするには、アノテーション @SDKSuppress(minSdkVersion=23) を使用します。
  • @SmallTest@MediumTest@LargeTest: テスト期間を分類します。 テストの実行頻度も考慮しますマイページ このアノテーションを使用して、実行するテストをフィルタできます。 android.testInstrumentationRunnerArguments.size プロパティ:
-Pandroid.testInstrumentationRunnerArguments.size=small

テストをシャーディングする

テストの実行を並列化する必要がある場合は、 複数のサーバーを高速に実行したり グループに分割したり シャード。テストランナーは、1 つのテストスイートを複数に分割することができる 分割できるので、同じシャードに属するテストを できます。各シャードはインデックス番号で識別されます。テストを実行するときは、 -e numShards オプションを使用すると、作成するシャードの数と、 -e shardIndex オプションを使用して、実行するシャードを指定します。

たとえば、テストスイートを 10 個のシャードに分割し、テストのみを実行する場合 次の adb コマンドを使用します。

adb shell am instrument -w -e numShards 10 -e shardIndex 2

Android Test Orchestrator を使用する

Android Test Orchestrator を使用すると、 Instrumentation の独自の呼び出し。AndroidJUnitRunner バージョン 1.0 を使用する場合 Android Test Orchestrator にアクセスできる。

Android Test Orchestrator には、テストにおける次の利点があります。 環境:

  • 最小共有状態: 各テストは独自の Instrumentation で実行されます。 作成します。そのため、テストでアプリの状態を共有している場合、共有された状態のほとんどは 各テスト後にデバイスの CPU またはメモリから削除される。 共有状態を削除するたびに、デバイスの CPU とメモリからすべてclearPackageData フラグを使用します。Gradle から有効にするをご覧ください。 ご覧ください。
  • クラッシュの分離: 1 つのテストがクラッシュした場合でも、 Instrumentation の独自のインスタンス。つまり、同じインフラストラクチャ内の 完全なテスト結果を得ることができます

この分離により、テスト実行時間が長くなるため、テスト実行時間が長くなることがあります。 Android Test Orchestrator は、各テストの後にアプリを再起動します。

Android Studio と Firebase Test Lab の両方に Android Test Orchestrator が搭載されている プリインストールされていますが、この機能を有効にするには、Android 。

Gradle から有効にする

Gradle コマンドライン ツールを使用して Android Test Orchestrator を有効にするには、次の手順を完了します。 手順は次のとおりです。

  • ステップ 1: Gradle ファイルを変更する次のステートメントを プロジェクトの build.gradle ファイルに追加します。
android {
 defaultConfig {
  ...
  testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

  // The following argument makes the Android Test Orchestrator run its
  // "pm clear" command after each test invocation. This command ensures
  // that the app's state is completely cleared between tests.
  testInstrumentationRunnerArguments clearPackageData: 'true'
 }

 testOptions {
  execution 'ANDROIDX_TEST_ORCHESTRATOR'
 }
}

dependencies {
 androidTestImplementation 'androidx.test:runner:1.1.0'
 androidTestUtil 'androidx.test:orchestrator:1.1.0'
}
  • ステップ 2: 次のコマンドを実行して Android Test Orchestrator を実行します。
./gradlew connectedCheck

Android Studio から有効にする

Android Studio で Android Test Orchestrator を有効にするには、次に示すステートメントを追加します。 「Enable from Gradle」をアプリの build.gradle ファイルに追加します。

コマンドラインから有効にする

コマンドラインで Android Test Orchestrator を使用するには、次のコマンドを実行します。 ターミナル ウィンドウで次のコマンドを実行します。

DEVICE_API_LEVEL=$(adb shell getprop ro.build.version.sdk)

FORCE_QUERYABLE_OPTION=""
if [[ $DEVICE_API_LEVEL -ge 30 ]]; then
   FORCE_QUERYABLE_OPTION="--force-queryable"
fi

# uninstall old versions
adb uninstall androidx.test.services
adb uninstall androidx.test.orchestrator

# Install the test orchestrator.
adb install $FORCE_QUERYABLE_OPTION -r path/to/m2repository/androidx/test/orchestrator/1.4.2/orchestrator-1.4.2.apk

# Install test services.
adb install $FORCE_QUERYABLE_OPTION -r path/to/m2repository/androidx/test/services/test-services/1.4.2/test-services-1.4.2.apk

# Replace "com.example.test" with the name of the package containing your tests.
# Add "-e clearPackageData true" to clear your app's data in between runs.
adb shell 'CLASSPATH=$(pm path androidx.test.services) app_process / \
 androidx.test.services.shellexecutor.ShellMain am instrument -w -e \
 targetInstrumentation com.example.test/androidx.test.runner.AndroidJUnitRunner \
 androidx.test.orchestrator/.AndroidTestOrchestrator'

コマンド構文が示すように、Android Test Orchestrator をインストールして使用します。 直接渡されます。

adb shell pm list instrumentation

さまざまなツールチェーンの使用

別のツールチェーンを使用してアプリをテストする場合でも、 次の手順で Orchestrator をテストします。

  1. 必要なパッケージをアプリのビルドファイルに含めます。
  2. コマンドラインから Android Test Orchestrator を有効にします。

アーキテクチャ

オーケストレーター サービス APK は、 テスト APK とテスト対象アプリの APK:

<ph type="x-smartling-placeholder">
</ph> オーケストレーターを使用すると、JUnit テストを制御できる
図 1: Android テスト オーケストレーション APK の構造

Android Test Orchestrator はテストの開始時に JUnit テストを収集します。 各テストは個別のインスタンスで 実行されます Instrumentation

詳細

AndroidJUnitRunner の使用方法について詳しくは、API リファレンスをご覧ください。

参考情報

AndroidJUnitRunner の使用について詳しくは、以下をご覧ください。 説明します。

サンプル

  • AndroidJunitRunnerSample: テスト アノテーションを紹介します。 テストスイートの作成が含まれます