15_Android測(cè)試

????一直以來(lái),對(duì)Android App的測(cè)試部分是有所忽視的。對(duì)它的了解得并不深入,也不全面,每次都是淺嘗輒止,精力主要集中在功能實(shí)現(xiàn)上。畢竟,有專門的測(cè)試人員來(lái)完成這一工作。但話又說(shuō)回來(lái),他們大多數(shù)是基于黑盒進(jìn)行手動(dòng)測(cè)試的,主要驗(yàn)證需求是否實(shí)現(xiàn)和一些邊界是否異常等。對(duì)于白盒測(cè)試,更多則需要開發(fā)者來(lái)完成。本篇文章就來(lái)稍微彌補(bǔ)這一空缺。此外,保證一些重要、核心的方法在長(zhǎng)久的迭代、升級(jí)過(guò)程中的正確性,不被意外修改或影響,是非常重要的。這一點(diǎn)可以通過(guò)特定的白盒測(cè)試用例來(lái)實(shí)現(xiàn)。

(1)分類

????根據(jù)不同的粒度,測(cè)試可以分為單元測(cè)試、集成測(cè)試和大型測(cè)試。

  1. 單元測(cè)試:對(duì)App小部分的測(cè)試,如一個(gè)方法或一個(gè)類;
  2. 集成測(cè)試:也叫中等測(cè)試(Medium tests),由兩個(gè)或多個(gè)單元測(cè)試構(gòu)成;
  3. 大型測(cè)試:Big tests,也叫端對(duì)端測(cè)試(End-to-End test),對(duì)App多個(gè)部分進(jìn)行的測(cè)試,如一個(gè)用戶操作流程、一個(gè)顯示過(guò)程等;

(2)androidTest和test

????使用Android Studio(后文簡(jiǎn)稱AS)創(chuàng)建新Project時(shí),會(huì)有兩個(gè)目錄:

  1. androidTest:也叫插樁測(cè)試(Instrumented tests),運(yùn)行在真機(jī)或模擬器上的測(cè)試;它通常是UI測(cè)試,啟動(dòng)一個(gè)app,測(cè)試相關(guān)的交互;
  2. test:?jiǎn)卧獪y(cè)試,也叫本地測(cè)試(local tests),一般在本地PC上完成;因此,還稱它為host-side tests。

????在build.gradle中,兩個(gè)相關(guān)的依賴項(xiàng):

    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'

????幾點(diǎn)說(shuō)明:

  1. testImplementation:表示使用的依賴項(xiàng)僅為了單元測(cè)試;
  2. androidTestImplementation:表示使用的依賴項(xiàng)僅為了插樁測(cè)試(Instrumented tests);

(3)AndroidX Test API

????如果編寫的單元測(cè)試依賴于Android框架,那么可以使用與設(shè)備無(wú)關(guān)的統(tǒng)一API,如androidx.test API。
????如果測(cè)試依賴于資源,那么需要在build.gradle中配置:

android {
        // ...
        testOptions {
            unitTests {
                includeAndroidResources = true
            }
        }
    }

????一個(gè)基本AndroidX Test的示例:

import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.ext.junit.runners.AndroidJUnit4

import org.junit.Test
import org.junit.runner.RunWith

import org.junit.Assert.*

/**
 * Instrumented test, which will execute on an Android device.
 *
 * See [testing documentation](http://d.android.com/tools/testing).
 */
@RunWith(AndroidJUnit4::class)
class ExampleInstrumentedTest {
    @Test
    fun useAppContext() {
        // Context of the app under test.
        val appContext = InstrumentationRegistry.getInstrumentation().targetContext
        assertEquals("com.xxx.xxx", appContext.packageName)
    }
}

????作為對(duì)比,一個(gè)不使用AndroidX的基本示例:

import org.junit.Test;
import static org.junit.Assert.*;

class ExampleUnitTest {
    @Test
    fun addition_isCorrect() {
        assertEquals(4, 2 + 2)
    }
}

(4)Robolectric簡(jiǎn)介

????上面的AndroidX API 是和Android真機(jī)或模擬器關(guān)聯(lián)的,而Robolectric是一種PC上模擬Android運(yùn)行環(huán)境的工具。真機(jī)或模擬器有時(shí)候會(huì)很慢,比如冷啟動(dòng)一個(gè)大App首頁(yè)就要很長(zhǎng)時(shí)間。Robolectric就是針對(duì)這一點(diǎn)做的改進(jìn),以加快測(cè)試速度。
????如果想使用Robolectric,需要在build.gradle添加如下配置:

