Android Gradle Plugin指南(四)——测试

来源:互联网 发布:sql server 2000卸载 编辑:程序博客网 时间:2024/05/01 16:57

原文地址: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对象中配置:

[plain] view plain copy
  1. android {  
  2.     defaultConfig {  
  3.         testPackageName "com.test.foo"  
  4.         testInstrumentationRunner "android.test.InstrumentationTestRunner"  
  5.         testHandleProfiling true  
  6.         testFunctionalTest true  
  7.     }  
  8. }  

在测试应用程序的manifest文件中,instrumentation节点的targetPackage属性值会自动使用测试应用的package名称设置,即使这个名称是通过defaultConfig或者Build Type对象自定义的。这也是manifest文件需要自动生成的一个原因。


另外,这个测试sourceSet也可以拥有自己的依赖。

默认情况下,应用程序和他的依赖会自动添加的测试应用的classpath中,但是也可以通过以下来扩展:

[plain] view plain copy
  1. dependencies {  
  2.     androidTestCompile 'com.google.guava:guava:11.0.2'  
  3. }  

测试应用通过assembleTest task来构建。assembleTest不依赖于main中的assemble task,需要手动设置运行,不能自动运行。


目前只有一个Build Type被测试。默认情况下是debug Build Type,但是这也可以通过以下自定义配置:

[plain] view plain copy
  1. android {  
  2.     ...  
  3.     testBuildType "staging"  
  4. }  

5.2 Running tests(运行测试)


正如前面提到的,标志性task connectedCheck要求一个连接的设备来启动。

这个过程依赖于androidTest task,因此将会运行androidTest。这个task将会执行下面内容:

    * 确认应用和测试应用都被构建(依赖于assembleDebug和assembleTest)。

    * 安装这两个应用。

    * 运行这些测试。

    * 卸载这两个应用。


如果有多于一个连接设备,那么所有测试都会同时运行在所有连接设备上。如果其中一个测试失败,不管是哪一个设备算失败。


所有测试结果都被保存为XML文档,路径为:

    build/androidTest-results

(这类似于JUnit的运行结果保存在build/test-results)


同样,这也可以自定义配置:

[plain] view plain copy
  1. android {  
  2.     ...  
  3.   
  4.     testOptions {  
  5.         resultsDir = "$project.buildDir/foo/results"  
  6.     }  
  7. }  

这里的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>/。


这个路径也可以通过以下方式自定义:

[plain] view plain copy
  1. android {  
  2.     ...  
  3.   
  4.     testOptions {  
  5.         reportDir = "$project.buildDir/foo/report"  
  6.     }  
  7. }  

报告将会合并运行在不同设备上的测试结果。


5.4.2 Multi-projects reports(多项目报告)


在一个配置了多个应用或者多个库项目的多项目里,当同时运行所有测试的时候,生成一个报告文件记录所有的测试可能是非常有用的。


为了实现这个目的,需要使用同一个依赖文件(译注:指的是使用android gradle插件的依赖文件)中的另一个插件。可以通过以下方式添加:

[plain] view plain copy
  1. buildscript {  
  2.     repositories {  
  3.         mavenCentral()  
  4.     }  
  5.   
  6.     dependencies {  
  7.         classpath 'com.android.tools.build:gradle:0.5.6'  
  8.     }  
  9. }  
  10.   
  11. apply plugin: 'android-reporting'  

这必须添加到项目的根目录下,例如与settings.gradle文件同个目录的build.gradle文件中。


之后,在命令行中导航到项目根目录下,输入以下命令就可以运行所有测试并合并所有报告:

[plain] view plain copy
  1. gradle deviceCheck mergeAndroidReports --continue  

注意:这里的--continue选项将允许所有测试,即使子项目中的任何一个运行失败都不会停止。如果没有这个选项,第一个失败测试将会终止全部测试的运行,这可能导致一些项目没有执行过它们的测试。


5.5 Lint support(Lint支持,译者注:Lint是一个可以检查Android项目中存在的问题的工具)


从0.7.0版本开始,你可以为项目中一个特定的Variant(变种)版本运行lint,也可以为所有Variant版本都运行lint。它将会生成一个报告描述哪一个Variant版本中存在着问题。


你可以通过以下lint选项配置lint。通常情况下你只需要配置其中一部分,以下列出了所有可使用的选项:

[plain] view plain copy
  1. android {  
  2.   
  3.     lintOptions {  
  4.   
  5.         // set to true to turn off analysis progress reporting by lint  
  6.         quiet true  
  7.         // if true, stop the gradle build if errors are found  
  8.         abortOnError false  
  9.         // if true, only report errors  
  10.         ignoreWarnings true  
  11.         // if true, emit full/absolute paths to files with errors (true by default)  
  12.         //absolutePaths true  
  13.         // if true, check all issues, including those that are off by default  
  14.         checkAllWarnings true  
  15.         // if true, treat all warnings as errors  
  16.         warningsAsErrors true  
  17.         // turn off checking the given issue id's  
  18.         disable 'TypographyFractions','TypographyQuotes'  
  19.         // turn on the given issue id's  
  20.         enable 'RtlHardcoded','RtlCompat', 'RtlEnabled'  
  21.         // check *only* the given issue id's  
  22.         check 'NewApi', 'InlinedApi'  
  23.         // if true, don't include source code lines in the error output  
  24.         noLines true  
  25.         // if true, show all locations for an error, do not truncate lists, etc.  
  26.         showAll true  
  27.         // Fallback lint configuration (default severities, etc.)  
  28.         lintConfig file("default-lint.xml")  
  29.         // if true, generate a text report of issues (false by default)  
  30.         textReport true  
  31.         // location to write the output; can be a file or 'stdout'  
  32.         textOutput 'stdout'  
  33.         // if true, generate an XML report for use by for example Jenkins  
  34.         xmlReport false  
  35.         // file to write report to (if not specified, defaults to lint-results.xml)  
  36.         xmlOutput file("lint-report.xml")  
  37.         // if true, generate an HTML report (with issue explanations, sourcecode, etc)  
  38.         htmlReport true  
  39.         // optional path to report (default will be lint-results.html in the builddir)  
  40.         htmlOutput file("lint-report.html")  
  41.   
  42.        // set to true to have all release builds run lint on issues with severity=fatal  
  43.   
  44.        // and abort the build (controlled by abortOnError above) if fatal issues are found  
  45.   
  46.        checkReleaseBuilds true  
  47.   
  48.         // Set the severity of the given issues to fatal (which means they will be  
  49.         // checked during release builds (even if the lint target is not included)  
  50.         fatal 'NewApi', 'InlineApi'  
  51.         // Set the severity of the given issues to error  
  52.         error 'Wakelock', 'TextViewEdits'  
  53.         // Set the severity of the given issues to warning  
  54.         warning 'ResourceAsColor'  
  55.         // Set the severity of the given issues to ignore (same as disabling the check)  
  56.         ignore 'TypographyQuotes'  
  57.     }  
  58.   
  59. }  
0 0
原创粉丝点击