Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android Gradle Plugin 指南(四):測試、報告與 Lint 一次搞懂

編輯:關於 Android 程式設計

5. Testing(測試)

好消息是:在 Gradle 體系下,測試已經被整合進 App 專案本身,你不需要再另外開一個獨立的測試專案。

5.1 基礎概念與配置

如前面所述,緊鄰著 main sourceSet 的就是 androidTest sourceSet,預設路徑在 src/androidTest/ 底下。

這個 sourceSet 會被打包成一支獨立的測試 APK,透過 Android 測試框架部署到裝置上,用來驗證你的 App。單元測試、整合測試、後續的 UI 自動化測試都放在這裡。

值得注意的是,這個 sourceSet 不需要(也不應該)自帶 AndroidManifest.xml,因為該檔案會由 plugin 自動產生。

測試相關的可配置項目有這幾個:

* testPackageName

* testInstrumentationRunner

* testHandleProfiling

* testFunctionalTest

它們都在 defaultConfig 區塊中設定:

android {
    defaultConfig {
        testPackageName "com.test.foo"
        testInstrumentationRunner "android.test.InstrumentationTestRunner"
        testHandleProfiling true
        testFunctionalTest true
    }
}

在自動產生的測試 APK manifest 中,instrumentation 節點的 targetPackage 屬性會自動被設成「測試對象的 package 名稱」——即使這個名稱是透過 defaultConfig 或某個 Build Type 自訂過的。這也正是 manifest 必須自動生成的原因之一:手写很容易和實際的 variant 對不上。

另外,測試 sourceSet 也可以擁有自己的依賴。預設情況下,App 本體與它的依賴會自動被加入測試的 classpath,若需要額外補充可以這樣寫:

dependencies {
    androidTestCompile 'com.google.guava:guava:11.0.2'
}

測試 APK 由 assembleTest task 產出。要注意它不依賴 main 的 assemble task,必須手動觸發,不會自動跟著跑。

目前一次只會針對一個 Build Type 進行測試,預設是 debug,但可以改:

android {
    ...
    testBuildType "staging"
}

5.2 執行測試

標誌性的 task 是 connectedCheck,顧名思義,它需要一台已連線的裝置才能啟動。

這個 task 依賴 androidTest,一但它被觸發,會依序執行:

* 確認 App 與測試 APK 都已建置完成(依賴 assembleDebug 與 assembleTest)。

* 安裝這兩個 APK。

* 執行測試。

* 卸載這兩個 APK。

如果同時接上多台裝置,所有測試會並行跑在所有裝置上;其中任何一台出現失敗,整體結果就算失敗。

測試結果以 XML 形式輸出,位置在:

build/androidTest-results

這和 JUnit 的結果位置 build/test-results 是一樣的設計思路。同樣可以自訂:

android {
    ...
    testOptions {
        resultsDir = "$project.buildDir/foo/results"
    }
}

這裡的 android.testOptions.resultsDir 會透過 Project.file(String) 解析。

5.3 測試 Android Library

測試函式庫專案的方式與 App 專案幾乎相同,只有一處關鍵差異:整個函式庫(含其所有依賴)會被自動視為依賴加入測試 APK。也就是說,測試 APK 裡不只包含測試程式碼,還包含了函式庫本體以及函式庫的所有依賴項。

函式庫的 manifest 會被合併進測試 APK 的 manifest(作為一個引用該函式庫的外層殼)。

androidTest task 的行為也只差在「只安裝/卸載測試 APK」,因為沒有第二個 APK 需要處理。

5.4 測試報告

執行測試時,Gradle 會輸出 HTML 格式的報告方便檢視。Android plugin 在此基礎上做了擴充:它會把所有連線裝置的測試結果合併到同一份報告裡。

5.4.1 單一專案

單一專案的測試報告預設產出於:build/reports/androidTests

這和 JUnit 報告所在的 build/reports/tests 位置類似,其他 plugin 的報告通常放在 build/reports/<plugin>/ 下。

一樣可以自訂路徑:

android {
    ...
    testOptions {
        reportDir = "$project.buildDir/foo/report"
    }
}

這份報告會把跑在不同裝置上的結果合併呈現。

5.4.2 多專案報告

在一個同時配置多個 App 或多個 Library 的多專案結構中,「一次跑完所有測試並產出單一報告」會實用很多。

要達到這個目的,需要額外套用 android-reporting plugin,一樣寫在根目錄的 build.gradle(與 settings.gradle 同層)裡:

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.5.6'
    }
}
apply plugin: 'android-reporting'

設定好之後,在專案根目錄執行:

gradle deviceCheck mergeAndroidReports --continue

這裡的 --continue 很重要:它讓所有子專案即使某個測試失敗也會繼續跑完,避免「第一個失敗就整批中止,導致部分專案根本沒跑到測試」的情況。

5.5 Lint 支援

Lint 是一個用來掃描 Android 專案中潛在問題的靜態分析工具。從 plugin 0.7.0 起,你可以針對單一 variant 執行 lint,也可以對所有 variant 一次跑完,報告中會標明問題屬於哪個 variant。

lint 的可用選項相當多,通常你只需要設定其中幾項:

android {
    lintOptions {
        // 安靜模式,不輸出分析進度
        quiet true
        // 發現錯誤時中止 gradle build
        abortOnError false
        // 只回報 error,忽略 warning
        ignoreWarnings true
        // 輸出絕對路徑(預設 true)
        //absolutePaths true
        // 檢查所有 issue,包含預設關閉的
        checkAllWarnings true
        // 把所有 warning 當成 error
        warningsAsErrors true
        // 關閉指定 issue
        disable 'TypographyFractions','TypographyQuotes'
        // 開啟指定 issue
        enable 'RtlHardcoded','RtlCompat','RtlEnabled'
        // 只檢查指定 issue
        check 'NewApi','InlinedApi'
        // 不在錯誤輸出中附上程式碼行
        noLines true
        // 顯示錯誤的所有位置,不截斷清單
        showAll true
        // 後備 lint 配置檔(預設嚴重度等)
        lintConfig file(default-lint.xml)
        // 輸出文字報告(預設 false)
        textReport true
        // 文字輸出去向,可以是檔案或 'stdout'
        textOutput 'stdout'
        // 輸出 XML 報告,可給 Jenkins 等 CI 使用
        xmlReport false
        xmlOutput file(lint-report.xml)
        // 輸出 HTML 報告(含 issue 說明、程式碼片段)
        htmlReport true
        htmlOutput file(lint-report.html)
        // 所有 release build 都檢查 severity=fatal 的問題,命中則中止建置
        checkReleaseBuilds true
        // 將指定 issue 的嚴重度設為 fatal
        fatal 'NewApi','InlineApi'
        // 設為 error
        error 'Wakelock','TextViewEdits'
        // 設為 warning
        warning 'ResourceAsColor'
        // 設為 ignore(等同關閉該檢查)
        ignore 'TypographyQuotes'
    }
}

實務上最值得打開的是 abortOnError 與 checkReleaseBuilds——把 fatal 級別的問題擋在打包之前,比事後被商店退回划算太多。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved