Espresso 意圖

Espresso-Intents 是 Espresso 的擴充功能,有利於 intents (意圖) 延伸 進行測試。這就像 Mockito,適用於 Android 意圖。

如果應用程式將功能委派給其他應用程式或平台,您可以 Espresso-Intent 專注於自家應用程式的邏輯,並假設其他應用程式 否則平台會正常運作使用 Espresso-Intent 時 並驗證傳出意圖,甚至提供虛設常式回應,以取代 實際的意圖回應

在專案中加入 Espresso-Intent

在應用程式的 app/build.gradle 檔案中,新增以下這行程式碼 dependencies:

Groovy

androidTestImplementation 'androidx.test.espresso:espresso-intents:3.6.1'

Kotlin

androidTestImplementation('androidx.test.espresso:espresso-intents:3.6.1')

Espresso-Intents 僅與 Espresso 2.1 以上版本和 0.3 以上版本 由於 Android 測試程式庫,請務必一併更新這些程式碼行:

Groovy

androidTestImplementation 'androidx.test:runner:1.6.1'
androidTestImplementation 'androidx.test:rules:1.6.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.6.1'

Kotlin

androidTestImplementation('androidx.test:runner:1.6.1')
androidTestImplementation('androidx.test:rules:1.6.1')
androidTestImplementation('androidx.test.espresso:espresso-core:3.6.1')

編寫測試規則

在編寫 Espresso-Intent 測試之前,請先設定 IntentsTestRule。這是 ActivityTestRule 類別的擴充套件,可讓您輕鬆使用 功能性 UI 測試中的 Espresso-Intent API。IntentsTestRule 會初始化 每次以 @Test 註解和版本註解的測試前 Espresso-Intents 每次執行測試後 Espresso-Intents。

下列程式碼片段為 IntentsTestRule 的範例:

Kotlin

@get:Rule
val intentsTestRule = IntentsTestRule(MyActivity::class.java)

Java

@Rule
public IntentsTestRule<MyActivity> intentsTestRule =
    new IntentsTestRule<>(MyActivity.class);

比對符合

Espresso-Intent 可根據下列項目攔截傳出意圖: 特定比對條件 (使用「漢堡比對器」定義)火腿 可讓你:

  • 使用現有的意圖比對工具:最容易的選項,這個選項應該幾乎一定會存在
  • 實作自己的意圖比對器:最靈活的選項。詳情請參閱 「編寫自訂比對器」一節在 Hamcrest 教學課程

Espresso-Intents 提供 intended()intending() 方法,用於驗證意圖及 分機運動兩者都拿著哈姆斯特Matcher<Intent>物件 引數。

下列程式碼片段顯示使用現有意圖的意圖驗證 符合啟動瀏覽器的意圖比對器:

Kotlin

assertThat(intent).hasAction(Intent.ACTION_VIEW)
assertThat(intent).categories().containsExactly(Intent.CATEGORY_BROWSABLE)
assertThat(intent).hasData(Uri.parse("www.google.com"))
assertThat(intent).extras().containsKey("key1")
assertThat(intent).extras().string("key1").isEqualTo("value1")
assertThat(intent).extras().containsKey("key2")
assertThat(intent).extras().string("key2").isEqualTo("value2")

Java

assertThat(intent).hasAction(Intent.ACTION_VIEW);
assertThat(intent).categories().containsExactly(Intent.CATEGORY_BROWSABLE);
assertThat(intent).hasData(Uri.parse("www.google.com"));
assertThat(intent).extras().containsKey("key1");
assertThat(intent).extras().string("key1").isEqualTo("value1");
assertThat(intent).extras().containsKey("key2");
assertThat(intent).extras().string("key2").isEqualTo("value2");

驗證意圖

Espresso-Intents 會記錄所有嘗試透過 進行測試。使用 intended() 方法,這與 Mockito.verify(),您可以斷言已看過指定意圖。不過 除非您明確設定,否則 Espresso-Intent 不會使意圖的回應出現錯誤 。

下列程式碼片段是可驗證但非虛設常式的測試範例 啟動外部「手機」活動:

Kotlin

@Test fun validateIntentSentToPackage() {
    // User action that results in an external "phone" activity being launched.
    user.clickOnView(system.getView(R.id.callButton))

    // Using a canned RecordedIntentMatcher to validate that an intent resolving
    // to the "phone" activity has been sent.
    intended(toPackage("com.android.phone"))
}

Java

@Test
public void validateIntentSentToPackage() {
    // User action that results in an external "phone" activity being launched.
    user.clickOnView(system.getView(R.id.callButton));

    // Using a canned RecordedIntentMatcher to validate that an intent resolving
    // to the "phone" activity has been sent.
    intended(toPackage("com.android.phone"));
}

懸吊式

使用 intending() 方法與 Mockito.when() 類似,您可以 能夠對以 startActivityForResult()。這對外部活動來說特別實用 因為您無法操控外部活動的使用者介面 控制傳回受測試活動的 ActivityResult

下列程式碼片段實作了一個範例 activityResult_DisplaysContactsPhoneNumber() 測試,可驗證 使用者啟動「聯絡人」受測應用程式中的活動、聯絡人手機 螢幕上會顯示數字:

  1. 建構要在啟動特定活動時傳回的結果。 樣本測試會攔截所有傳送至「contacts」的意圖並把他們的 包含有效 ActivityResult 的回應,使用結果代碼 RESULT_OK

    Kotlin

    val resultData = Intent()
    val phoneNumber = "123-345-6789"
    resultData.putExtra("phone", phoneNumber)
    val result = Instrumentation.ActivityResult(Activity.RESULT_OK, resultData)
    

    Java

    Intent resultData = new Intent();
    String phoneNumber = "123-345-6789";
    resultData.putExtra("phone", phoneNumber);
    ActivityResult result =
        new ActivityResult(Activity.RESULT_OK, resultData);
    
  2. 指示 Espresso 提供虛設常式結果物件,以回應所有 「聯絡人」叫用意圖:

    Kotlin

    intending(toPackage("com.android.contacts")).respondWith(result)
    

    Java

    intending(toPackage("com.android.contacts")).respondWith(result);
    
  3. 驗證用於啟動活動的動作是否產生了 虛設常式結果。在這個例子中,範例測試會檢查電話號碼 "123-345-6789" 會傳回, 「聯絡人活動」時顯示:

    Kotlin

    onView(withId(R.id.pickButton)).perform(click())
    onView(withId(R.id.phoneNumber)).check(matches(withText(phoneNumber)))
    

    Java

    onView(withId(R.id.pickButton)).perform(click());
    onView(withId(R.id.phoneNumber)).check(matches(withText(phoneNumber)));
    

以下是完整的 activityResult_DisplaysContactsPhoneNumber() 測試:

Kotlin

@Test fun activityResult_DisplaysContactsPhoneNumber() {
    // Build the result to return when the activity is launched.
    val resultData = Intent()
    val phoneNumber = "123-345-6789"
    resultData.putExtra("phone", phoneNumber)
    val result = Instrumentation.ActivityResult(Activity.RESULT_OK, resultData)

    // Set up result stubbing when an intent sent to "contacts" is seen.
    intending(toPackage("com.android.contacts")).respondWith(result)

    // User action that results in "contacts" activity being launched.
    // Launching activity expects phoneNumber to be returned and displayed.
    onView(withId(R.id.pickButton)).perform(click())

    // Assert that the data we set up above is shown.
    onView(withId(R.id.phoneNumber)).check(matches(withText(phoneNumber)))
}

Java

@Test
public void activityResult_DisplaysContactsPhoneNumber() {
    // Build the result to return when the activity is launched.
    Intent resultData = new Intent();
    String phoneNumber = "123-345-6789";
    resultData.putExtra("phone", phoneNumber);
    ActivityResult result =
        new ActivityResult(Activity.RESULT_OK, resultData);

    // Set up result stubbing when an intent sent to "contacts" is seen.
    intending(toPackage("com.android.contacts")).respondWith(result);

    // User action that results in "contacts" activity being launched.
    // Launching activity expects phoneNumber to be returned and displayed.
    onView(withId(R.id.pickButton)).perform(click());

    // Assert that the data we set up above is shown.
    onView(withId(R.id.phoneNumber)).check(matches(withText(phoneNumber)));
}

其他資源

如要進一步瞭解如何在 Android 測試中使用 Espresso-Intent,請參閱 以下資源。

範例