Android合并覆盖率报告,使用Jenkins + SonarQube + Jacoco实现Android测试覆盖率统计

66b52468c121889b900d4956032f1009.png

8种机械键盘轴体对比

本人程序员,要买一个写代码的键盘,请问红轴和茶轴怎么选?

Jacoco简介JaCoCo is a free code coverage library for Java, which has been created by the EclEmma team based on the lessons learned from using and integration existing libraries for many years.

JaCoCo是一个免费的Java代码覆盖库,由EclEmma团队基于多年来使用和整合现有库的经验教训创建。

开发环境JaCoCo项目托管在 GitHub 上

JaCoCo支持的最低JRE版本是Java 1.5

SonarQube跟踪Jacoco源代码质量问题

Android项目使用Jacoco

jacoco-AndroidGradle插件为Android单元测试创建JaCoCo测试报告

Gradle中配置Jacoco

1.配置project/build.gradle1

2

3

4

5

6

7

8

9

10

11

12

13

14

15buildscript {

repositories {

jcenter()

google()

}

dependencies {

classpath 'com.dicedmelon.gradle:jacoco-android:0.1.2'

// NOTE: Do not place your application dependencies here; they belong

// in the individual module build.gradle files

}

}

2.配置project/jacoco.gradle1

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//apply plugin: 'jacoco'

apply plugin: 'jacoco-android'

jacoco {

toolVersion = "0.7.7.201606060606"

reportsDir = file("$buildDir/JacocoReport")

}

/**

* Error:Cannot add task ':app:jacocoTestReport' as a task with that name already exists.

* 需要加overwrite: true

*/

//task jacocoTestReport(type: JacocoReport, dependsOn: 'testDebugUnitTest') {

task jacocoTestReport(type: JacocoReport, dependsOn: 'testDebugUnitTest',overwrite: true) {

group = "reporting"

reports {

xml.enabled = true

html.enabled = true

}

def debugTree = fileTree(dir: "${buildDir}/intermediates/classes/debug",

excludes: ['**/R.class',

'**/R$*.class',

'**/*$ViewInjector*.*',

'**/BuildConfig.*',

'**/Manifest*.*',

'**/*$Lambda$*.*', // Jacoco can not handle several "$" in class name.

'**/*Module.*', // Modules for Dagger.

'**/*Dagger*.*', // Dagger auto-generated code.

'**/*MembersInjector*.*', // Dagger auto-generated code.

'**/*_Provide*Factory*.*',

'**/*_Factory.*', //Dagger auto-generated code

'**/*$*$*.*' // Anonymous classes generated by kotlin

])

def mainSrc = "${project.projectDir}/src/main/java"

sourceDirectories = files([mainSrc])

classDirectories = files([debugTree])

executionData = files("${buildDir}/outputs/code-coverage/connected/coverage.ec")

}

3.配置project/app/build.gradle1

2

3

4

5

6

7

8

9apply from: "$project.rootDir/jacoco.gradle"

android {

buildTypes {

debug {

testCoverageEnabled true

}

}

}

编写工具类1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23public class{

private (){

throw new IllegalStateException("Utility class");

}

public static int max(int a, int b, int c){

if (a > b) {

if (a > c) {

return a;

} else {

return c;

}

} else {

if (b > c) {

return b;

} else {

return c;

}

}

}

}

编写测试工具类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

32import org.junit.Test;

import static org.junit.Assert.assertEquals;

public class MathUtilTest{

public void test_max_1_2_3(){

assertEquals(3, MathUtil.max(1, 2, 3));

}

public void test_max_1_3_2(){

assertEquals(3, MathUtil.max(1, 3, 2));

}

public void test_max_3_2_1(){

assertEquals(3, MathUtil.max(3, 2, 1));

}

public void test_max_0_0_0(){

assertEquals(0, MathUtil.max(0, 0, 0));

}

public void test_max_0_1_0(){

assertEquals(1, MathUtil.max(0, 1, 0));

}

}

查看结果

1e6070f900889fa4064a4f9d9da5ae9e.png

右击运行,结果可在appbuildJacocoReportjacocoTestDebugUnitTestReporthtmlindex.html中查看

Jenkins集成Jacoco

安装Jacoco插件

插件管理中搜索jacoco安装即可

配置构建1

2##jacocoTestReport 无数据

jacocoTestDebugUnitTestReport

0c48b487587b2d4b484a8bb4fe14f81f.png

配置构建后操作

1.Record JaCoCo coverage report

5f9a9d9a015b9ca5d7ecd29737fa7534.png1

2

3

4##**/outputs/code-coverage/connected/coverage.ec 无数据

**/build/jacoco/testDebugUnitTest.exec

**/build/intermediates/classes/debug

**/src/main/java

2.Publish Junit test result report

117dba02b3cecfb2ad621fb94c0bc9fa.png1app/build/test-results/testDebugUnitTest/*.xml

查看测试报告结果

立即构建后在Test Result中查看

f653c70b5504edff5eee515140cf1b91.png

SonarQube整合Jacoco

可以为SonarQube提供测试执行和代码覆盖率报告。

先决条件

Java插件将重用报表并不生成它们,因此在尝试配置分析以导入这些报表之前,需确保它们已正确生成,而不是空的。测试执行报告必须符合JUnit XML格式。

代码覆盖率报告必须由JaCoCo生成。

配置Jacoco

在Jenkins项目中配置构建Execute SonarQube Scanner,新增jacoco配置

5324ba5ec2f58fb74035278bfce86eed.png1

2sonar.core.codeCoveragePlugin=jacoco

sonar.jacoco.reportPath=app/build/jacoco/testDebugUnitTest.exec

立即构建

重新立即构建即可在SonarQube中查看覆盖率

相关主题

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值