2017-09-15 104 views
0

现在,随着JUnit 5.0的发布,我正在努力使用jacoco来获取代码覆盖数据。Gradle,Android,Jacoco和JUnit5

我的项目是多模块,我的问题是没有为每个模块创建的exec文件。只有一个在根项目中(这似乎几乎是空的)。我一直无法找到最新的指南。

我试过这里描述的不同方法:Gradle Jacoco and JUnit5没有任何成功。

有没有人有与JUnit5和Jacoco的多模块(非Android)Gradle项目中的工作设置?

还是一个带有JUnit5和Jacoco的Gradle Android项目?任何意见是极大的赞赏。即使有一些官方文档可用,也欢迎丑陋的黑客。我的代码(无论是在命令行,并从IDE至少在运行JUnit5作品)

相关部分:

<Root> build.gradle: 

buildscript { 
    dependencies { 
     ... 
     classpath 'org.junit.platform:junit-platform-gradle-plugin:1.0.0' 
     classpath "de.mannodermaus.gradle.plugins:android-junit5:1.0.0" 
    } 
} 

... 

apply plugin: 'org.junit.platform.gradle.plugin' 

ext.junitVintageVersion = '4.12.0' 
ext.junitPlatformVersion = '1.0.0' 
ext.junitJupiterVersion = '5.0.0' 

subprojects { 

    apply plugin: "jacoco" 
    jacoco { 
     toolVersion = "0.7.6.201602180812" 
    } 

    ... 

    junitPlatform { 
     filters { 
      engines { 
       include 'junit-jupiter', 'junit-vintage' 
      } 
      tags { 
       exclude 'slow' 
      } 
      includeClassNamePatterns '.*Test', '.*Tests' 
     } 
    } 
} 
... 
dependencies { 
    // org.junit.platform.commons.util.PreconditionViolationException: 
    // Cannot create Launcher without at least one TestEngine; consider adding an engine implementation JAR to the classpath 
    testCompile("org.junit.jupiter:junit-jupiter-engine:${junitJupiterVersion}") 
} 

回答

1

我已经试过这里描述的不同的方法:Gradle Jacoco and JUnit5没有任何成功。

对于非Android项目,这些方法看起来不错。

有没有人在使用JUnit5和Jacoco的多模块(非Android)Gradle项目中有工作设置?

但无论如何下面是一个完整的实例非Android的项目。

还是一个带有JUnit5和Jacoco的Gradle Android项目?

注意Android的项目是完全不同的野兽相比,非Android,包括但不限于测试配置/执行和覆盖范围的计算。

