原文地址: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'
} }

Android Gradle Plugin指南(四)——測试的更多相关文章

  1. Android Gradle Plugin指南(六)——高级构建定制

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

  2. Android Gradle Plugin指南(三)——依赖关系、android库和多项目配置

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

  3. Android Gradle Plugin指南(五)——Build Variants(构建变种版本号)

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

  4. 读书笔记--Android Gradle权威指南(下)

    前言 最近看了一本书<Android Gradle 权威指南>,收获挺多,就想着来记录一些读书笔记,方便后续查阅. 本篇内容是基于上一篇:读书笔记--Android Gradle权威指南( ...

  5. 读书笔记--Android Gradle权威指南(上)

    本篇文章已授权微信公众号 dasu_Android(大苏)独家发布 最近看了一本书<Android Gradle 权威指南>,对于 Gradle 理解又更深了,但不想过段时间就又忘光了,所 ...

  6. Gradle之Android Gradle Plugin 主要 Task 分析(三)

    [Android 修炼手册]Gradle 篇 -- Android Gradle Plugin 主要 Task 分析 预备知识 理解 gradle 的基本开发 了解 gradle task 和 plu ...

  7. Gradle之Android Gradle Plugin 主要流程分析(二)

    [Android 修炼手册]Gradle 篇 -- Android Gradle Plugin 主要流程分析 预备知识 理解 gradle 的基本开发 了解 gradle task 和 plugin ...

  8. The Android Gradle Plugin and Gradle version-compatibility

    http://tools.android.com/tech-docs/new-build-system/version-compatibility Version Compatibility Post ...

  9. Android AS升级3.1 编译报错:The SourceSet 'instrumentTest' is not recognized by the Android Gradle Plugin.

    AndroidStudio升级到3.1后编译报错:The SourceSet ‘instrumentTest’ is not recognized by the Android Gradle Plug ...

随机推荐

  1. JS实现点击图片,在浏览器中查看。

    工作中遇到要实现点击图片查看的功能,从网上找了一段js代码,可以用. <img src="/pic/${pictureCertificate}" alt="凭证&q ...

  2. day5模块学习--sys模块

    sys模块 sys模块是处理与系统相关的模块,sys(system),下面来看看sys模块常用的方法: 1.sys.argv         #命令行参数list,第一个元素是程序本身路径 2.sys ...

  3. MVC图片上传并显示缩略图

    前面已经说了怎么通过MVC来上传文件,那么这次就说说如何上传图片然后显示缩略图,这个的实用性还是比较大.用UpLoad文件夹来保存上传的图片,而Temp文件夹来保存缩略图,前面文件上传部分就不再重复了 ...

  4. 2017冬季24集训模拟题-24星球的末日(Floyd)

    24 星球的末日[问题描述]24 星球的世界末日就要到了 , 可是诺亚方舟还没有制造完成 . 为了制造诺亚方舟这个星球上的所有国家都站在统一战线 . 现在一共有n个国家 , 一个国家到另一个国家都有一 ...

  5. Ionic实战三:Ionic 图片预览可放大缩小左右滑动demo-iClub图片预览

    这个demo的主要功能有两个,一个是首页的导航向上拉动会浮动在最上面的效果,另一个就是我们平时非常实用的功能,就是图片预览功能 点击可以放大图片,并且可以左右滑动,还可以双击放大缩小图片以及双手指控制 ...

  6. CSUOJ 1162 Balls in the Boxes 快速幂

    Description Mr. Mindless has many balls and many boxes,he wants to put all the balls into some of th ...

  7. In 和Exists

    1.exist,not exist一般都是与子查询一起使用. In可以与子查询一起使用,也可以直接in (a,b.....) 2.exist会针对子查询的表使用索引. not exist会对主子查询都 ...

  8. 磁盘备份工具dcfldd

    磁盘备份工具dcfldd   dcfldd是Kali Linux自带的一款磁盘备份工具.该工具是dd工具的增强版,更适合渗透测试和安全领域.dcfldd提供实时哈希校验功能,确保数据的安全.同时,它还 ...

  9. iOS Sprite Kit教程之xcode安装以及苹果帐号绑定

    iOS Sprite Kit教程之xcode安装以及苹果帐号绑定 其它的网站上下载安装Xcode 有时候,应用商店下载较慢,所以用户也可以选择从其他网站下载Xcode安装文件.下面讲解这种Xcode的 ...

  10. 面向对象设计原则 依赖倒置原则(Dependency Inversion Principle)

    依赖倒置原则(Dependence Inversion Principle)是程序要依赖于抽象接口,不要依赖于具体实现. 简单的说就是要求对抽象进行编程,不要对实现进行编程,这样就降低了客户与实现模块 ...