第 5 章 測試

2018-04-13 18:26 更新

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

單元測試

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

基本知識和配置

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

下面這些值可能會在測試應(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è)置,即使這個(gè)名稱是通過defaultConfig或者Build Type對象自定義的。這也是manifest文件需要自動生成的一個(gè)原因。

另外,這個(gè)測試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)行。

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

android {
    ...
    testBuildType "staging"
}

運(yùn)行測試

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

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

如果有多于一個(gè)連接設(shè)備,那么所有測試都會同時(shí)運(yùn)行在所有連接設(shè)備上。如果其中一個(gè)測試失敗,不管是哪一個(gè)設(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庫項(xiàng)目的方法與應(yīng)用項(xiàng)目的方法類似。

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

androidTest task的變改只是安裝(或者卸載)測試APK(因?yàn)闆]有其它APK被安裝)。

其它的部分都是類似的。

測試報(bào)告

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

獨(dú)立項(xiàng)目

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

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

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

android {
    ...

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

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

多項(xiàng)目報(bào)告

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

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

buildscript {
    repositories {
        mavenCentral()
    }

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

apply plugin: 'android-reporting'

這必須添加到項(xiàng)目的根目錄下,例如與settings.gradle文件同個(gè)目錄的build.gradle文件中。

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

gradle deviceCheck mergeAndroidReports --continue

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

Lint 支持

Lint支持,譯者注:Lint是一個(gè)可以檢查Android項(xiàng)目中存在的問題的工具

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

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

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'
    }
}

以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號