android {
  testOptions {
    unitTests {
      includeAndroidResources = true
    }
  }
}

dependencies {
  testImplementation 'junit:junit:4.13.2'
  testImplementation 'org.robolectric:robolectric:4.9'
}

????具體使用示例:

@RunWith(RobolectricTestRunner.class)
public class MyActivityTest {

  @Test
  public void clickingButton_shouldChangeMessage() {
    try (ActivityController<MyActvitiy> controller = Robolectric.buildActivity(MyActvitiy.class)) {
      controller.setup(); // Moves Activity to RESUMED state
      MyActvitiy activity = controller.get();

      activity.findViewById(R.id.button).performClick();
      assertEquals(((TextView) activity.findViewById(R.id.text)).getText(), "Robolectric Rocks!");
    }
  }
}

????官方地址:http://robolectric.org/

(5)Espresso簡(jiǎn)介

???? Espresso是AndroidX Test API中的一部分,提供準(zhǔn)確的、可信賴的UI測(cè)試?;谒?,可以做一些自動(dòng)化測(cè)試。
???? 配置如下:

dependencies {
        ...
        androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
    }

???? 基本使用如下:

import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.action.ViewActions.click
import androidx.test.espresso.matcher.ViewMatchers.withText
import androidx.test.espresso.matcher.ViewMatchers.withId

@Test
fun greeterSaysHello() {
    onView(withId(R.id.name_field)).perform(typeText("Steve"))
    onView(withId(R.id.greet_button)).perform(click())
}

???? Espresso的具體內(nèi)容非常多,感興趣的朋友可以去找官方文檔學(xué)習(xí)。

(6)UI Automator簡(jiǎn)介

????UI Automator是一個(gè)可以跨app的UI測(cè)試框架。通過(guò)使用它的一些API,可以與屏幕上可見(jiàn)的View進(jìn)行交互。它并不局限于焦點(diǎn)Activity,可以操作桌面launcher上任何一項(xiàng)。通過(guò)類名、文字或內(nèi)容描述等來(lái)找到屏幕上的元素。
????build.gradle中添加依賴項(xiàng):

dependencies {
        ...
        androidTestImplementation 'androidx.test.uiautomator:uiautomator:2.2.0'
    }

????使用示例:

import androidx.test.uiautomator.UiDevice
import androidx.test.uiautomator.UiSelector
import androidx.test.uiautomator.UiObject
import androidx.test.uiautomator.UiCollection

//示例1
val device = UiDevice.getInstance(getInstrumentation())
device.pressHome()

// Bring up the default launcher by searching for a UI component
// that matches the content description for the launcher button.
val allAppsButton: UiObject = device.findObject(UiSelector().description("Apps"))

// Perform a click on the button to load the launcher.
allAppsButton.clickAndWaitForNewWindow()

//示例2
val videos = UiCollection(UiSelector().className("android.widget.FrameLayout"))
// Retrieve the number of videos in this collection:
val count = videos.getChildCount(
  UiSelector().className("android.widget.LinearLayout")
)

// Find a specific video and simulate a user-click on it
val video: UiObject = videos.getChildByText(
  UiSelector().className("android.widget.LinearLayout"),
  "Cute Baby Laughing"
)
video.click()

????對(duì)于帶滾動(dòng)功能的控件如ListView,操作如下:

 val settingsItem = UiScrollable(UiSelector().className("android.widget.ListView"))
 val about: UiObject = settingsItem.getChildByText(UiSelector().className("android.widget.LinearLayout"),"About tablet")
 about.click()

????快速定位某個(gè)View有一個(gè)快捷的方式,尤其對(duì)不熟悉的復(fù)雜頁(yè)面,即使用uiautomatorviewer命令,它可以展示UI對(duì)應(yīng)層次、類名、id等。根據(jù)這些信息,就可以像上面代碼示例中那樣獲取對(duì)應(yīng)的UiObject,然后對(duì)它進(jìn)行一些操作。
????在命令行運(yùn)行:uiautomatorviewer(<android-sdk>/tools/目錄下),就可以打開一個(gè)可視界面。點(diǎn)擊左上角按鈕(Screen Shot)對(duì)手機(jī)進(jìn)行截圖,截圖內(nèi)容會(huì)展示在左邊的面板,移動(dòng)鼠標(biāo)到不同的UI元素上,右邊的面板就會(huì)展示出對(duì)應(yīng)的信息,有頁(yè)面的層次結(jié)構(gòu)、類名、resource-ID等。值得注意的是:該命令在Java 8運(yùn)行良好,在Java 11、12、19都有問(wèn)題(親測(cè),Android SDK已升級(jí)到最新),報(bào)錯(cuò)如下:

