鍍金池/ 教程/ Android/ 測試 - Testing
依賴關(guān)系,Android 庫和多項目設(shè)置 - Dependencies,Android Libraries and Multi-
要求 - Requirements
構(gòu)建變種版本 - Build Variants
高級構(gòu)建定制 - Advanced Build Customization
測試 - Testing
基本項目 - Basic Project
簡介 - Introduction

測試 - Testing

構(gòu)建一個測試程序已經(jīng)被集成到應(yīng)用項目中,沒有必要再專門建立一個測試項目。

單元測試

單元測試的支持被試驗性地添加到 1.1 版本中,請看 這頁。本章節(jié)剩余部分描述了可以在真機(jī)(或模擬器)上運(yùn)行的 “instrumentation tests”,以及需要構(gòu)建的獨(dú)立、測試性的 APK。

基本知識和配置

正如前面所提到的,緊鄰main _sourceSet_的就是androidTest sourceSet,默認(rèn)路徑在src/androidTest/下。 在這個測試_sourceSet_中會構(gòu)建一個使用Android測試框架,并且可以部署到設(shè)備上的測試apk來測試應(yīng)用程序。這里面包含單元測試,集成測試,和后續(xù)UI自動化測試。 這個測試sourceSet不應(yīng)該包含AndroidManifest.xml文件,因為這個文件會自動生成。

下面這些值可能會在測試應(yīng)用配置中使用到:

  • testPackageName
  • testInstrumentationRunner
  • testHandleProfiling
  • testfunctionalTest

正如前面所看到的,這些配置在defaultConfig對象中配置:

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

在測試應(yīng)用程序的manifest文件中,instrumentation節(jié)點(diǎn)的targetPackage屬性值會自動使用測試應(yīng)用的package名稱設(shè)置,即使這個名稱是通過defaultConfig或者_(dá)Build Type_對象自定義的。這也是manifest文件需要自動生成的一個原因。

另外,這個測試_sourceSet_也可以擁有自己的依賴。 默認(rèn)情況下,應(yīng)用程序和他的依賴會自動添加的測試應(yīng)用的classpath中,但是也可以通過以下來擴(kuò)展:

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

測試應(yīng)用通過assembleTest task來構(gòu)建。assembleTest不依賴于main中的assemble task,需要手動設(shè)置運(yùn)行,不能自動運(yùn)行。

目前只有一個_Build Type_被測試。默認(rèn)情況下是debug Build Type,但是這也可以通過以下自定義配置:

    android {
        ...
        testBuildType "staging"
    }

運(yùn)行測試

正如前面提到的,標(biāo)志性task connectedCheck要求一個連接的設(shè)備來啟動。 這個過程依賴于androidTest task,因此將會運(yùn)行androidTest。這個task將會執(zhí)行下面內(nèi)容:

  • 確認(rèn)應(yīng)用和測試應(yīng)用都被構(gòu)建(依賴于assembleDebugassembleTest)。
  • 安裝這兩個應(yīng)用。
  • 運(yùn)行這些測試。
  • 卸載這兩個應(yīng)用。

如果有多于一個連接設(shè)備,那么所有測試都會同時運(yùn)行在所有連接設(shè)備上。如果其中一個測試失敗,不管是哪一個設(shè)備算失敗。

所有測試結(jié)果都被保存為XML文檔,路徑為:

    _build/androidTest-results_

(這類似于JUnit的運(yùn)行結(jié)果保存在build/test-results)

