使用 Espresso 撰寫精簡、精美且可靠的 Android UI 測試。
下列程式碼片段為 Espresso 測試的範例:
Kotlin
@Test fun greeterSaysHello() { onView(withId(R.id.name_field)).perform(typeText("Steve")) onView(withId(R.id.greet_button)).perform(click()) onView(withText("Hello Steve!")).check(matches(isDisplayed())) }
Java
@Test public void greeterSaysHello() { onView(withId(R.id.name_field)).perform(typeText("Steve")); onView(withId(R.id.greet_button)).perform(click()); onView(withText("Hello Steve!")).check(matches(isDisplayed())); }
核心 API 不僅規模小、容易預測且易於學習,卻仍開放自訂使用。Espresso 會明確測試狀態的預期、互動和斷言,而不受樣板內容、自訂基礎架構或雜亂的實作詳情幹擾。
Espresso 測試可達到最佳執行速度!讓您可以一邊等待等待、同步、睡眠和輪詢,一邊在應用程式 UI 處於靜止狀態時操控應用程式 UI 並斷言。
目標對象
Espresso 的目標對像是開發人員,他們認為自動化測試是開發生命週期中不可或缺的一環。雖然這可用於黑箱測試,但熟悉測試中程式碼集的人員卻將 Espresso 的完整功能解鎖。
同步功能
每次測試叫用 onView()
時,Espresso 都會等待執行對應的 UI 動作或斷言,直到符合下列同步處理條件為止:
藉由執行這些檢查,Espresso 會大幅提高在任何特定時間都只能執行一個 UI 動作或斷言的可能性。這項功能可為您提供更可靠且更可靠的測試結果。
套裝組合
espresso-core
:包含核心和基本View
比對器、動作和斷言。請參閱基本和方案。espresso-web
- 包含WebView
支援的資源。espresso-idling-resource
- Espresso 與背景工作同步的機制。espresso-contrib
- 包含DatePicker
、RecyclerView
和Drawer
動作、無障礙功能檢查和CountingIdlingResource
的外部貢獻。espresso-intents
- 用於驗證及虛設意圖的擴充功能,以便進行密封測試。espresso-remote
- Espresso 多程序功能的位置。
如要進一步瞭解最新版本,請參閱版本資訊。
其他資源
如要進一步瞭解如何在 Android 測試中使用 Espresso,請參閱下列資源。
範例
- Espresso 程式碼範例包含完整的 Espresso 樣本。
- BasicSample:基本 Espresso 範例。
- (更多...)