gradle很好用,但是默认是没有代码覆盖功能的,只好自己写。曾经在网上找到过别人的一段脚本,虽然也能用,但是有一些不爽的地方,一个原因是它不支持对层级工程中全部代码的覆盖,另一个原因是它用替换build/classes/main里面的class文件,再依赖gradle的单元方式来实现的。我自己写了一个代码覆盖的脚本,可以避免这两个问题,代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
allprojects {
    apply plugin:'idea'
}
subprojects {
    apply plugin:'java'
    apply plugin:'eclipse'
    apply plugin:'maven'
    apply plugin:'project-report'
    sourceCompatibility = 1.6
    targetCompatibility = 1.6
    [compileJava, compileTestJava]*.options*.encoding = 'UTF-8'
    dependencies {
        runtime 'org.slf4j:slf4j-log4j12:1.4.2@jar'
        testCompile 'junit:junit:4.8.2'
        testCompile 'org.easymock:easymock:3.0'
        testRuntime module('net.sourceforge.cobertura:cobertura:1.9.4') {
            dependencies "asm:asm:3.1", "oro:oro:2.0.8", "asm:asm-tree:3.0"
        }
        testRuntime 'log4j:log4j:1.2.13'
        testRuntime('org.apache.ant:ant-junit:1.8.2'){transitive = false}
    }
    /* START 代码覆盖 */
    task runCover(dependsOn: testClasses) << {
        def codeCoverDir = new File(buildDir, "codeCover")
        def codeCoverClassesDir = new File(codeCoverDir, "classes")
        def codeCoverTestReportDir = new File(codeCoverDir, "testReport")
        def codeCoverDataFile = new File(codeCoverDir, "cobertura.ser")
        def originalClassesDir = new File(buildDir, "classes/main")
        def unitTestClassesDir = new File(buildDir, "classes/test")
        def projectPath = project.path
        ant {
            delete(dir: codeCoverDir, failonerror:false)
            mkdir(dir: codeCoverDir)
            mkdir(dir: codeCoverClassesDir)
            mkdir(dir: codeCoverTestReportDir)
            if (!unitTestClassesDir.exists()) {
                mkdir(dir: unitTestClassesDir)
            }
            taskdef(resource:'tasks.properties', classpath: configurations.testRuntime.asPath)
            taskdef(name: 'junit', classname: 'org.apache.tools.ant.taskdefs.optional.junit.JUnitTask',
                    classpath: configurations.testRuntime.asPath)
            copy(todir: codeCoverClassesDir) {
                fileset(dir: originalClassesDir)
            }
            logger.lifecycle("cobertura-instrument: ${projectPath}")
            'cobertura-instrument'(datafile:codeCoverDataFile) {
                fileset(dir: codeCoverClassesDir, includes:"**/*.class")
            }
            logger.lifecycle("junit: ${projectPath}")
            junit(haltonfailure: true, showoutput: true, fork: true, forkmode:'once') {
                sysproperty(key: "net.sourceforge.cobertura.datafile", value: codeCoverDataFile)
                classpath {
                    pathelement(path: configurations.testRuntime.asPath)
                    pathelement(location: codeCoverClassesDir)
                    pathelement(location: unitTestClassesDir)
                }
                formatter(type: 'plain')
                batchtest(todir: codeCoverTestReportDir) {
                    fileset(dir: unitTestClassesDir, includes: "**/*Test.class")
                }
            }
        }
    }
    task reportCover(dependsOn: runCover) << {
        def codeCoverDir = new File(buildDir, "codeCover")
        def codeCoverReportDir = new File(codeCoverDir, "coverReport")
        def codeCoverDataFile = new File(codeCoverDir, "cobertura.ser")
        ant {
            mkdir(dir: codeCoverReportDir)
            taskdef(resource:'tasks.properties', classpath: configurations.testRuntime.asPath)
            'cobertura-report'(destdir: codeCoverReportDir, format:'html', datafile:codeCoverDataFile, encoding:'utf8') {
                fileset(dir: "${projectDir}/src/main/java", includes: "**/*.java")
            }
        }
    }
    /* END */
}
/**
 * 在根目录的build/codeCover/coverReport目录里生成整个工程的代码覆盖报告。必须至少有一个子工程存在,才能正常执行
 */
task reportCoverAll(dependsOn: subprojects.collect{"${it.path}:runCover"}) << {
    def codeCoverDir = new File(buildDir, "codeCover")
    def codeCoverReportDir = new File(codeCoverDir, "coverReport")
    def codeCoverDataFile = new File(codeCoverDir, "cobertura.ser")
    ant {
        mkdir(dir: codeCoverReportDir)
        taskdef(resource:'tasks.properties', classpath: subprojects.toArray()[0].configurations.testRuntime.asPath)
        'cobertura-merge'(datafile: codeCoverDataFile) {
            fileset(dir: rootDir, includes: "*/build/codeCover/cobertura.ser")
        }
        'cobertura-report'(destdir: codeCoverReportDir, format:'html', datafile:codeCoverDataFile, encoding:'utf8') {
            subprojects.each {
                fileset(dir: "${it.projectDir}/src/main/java", includes: "**/*.java")
            }
        }
    }
}

实现的思路就是在每个子工程的build目录下生成codeCover目录,然后把testClasses生成的被测试的代码通过cobertura加工到这个目录下的子目录,再调用junit测试,最后生成报告,与gradle java插件里面的test任务没有任何关系。根工程里面的reportCoverAll可以把各子工程生成的cobertura.ser文件合并,生成统一的报告。

This work, unless otherwise expressly stated, is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License.