同樣,這也可以自定義配置:

    android {
        ...

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

這里的android.testOptions.resultsDir將由Project.file(String)獲得。

測試 Android 庫

測試Android庫項目的方法與應(yīng)用項目的方法類似。

唯一的不同在于整個庫(包括它的依賴)都是自動作為依賴庫被添加到測試應(yīng)用中。結(jié)果就是測試APK不單只包含它的代碼,還包含了庫項目自己和庫的所有依賴。 庫的manifest被組合到測試應(yīng)用的manifest中(作為一些項目引用這個庫的殼)。

androidTest task的變改只是安裝(或者卸載)測試APK(因為沒有其它APK被安裝)。

其它的部分都是類似的。

測試報告

當(dāng)運(yùn)行單元測試的時候,Gradle會輸出一份HTML格式的報告以方便查看結(jié)果。 Android plugin也是基于此,并且擴(kuò)展了HTML報告文件,它將所有連接設(shè)備的報告都合并到一個文件里面。

獨(dú)立項目

一個項目將會自動生成測試運(yùn)行。默認(rèn)位置為:build/reports/androidTests

這非常類似于JUnit的報告所在位置build/reports/tests,其它的報告通常位于build/reports/< plugin >/。

這個路徑也可以通過以下方式自定義:

    android {
        ...

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

報告將會合并運(yùn)行在不同設(shè)備上的測試結(jié)果。

多項目報告

在一個配置了多個應(yīng)用或者多個庫項目的多項目里,當(dāng)同時運(yùn)行所有測試的時候,生成一個報告文件記錄所有的測試可能是非常有用的。

為了實現(xiàn)這個目的,需要使用同一個依賴文件(譯注:指的是使用android gradle插件的依賴文件)中的另一個插件??梢酝ㄟ^以下方式添加:

    buildscript {
        repositories {
            mavenCentral()
        }

        dependencies {
            classpath 'com.android.tools.build:gradle:0.5.6'
        }
    }

    apply plugin: 'android-reporting'

這必須添加到項目的根目錄下,例如與settings.gradle文件同個目錄的build.gradle文件中。

之后,在命令行中導(dǎo)航到項目根目錄下,輸入以下命令就可以運(yùn)行所有測試并合并所有報告:

    gradle deviceCheck mergeAndroidReports --continue

注意:
這里的--continue選項將允許所有測試,即使子項目中的任何一個運(yùn)行失敗都不會停止。如果沒有這個選項,第一個失敗測試將會終止全部測試的運(yùn)行,這可能導(dǎo)致一些項目沒有執(zhí)行過它們的測試。

Lint 支持

Lint支持,譯者注:Lint是一個可以檢查Android項目中存在的問題的工具

從0.7.0版本開始,你可以為項目中一個特定的Variant(變種)版本運(yùn)行l(wèi)int,也可以為所有Variant版本都運(yùn)行l(wèi)int。它將會生成一個報告描述哪一個Variant版本中存在著問題。

你可以通過以下lint選項配置lint。通常情況下你只需要配置其中一部分,以下列出了所有可使用的選項:

    android {
        lintOptions {
            // set to true to turn off analysis progress reporting by lint
            quiet true
            // if true, stop the gradle build if errors are found
            abortOnError false
            // if true, only report errors
            ignoreWarnings true
            // if true, emit full/absolute paths to files with errors (true by default)
            //absolutePaths true
            // if true, check all issues, including those that are off by default
            checkAllWarnings true
            // if true, treat all warnings as errors
            warningsAsErrors true
            // turn off checking the given issue id's
            disable 'TypographyFractions','TypographyQuotes'
            // turn on the given issue id's
            enable 'RtlHardcoded','RtlCompat', 'RtlEnabled'
            // check *only* the given issue id's
            check 'NewApi', 'InlinedApi'
            // if true, don't include source code lines in the error output
            noLines true
            // if true, show all locations for an error, do not truncate lists, etc.
            showAll true
            // Fallback lint configuration (default severities, etc.)
            lintConfig file("default-lint.xml")
            // if true, generate a text report of issues (false by default)
            textReport true
            // location to write the output; can be a file or 'stdout'
            textOutput 'stdout'
            // if true, generate an XML report for use by for example Jenkins
            xmlReport false
            // file to write report to (if not specified, defaults to lint-results.xml)
            xmlOutput file("lint-report.xml")
            // if true, generate an HTML report (with issue explanations, sourcecode, etc)
            htmlReport true
            // optional path to report (default will be lint-results.html in the builddir)
            htmlOutput file("lint-report.html")

       // set to true to have all release builds run lint on issues with severity=fatal
       // and abort the build (controlled by abortOnError above) if fatal issues are found
       checkReleaseBuilds true
            // Set the severity of the given issues to fatal (which means they will be
            // checked during release builds (even if the lint target is not included)
            fatal 'NewApi', 'InlineApi'
            // Set the severity of the given issues to error
            error 'Wakelock', 'TextViewEdits'
            // Set the severity of the given issues to warning
            warning 'ResourceAsColor'
            // Set the severity of the given issues to ignore (same as disabling the check)
            ignore 'TypographyQuotes'
        }
    }