寫在開頭:Android UI 自動化測試推薦網易的Airtest,也是谷歌推薦的,操作簡單,而且基于圖像識別根據用戶操作界面自動生成Python測試代碼
JUnit單元測試
testImplementation 'junit:junit:4.12'
image.png
image.png
使用gradle命令進行單元測試gradle test,還可以通過gradle testDebugUnitTest,或者是gradle testReleaseUnitTest,分別運行 debug 和 release 版本的 unit testing
Espresso
黑盒白盒測試區別如下:黑盒測試:已知產品的功能設計規格,可以進行測試證明每個實現了的功能是否符合要求。白盒測試:已知產品的內部工作過程,可以通過測試證明每種內部操作是否符合設計規格要求,所有內部成分是否以經過檢查。
Espresso是官方默認引入的,我們先看一下這個白盒測試工具
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
發個牢騷,看英文文檔好費勁,只能用翻譯插件一點一點翻譯,效率很低,效果還不如看一些相關中文博客,但是畢竟官網全面系統,而且很多技術文檔都是英文。希望自己堅持看英文文檔,提高英文水平。
當我根據文檔書寫代碼的時候,發現找不到onView方法,雙擊Shift,發現該方法在Espresso.onView
image.png
然后執行測試報錯java.lang.RuntimeException: No activities found. Did you forget to launch the activity by calling getActivity() or startActivitySync or similar?
解決方法
@get:Rule
var mActivityRule = ActivityTestRule(MainActivity::class.java)
簡單介紹一下Rule
一個JUnit Rule就是一個實現了TestRule的類,這些類的作用類似于@Before、@After,是用來在每個測試方法的執行前后執行一些代碼的一個方法
@RunWith(AndroidJUnit4::class)
class ExampleInstrumentedTest {
@get:Rule
var mActivityRule = ActivityTestRule(MainActivity::class.java)
@Test
fun useA() {
//EditText輸入文字
onView(withId(R.id.et)).perform(replaceText("劉德華"))
val btn = onView(withId(R.id.btn))
println("-------------------$btn")
//驗證更新按鈕是否顯示
btn.check(matches(isDisplayed()))
//點擊更新按鈕
btn.perform(click())
}
}
這里記錄一個遇到的問題:輸入中文的時候,由于鍵盤上沒有中文,所以要用replaceText而不是typeText,否則會報錯誤i.e. current IME does not understand how to translate the string into key events). As a workaround, you can use replaceText action to set the text directly in the EditText field.
單頁面測試使用Espresso,多頁面測試使用 UI Automator
3.UI Automator
測試錄屏.gif
參考官方文檔
在 Android 應用模塊的 build.gradle 文件中,您必須設置對 UI Automator 庫的依賴項引用
dependencies {
...
androidTestImplementation 'androidx.test.uiautomator:uiautomator:2.2.0'
}
示例代碼
private const val PACKAGE_NAME = "club.guozengjie.jetpack"
private const val LAUNCH_TIMEOUT = 5000L
@RunWith(AndroidJUnit4::class)
@SdkSuppress(minSdkVersion = 18)
class UIAutomatorTest {
private lateinit var device: UiDevice
@Before
fun startMainActivityFromHomeScreen() {
device = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation())
device.pressHome()
// Wait for launcher
val launcherPackage: String = device.launcherPackageName
assertThat(launcherPackage, notNullValue())
device.wait(
Until.hasObject(By.pkg(launcherPackage).depth(0)),
LAUNCH_TIMEOUT
)
// Launch the app
val context = ApplicationProvider.getApplicationContext()
val intent = context.packageManager.getLaunchIntentForPackage(
PACKAGE_NAME
)?.apply {
// Clear out any previous instances
addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK)
}
context.startActivity(intent)
// Wait for the app to appear
device.wait(
Until.hasObject(By.pkg(PACKAGE_NAME).depth(0)),
LAUNCH_TIMEOUT
)
}
@Test
fun aaa() {
// Type text and then press the button.
device.findObject(
By.res(
PACKAGE_NAME,
"et"
)
).text = "文川雪"
device.findObject(
By.res(
PACKAGE_NAME,
"btn"
)
).click()
device.waitForIdle()
device.findObject(By.res(PACKAGE_NAME, "tv")).click()
}
}
Airtest
Airtest是一款由網易研發并開源的自動化測試框架,官網
官網有詳細的文檔,這里就不記錄了。只想對Airtest說一個字:牛。強烈推薦
SoloPi
SoloPi是一個無線化、非侵入式的Android自動化工具,公測版擁有錄制回放、性能測試、一機多控三項主要功能,能為測試開發人員節省寶貴時間。