インストルメンテーション テストを作成する

インストルメンテーション テストは、物理デバイスかエミュレートされたデバイスかにかかわらず、Android デバイス上で実行されます。として Android フレームワーク API を利用できます。インストルメンテーション テスト ローカルテストよりも忠実度に優れていますが、ローカルテストよりも できます。

インストルメンテーション テストは、 動作を確認してみましょう。AndroidX Test には複数のライブラリが用意されています。 必要に応じてインストルメンテーション テストを簡単に作成できます。

テスト環境をセットアップする

Android Studio プロジェクトでは、インストルメント化されたアプリのソースファイルを格納します。 module-name/src/androidTest/java/ のテスト。このディレクトリはすでに存在しています: 新しいプロジェクトを作成し、インストルメンテーション テストのサンプルを含めます。

始める前に、AndroidX Test API を追加しておく必要があります。これにより、 アプリのインストルメンテーション テストコードをビルドして実行します。AndroidX Test は、 JUnit 4 のテストランナー、AndroidJUnitRunner、および UI の機能テスト用の API たとえば、EspressoUI AutomatorCompose テストなどです。

また、プロジェクトの Android テスト依存関係を構成して、 AndroidX Test が提供するテストランナーとルール API を使用します。

アプリの最上位の build.gradle ファイルで、これらのライブラリを指定する必要があります。 使用します。

dependencies {
    androidTestImplementation
"androidx.test:runner:$androidXTestVersion"
    androidTestImplementation
"androidx.test:rules:$androidXTestVersion"
   
// Optional -- UI testing with Espresso
    androidTestImplementation
"androidx.test.espresso:espresso-core:$espressoVersion"
   
// Optional -- UI testing with UI Automator
    androidTestImplementation
"androidx.test.uiautomator:uiautomator:$uiAutomatorVersion"
   
// Optional -- UI testing with Compose
    androidTestImplementation
"androidx.compose.ui:ui-test-junit4:$compose_version"
}

最新バージョンは AndroidX リリースノートCompose で入手できます。 UI リリースノートをご覧ください。

JUnit 4 のテストクラスを使用し、テストのフィルタリングなどの機能にアクセスするには、 AndroidJUnitRunner をデフォルトのテスト インストルメンテーションとして指定してください。 使用するには、アプリの依存関係に次の設定を含めます。 モジュール レベルの build.gradle ファイル:

android {
    defaultConfig
{
        testInstrumentationRunner
"androidx.test.runner.AndroidJUnitRunner"
   
}
}

インストルメンテーション テストクラスを作成する

インストルメンテーション テスト クラスは、 ローカルテストのビルド方法のセクションで説明されているクラス。

インストゥルメント化された JUnit 4 テストクラスを作成するには、次のように AndroidJUnit4 を指定します。 あります。

次の例は、インストルメンテーション テストを作成して検証する方法を示しています。 Parcelable インターフェースが LogHistory クラス:

KotlinJava
import android.os.Parcel
import android.text.TextUtils.writeToParcel
import androidx.test.filters.SmallTest
import androidx.test.runner.AndroidJUnit4
import com.google.common.truth.Truth.assertThat
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith

const
val TEST_STRING = "This is a string"
const
val TEST_LONG = 12345678L

// @RunWith is required only if you use a mix of JUnit3 and JUnit4.
@RunWith(AndroidJUnit4::class)
@SmallTest
class LogHistoryAndroidUnitTest {
   
private lateinit var logHistory: LogHistory

   
@Before
   
fun createLogHistory() {
        logHistory
= LogHistory()
   
}

   
@Test
   
fun logHistory_ParcelableWriteRead() {
       
val parcel = Parcel.obtain()
        logHistory
.apply {
           
// Set up the Parcelable object to send and receive.
            addEntry
(TEST_STRING, TEST_LONG)

           
// Write the data.
            writeToParcel
(parcel, describeContents())
       
}

       
// After you're done with writing, you need to reset the parcel for reading.
        parcel
.setDataPosition(0)

       
// Read the data.
       
val createdFromParcel: LogHistory = LogHistory.CREATOR.createFromParcel(parcel)
        createdFromParcel
.getData().also { createdFromParcelData: List<Pair<String, Long>> ->

           
// Verify that the received data is correct.
            assertThat
(createdFromParcelData.size).isEqualTo(1)
            assertThat
(createdFromParcelData[0].first).isEqualTo(TEST_STRING)
            assertThat
(createdFromParcelData[0].second).isEqualTo(TEST_LONG)
       
}
   
}
}

import android.os.Parcel;
import android.util.Pair;
import androidx.test.runner.AndroidJUnit4;
import com.google.common.truth.Truth.assertThat;
import java.util.List;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

// @RunWith is required only if you use a mix of JUnit3 and JUnit4.
@RunWith(AndroidJUnit4.class)
public class LogHistoryAndroidUnitTest {

   
public static final String TEST_STRING = "This is a string";
   
public static final long TEST_LONG = 12345678L;
   
private LogHistory mLogHistory;

   
@Before
   
public void createLogHistory() {
        mLogHistory
= new LogHistory();
   
}

   
@Test
   
public void logHistory_ParcelableWriteRead() {
       
// Set up the Parcelable object to send and receive.
        mLogHistory
.addEntry(TEST_STRING, TEST_LONG);

       
// Write the data.
       
Parcel parcel = Parcel.obtain();
        mLogHistory
.writeToParcel(parcel, mLogHistory.describeContents());

       
// After you're done with writing, you need to reset the parcel for reading.
        parcel
.setDataPosition(0);

       
// Read the data.
       
LogHistory createdFromParcel = LogHistory.CREATOR.createFromParcel(parcel);
       
List<Pair<String, Long>> createdFromParcelData
               
= createdFromParcel.getData();

       
// Verify that the received data is correct.
        assertThat
(createdFromParcelData.size()).isEqualTo(1);
        assertThat
(createdFromParcelData.get(0).first).isEqualTo(TEST_STRING);
        assertThat
(createdFromParcelData.get(0).second).isEqaulTo(TEST_LONG);
   
}
}

インストルメンテーション テストを実行する

インストルメンテーション テストは、実際のデバイスまたはエミュレータで実行できます。Android Studio ガイドでは、以下の方法を学ぶことができます。

参考情報

UI テストは通常、アプリの正しい動作を検証するインストルメンテーション テストです。 できます。EspressoCompose Test などのフレームワークを使用します。学習内容 詳しくは、UI テストガイドをご覧ください。

インストルメンテーション テストの使用について詳しくは、以下をご覧ください。 説明します。

サンプル

Codelab