http://sulong.me/2011/08/03/use_cobertura_in_gradle_in_a_better_way

gradle中使用cobertura做代码覆盖(转)的更多相关文章

  1. 完美解决--用VS中的Git做代码管理器,与他人共享代码

    1.创建代码仓库,这里说一下为什么要创建仓库,Git不能够作为源代码管理器,vs中自带的也只能够在本地进行管理,要和他们共享的话必须要有服务器端去存储代码,类似于SVN,它就有客户端和服务器端,这里推 ...

  2. [资源]--完美解决--用VS中的Git做代码管理器,与他人共享代码

    1.创建代码仓库,这里说一下为什么要创建仓库,Git不能够作为源代码管理器,vs中自带的也只能够在本地进行管理,要和他们共享的话必须要有服务器端去存储代码,类似于SVN,它就有客户端和服务器端,这里推 ...

  3. 测者的测试技术手册:自动化单元工具EvoSuie的代码覆盖报告

    EvoSuite是由Sheffield等大学联合开发的一种开源工具,用于自动生成测试用例集,生成的测试用例均符合Junit的标准,可直接在Junit中运行.得到了Google和Yourkit的支持. ...

  4. java代码覆盖实战

    Jacoco原理 代码插桩 On-the-fly插桩: JVM中通过-javaagent参数指定特定的jar文件启动Instrumentation的代理程序,代理程序在通过Class Loader装载 ...

  5. Gradle中的buildScript代码块

    在编写Gradle脚本的时候,在build.gradle文件中经常看到这样的代码: build.gradle 1 2 3 4 5 6 7 8 9 buildScript { repositories ...

  6. [转] Gradle中的buildScript代码块

    PS: 在build script中的task apply plugin: 'spring-boot' 需要 classpath("org.springframework.boot:spri ...

  7. VS中代码覆盖问题

    在VS中编写代码时,需要插入代码是,经常是将插入点后面的代码覆盖掉而不是将它向后推. 解决这样的问题,只需要按   Insert 键即可, 我的笔记本是   Fn 加 del

  8. 测者的测试技术手册:自动的自动化框架EvoSuite集成Cobertura得到可视化的代码覆盖报告

    EvoSuite是由Sheffield等大学联合开发的一种开源工具,用于自动生成测试用例集,生成的测试用例均符合Junit的标准,可直接在Junit中运行.得到了Google和Yourkit的支持. ...

  9. C/C++代码覆盖工具gcov与lcov入门

    C/C++代码覆盖工具gcov与lcov入门 gcov是一个可用于C/C++的代码覆盖工具,是gcc的内建工具.下面介绍一下如何利用gcov来收集代码覆盖信息.想要用gcov收集代码覆盖信息,需要在g ...

随机推荐

  1. HttpClient4的使用,模拟浏览器登陆新浪微博,发表微博和文字+图片微博

    HttpClient4,最原始的需求就是使用其来模拟浏览器想服务器发起http请求,当然,他的功能不止于此,但是我需要的就是这个功能而已,jdk也有其自带的类似的api:UrlConnection,效 ...

  2. 设置 zend studio 默认编码为UTF8

    今天用zend studio 打开文件时发现为乱码,这肯定是编码出了问题,我看了一下果然是编码出了问题,默认的是以GBK编码方式打开,我换utf8编码打开就好了,换编码打开的方法是: 1点击工具栏中的 ...

  3. M3U8格式解说及实际应用分析

    M3U8有啥优点 ? 网上搜索了一下,大家众说纷纭,个人理解主要是能够做多码率的适配,依据网络带宽,client会选择一个适合自己码率的文件进行播放,保证视频流的流畅. 在IOS device和mac ...

  4. ubuntu软件中心崩溃

    网上找了下别人的解决方法(本人測试成功解决此问题): 提示说是lists出错 我的正是这样的情况 使用例如以下命令能够修复: 1.删除lists sudo rm /var/lib/apt/lists/ ...

  5. 使用装饰器模式动态设置Drawable的ColorFilter

    使用装饰器模式动态设置Drawable的ColorFilter 欢迎各位关注我的新浪微博:微博 转载请标明出处(kifile的博客) 非常多时候我们都希望Android控件点击的时候,有按下效果,选中 ...

  6. 黄聪:Microsoft Enterprise Library 5.0 系列教程(一) Caching Application Block (高级)

    原文:黄聪:Microsoft Enterprise Library 5.0 系列教程(一) Caching Application Block (高级) Caching Application Bl ...

  7. C++ 中的比較函数

    在敲代码的时候,排序是一种和经常使用的算法.在排序中.比較又是当中最经常使用的操作.这里,我们来分析一下C++中的比較问题. 当中,基本数据类型int. float.string等已经提供了默认的比較 ...

  8. 认为C/C++很难理解、找工作面试笔试,快看看这本书!

    假设你是C/C++谁刚开始学习,看这本书.因为也许你读其他的书还不如不看.一定要选择一本好书. 假设你正在准备工作,请认真看这本书,由于这本书会教会你工作中必备的知识,相信你即将面临的语法类题目不会超 ...

  9. uva 571 素数的性质

    给定 两个杯子,容量分别分Ca,Cb, 要我们用这两个瓶子倒来倒去,得到某个瓶子里装有N的水 而且给的数据保证  Cb > N,且Ca,Cb互质 那么我们肯定可以在容量为Cb的杯子里得到N的水 ...

  10. mysql重装后出现乱码解决办法

    查看当前连接系统参数:SHOW VARIABLES LIKE '%char%'; mysql> show variables like 'char%'; +------------------- ...