Error: Could not create the Java Virtual Machine.
Error: A fatal exception has occurred. Program will exit.

????此外,該工具是基于View體系的,如果使用了Jetpack Compose,很多的頁(yè)面層次結(jié)構(gòu)是無(wú)法展示出來(lái)的,特別是各種充當(dāng)ViewGroup功能的組件,如Row、Column等。

(7)自動(dòng)化測(cè)試簡(jiǎn)介

????自動(dòng)化測(cè)試屬于Instrumented test,使用上面介紹的Espresso或UI Automator,將用戶的手動(dòng)操作轉(zhuǎn)換為特定步驟的測(cè)試代碼,就可以實(shí)現(xiàn)自動(dòng)化測(cè)試。
????這些特定的步驟,每一步都必須基于一個(gè)確定的行為。如果某一步存在不確定性,那么后續(xù)步驟就無(wú)法進(jìn)行。例如某一步驟依賴接口返回,這存在斷網(wǎng)、接口緩慢或者服務(wù)器錯(cuò)誤等異常情況,就使得后續(xù)的步驟無(wú)法執(zhí)行。
????Espresso保證只有在UI 空閑(此時(shí)主線程等待,已交出cpu時(shí)間)的情況下,才執(zhí)行下一個(gè)步驟(術(shù)語(yǔ):同步)。這在一定程度上減少了不確定性,但并不能解決所有的問(wèn)題,如上面接口的異常等。一種解決方案是等待固定的時(shí)間,如2s,再執(zhí)行下一步。

(8)Compose測(cè)試

????Espresso或UI Automator對(duì)于Compose并不適用,Compose的測(cè)試需要對(duì)應(yīng)的測(cè)試框架。添加依賴項(xiàng):

// Test rules and transitive dependencies:
androidTestImplementation("androidx.compose.ui:ui-test-junit4:$compose_version")
// Needed for createAndroidComposeRule, but not createComposeRule:
debugImplementation("androidx.compose.ui:ui-test-manifest:$compose_version")

????使用示例:

class MyComposeTest {

    @get:Rule
    val composeTestRule = createComposeRule()
    // use createAndroidComposeRule<YourActivity>() if you need access to
    // an activity

    @Test
    fun myTest() {
        // Start the app
        composeTestRule.setContent {
            MyAppTheme {
                MainScreen(uiState = fakeUiState, /*...*/)
            }
        }

        composeTestRule.onNodeWithText("Continue").performClick()

        composeTestRule.onNodeWithText("Welcome").assertIsDisplayed()
    }
}

????與元素交互的方式有三種:

  1. 查找器(Finder):選擇頁(yè)面元素;
  2. 斷言(Assert):驗(yàn)證元素是否有某些屬性;
  3. 行為(Action):在元素上注入模擬的用戶事件。

????onNodeWithText()是常見(jiàn)的查找元素方式。onNode()、onAllNode()分別選擇一個(gè)和多個(gè)元素,示例如下:

composeTestRule.onNode(hasText("Button")) // Equivalent to onNodeWithText("Button")

composeTestRule .onAllNodes(hasText("Button")) // Equivalent to onAllNodesWithText("Button")

(9)命令行執(zhí)行測(cè)試

????AS上可以方便的運(yùn)行測(cè)試,在相應(yīng)的測(cè)試文件上右鍵點(diǎn)擊,出現(xiàn)“Run ...”選項(xiàng),點(diǎn)擊它即可。除此之外,還可以使用命令行來(lái)運(yùn)行,如下:

./gradlew test :執(zhí)行所有的單元測(cè)試;
./gradlew connectedAndroidTest :執(zhí)行所有的Instrumented測(cè)試,可以簡(jiǎn)寫為./gradlew cAT,第一個(gè)單詞首字母小寫,后續(xù)單詞首字母大寫;
./gradlew myLib:connectedAndroidTest :執(zhí)行myLib模塊的所有的Instrumented測(cè)試。

????Over !

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。
禁止轉(zhuǎn)載,如需轉(zhuǎn)載請(qǐng)通過(guò)簡(jiǎn)信或評(píng)論聯(lián)系作者。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容