Android Gradle Plugin指南(四)——測試

原文地址:http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Testing


5、Testing(測試)


構建一個測試程序已經被集成到應用項目中,沒有必要再專門建立一個測試項目。


5.1 Basics and Configuration(基本知識和配置)


正如前面所提到的,緊鄰main sourceSet的就是androidTest sourceSet,默認路徑在src/androidTest/下。

在這個測試sourceSet中會構建一個使用Android測試框架,並且可以部署到設備上的測試apk來測試應用程序。這裏麪包含單元測試,集成測試,和後續UI自動化測試。

這個測試sourceSet不應該包含AndroidManifest.xml文件,因爲這個文件會自動生成。


下面這些值可能會在測試應用配置中使用到:

    * testPackageName

    * testInstrumentationRunner

    * testHandleProfiling

    * testfunctionalTest


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

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

在測試應用程序的manifest文件中,instrumentation節點的targetPackage屬性值會自動使用測試應用的package名稱設置,即使這個名稱是通過defaultConfig或者Build Type對象自定義的。這也是manifest文件需要自動生成的一個原因。


另外,這個測試sourceSet也可以擁有自己的依賴。

默認情況下,應用程序和他的依賴會自動添加的測試應用的classpath中,但是也可以通過以下來擴展:

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


測試應用通過assembleTest task來構建。assembleTest不依賴於main中的assemble task,需要手動設置運行,不能自動運行。


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

android {
        ...
        testBuildType "staging"
    }


5.2 Running tests(運行測試)


正如前面提到的,標誌性task connectedCheck要求一個連接的設備來啓動。

這個過程依賴於androidTest task,因此將會運行androidTest。這個task將會執行下面內容:

    * 確認應用和測試應用都被構建(依賴於assembleDebug和assembleTest)。

    * 安裝這兩個應用。

    * 運行這些測試。

    * 卸載這兩個應用。


如果有多於一個連接設備,那麼所有測試都會同時運行在所有連接設備上。如果其中一個測試失敗,不管是哪一個設備算失敗。


所有測試結果都被保存爲XML文檔,路徑爲:

    build/androidTest-results

(這類似於JUnit的運行結果保存在build/test-results)


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

android {
        ...

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

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


5.3 Testing Android Libraries(測試Android庫)


測試Android庫項目的方法與應用項目的方法類似。


唯一的不同在於整個庫(包括它的依賴)都是自動作爲依賴庫被添加到測試應用中。結果就是測試APK不單隻包含它的代碼,還包含了庫項目自己和庫的所有依賴。

庫的manifest被組合到測試應用的manifest中(作爲一些項目引用這個庫的殼)。


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


其它的部分都是類似的。


5.4 Test reports(測試報告)


當運行單元測試的時候,Gradle會輸出一份HTML格式的報告以方便查看結果。

Android plugin也是基於此,並且擴展了HTML報告文件,它將所有連接設備的報告都合併到一個文件裏面。


5.4.1 Single projects(獨立項目)


一個項目將會自動生成測試運行。默認位置爲:build/reports/androidTests


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


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

android {
        ...

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


報告將會合並運行在不同設備上的測試結果。


5.4.2 Multi-projects reports(多項目報告)


在一個配置了多個應用或者多個庫項目的多項目裏,當同時運行所有測試的時候,生成一個報告文件記錄所有的測試可能是非常有用的。


爲了實現這個目的,需要使用同一個依賴文件(譯註:指的是使用android gradle插件的依賴文件)中的另一個插件。可以通過以下方式添加:

buildscript {
        repositories {
            mavenCentral()
        }

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

    apply plugin: 'android-reporting'

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


之後,在命令行中導航到項目根目錄下,輸入以下命令就可以運行所有測試併合並所有報告:

gradle deviceCheck mergeAndroidReports --continue


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


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


從0.7.0版本開始,你可以爲項目中一個特定的Variant(變種)版本運行lint,也可以爲所有Variant版本都運行lint。它將會生成一個報告描述哪一個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'
        }

    }



發佈了29 篇原創文章 · 獲贊 2 · 訪問量 7萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章