特别是 - 默认情况下,JaCoCo库(http://www.jacoco.org/jacoco/index.html)在使用Java代理执行应用程序期间,会更改用于“即时”记录覆盖范围的类。而这种模式正是默认的JaCoCo Gradle插件(https://docs.gradle.org/4.1/userguide/jacoco_plugin.html)提供的。

Java代理不能在Android上使用,所以Android的更改会在Android Gradle插件(https://developer.android.com/studio/releases/gradle-plugin.html)生成过程中“离线”(http://www.jacoco.org/jacoco/trunk/doc/offline.html)。

似乎也org.junit.platform:junit-platform-gradle-plugin不适用于Android的项目 - 有https://github.com/aurae/android-junit5和时间在https://github.com/aurae/android-junit5/issues/4

约为覆盖率公开讨论并非是Android的项目的日常开发者,我不能提供比更多信息这个。


settings.gradle

include 'a' 
include 'b' 

build.gradle

buildscript { 
    repositories { 
    mavenCentral() 
    } 
    dependencies { 
    classpath 'org.junit.platform:junit-platform-gradle-plugin:1.0.0' 
    } 
} 

ext.junitJupiterVersion = '5.0.0' 

subprojects { 
    repositories { 
    mavenCentral() 
    } 

    apply plugin: 'java' 

    apply plugin: 'org.junit.platform.gradle.plugin' 

    apply plugin: 'jacoco' 
    jacoco { 
    toolVersion = '0.7.9' 
    applyTo junitPlatformTest 
    } 
    junitPlatformTest { 
    jacoco { 
     destinationFile = file("$buildDir/jacoco/test.exec") 
    } 
    } 

    dependencies { 
    testCompile("org.junit.jupiter:junit-jupiter-api:${junitJupiterVersion}") 
    testRuntime("org.junit.jupiter:junit-jupiter-engine:${junitJupiterVersion}") 
    } 
} 

task wrapper(type: Wrapper) { 
    gradleVersion = '4.1' 
} 

a/src/main/java/A.java

public class A { 
} 

a/src/test/java/ATest.java

public class ATest { 
    @org.junit.jupiter.api.Test 
    public void test() { 
    new A(); 
    } 
} 

b/src/main/java/B.java

public class B { 
} 

b/src/test/java/BTest.java

public class BTest { 
    @org.junit.jupiter.api.Test 
    public void test() { 
    new B(); 
    } 
} 

gradle test jacocoTestReport文件a/build/jacoco/test.execb/build/jacoco/test.exec执行产生之后,以及下面报告中显示。

a/build/reports/jacoco/test/html/index.html

a/build/reports/jacoco/test/html/index.html

b/build/reports/jacoco/test/html/index.html

b/build/reports/jacoco/test/html/index.html

+0

当然,我忘了。我仍然努力工作。至少我越来越近了:https://github.com/aurae/android-junit5/issues/4 – Alix

+0

不得不删除评论,因为它引用了错误的问题 – Alix

0

感谢Godins的例子,我能得到一些*代码覆盖工作的指导意见(通过打开生成的验证。 exec文件,并检查是否显示覆盖率):

*从JUnit4有些意思覆盖测试JUnit5

下运行
gradlew junitPlatformTestDebug 

- >$buildDir/jacoco/junitPlatformTestDebug.exec

settings.gradle:

include 'a' 
include 'b' 

的build.gradle(父):

buildscript { 
    dependencies { 
    classpath 'com.android.tools.build:gradle:2.3.3' 
    classpath 'de.mannodermaus.gradle.plugins:android-junit5:1.0.0' 
    } 
} 

subprojects { 

    apply plugin: 'jacoco' 
    jacoco { 
    toolVersion = '0.7.9' 
    } 

} 

build.gradle(a - 主要应用程序):

apply plugin: 'com.android.application' 
apply plugin: 'de.mannodermaus.android-junit5' 

android { 
    testOptions { 
     unitTests.all { 
      jacoco { 
       includeNoLocationClasses = true 
      } 
     } 
    } 
    junitPlatform { 
     jupiterVersion '5.0.0' 
     vintageVersion '4.12.0' 
    } 
} 
dependencies { 
    testCompile junit5() 
} 
project.afterEvaluate { 
    // Workaround: https://stackoverflow.com/questions/39362955/gradle-jacoco-and-junit5/39386661#39386661 
    apply plugin: "jacoco" 
    jacoco { 
     applyTo junitPlatformTestDebug 
    } 
} 

的build.gradle(二 - Android的库模块):

apply plugin: 'com.android.library' 
apply plugin: 'de.mannodermaus.android-junit5' 

android { 
    testOptions { 
     unitTests.all { 
      jacoco { 
       includeNoLocationClasses = true 
      } 
     } 

    junitPlatform { 
     jupiterVersion '5.0.0' 
     vintageVersion '4.12.0' 
    } 
} 
dependencies { 
    testCompile junit5() 
} 
project.afterEvaluate { 
    // Workaround: https://stackoverflow.com/questions/39362955/gradle-jacoco-and-junit5/39386661#39386661 
    apply plugin: "jacoco" 
    jacoco { 
     applyTo junitPlatformTestDebug 
    } 
}