tomcat - Jacoco 代码覆盖率在 Tomcat 上收集 0% : JDK 7 + Gradle 1. 5 + Jacoco 0.6.2 + Tomcat 7.0.29

标签 tomcat gradle jacoco

我有以下环境:JDK 7 + Gradle 1.5 + Jacoco 0.6.2 + Tomcat 7.0.29(我的 GIST 中的完整代码 https://gist.github.com/marcellodesales/5496686 )。

以下 build.gradle 是我当前的构建脚本,我尝试从中运行以 Rest-Assured 为 Restful API 编写的集成测试的代码覆盖率。我尝试了 Jacoco 插件 (org.ajoberstar:gradle-jacoco:0.3.0),我尝试使用 Jars 中的 Jacoco 并尝试使用 Agent,但它甚至没有启动(https://stackoverflow.com/a/16035811/433814)。

/*******  Java project plugin *******/
apply plugin: 'java'

/******* IDE plugins ********/
apply plugin: 'eclipse'

/******* Deployment + Test Plugins ******/
apply plugin: 'maven'
apply plugin: 'tomcat'
apply plugin: 'jacoco'

buildscript {

  repositories {
    mavenCentral()
  }
  //******************* Supporting Embedded Tomcat
  dependencies {
    classpath 'org.gradle.api.plugins:gradle-tomcat-plugin:0.9.8'
    classpath 'org.ajoberstar:gradle-jacoco:0.3.0'
  }

}

sourceCompatibility = 1.7
targetCompatibility = 1.7

sourceSets {
  integrationTest {
    java.srcDir file('src/integration-test/java')
    resources.srcDir file('src/integration-test/resources')
  }
}

repositories {
  mavenCentral()
}

dependencies {
  compile 'com.sun.jersey:jersey-client:1.17'
  compile 'com.sun.jersey:jersey-server:1.17'
  compile 'com.sun.jersey:jersey-servlet:1.17'
  compile 'com.sun.jersey:jersey-core:1.17'
  compile 'com.sun.jersey:jersey-json:1.17'
  compile 'com.sun.jersey.contribs:jersey-multipart:1.17'
  compile 'com.google.guava:guava:13.0.1'
  compile 'com.google.code.gson:gson:2.2.2'
  compile 'com.octo.captcha:jcaptcha-all:1.0-RC6'
  compile 'commons-io:commons-io:2.4'
  compile 'javax.ws.rs:jsr311-api:1.1.1'
  providedCompile 'javax.servlet:javax.servlet-api:3.0.1', 'javax.servlet:jsp-api:2.0'
  compile 'log4j:log4j:1.2.17'
  compile 'nl.captcha:simplecaptcha:1.2.1'
  compile group: 'net.sf.json-lib', name: 'json-lib', version: '2.3', classifier: 'jdk15' 
  compile 'org.apache.commons:commons-codec:1.3'
  compile 'org.apache.commons:commons-io:1.3.2'
  compile 'org.codehaus.jackson:jackson-core-asl:1.9.12'
  compile 'org.codehaus.jackson:jackson-xc:1.9.12'
  compile 'org.codehaus.jackson:jackson-mapper-asl:1.9.12'
  compile 'org.codehaus.jackson:jackson-jaxrs:1.9.12'
  compile 'xom:xom:1.1'

  // UNIT Tests
  testCompile 'junit:junit:4.11'

  // Integration Tests
  testCompile 'com.jayway.restassured:rest-assured:1.8.0'
  testCompile 'com.jayway.restassured:json-path:1.8.0'
  testCompile 'org.codehaus.groovy:groovy-all:2.1.3'

  integrationTestCompile sourceSets.main.output
  integrationTestCompile configurations.testCompile
  integrationTestCompile sourceSets.test.output
  integrationTestRuntime configurations.testRuntime

  // Embedded Tomcat Support
  def tomcatVersion = '7.0.29'
  tomcat "org.apache.tomcat.embed:tomcat-embed-core:${tomcatVersion}",
         "org.apache.tomcat.embed:tomcat-embed-logging-juli:${tomcatVersion}"
  tomcat("org.apache.tomcat.embed:tomcat-embed-jasper:${tomcatVersion}") {
    exclude group: 'org.eclipse.jdt.core.compiler', module: 'ecj'
  }

}

// set source file encoding for all compile tasks
tasks.withType(Compile) {
  options.encoding = 'UTF-8'
}

jacoco {
  // change the version of Jacoco in use
  toolVersion = '0.6.2+'
}

test {
  maxParallelForks = 5
  forkEvery = 5

  jacoco {
    // would have been enabled by default
    enabled = true
  }

}

// ########################### Embedded Tomcat Support ###############################
tomcatRun {
  httpPort = 8080
}

[tomcatRun, tomcatRunWar, tomcatStop]*.stopPort = 8081
[tomcatRun, tomcatRunWar, tomcatStop]*.stopKey = 'stopKey'
[tomcatRun, tomcatRunWar]*.outputFile = project.file("$buildDir/tomcat.log")

// ########################## Integration Tests ###############################

task integrationTest(type: Test, dependsOn: jar, description: 'Runs the integration tests.', group: 'verification') {
  println "Starting the integration tests"
  testClassesDir = sourceSets.integrationTest.output.classesDir
  classpath = sourceSets.integrationTest.runtimeClasspath
  testLogging.showStandardStreams = true

  jacoco {
    // would have been enabled by default
    enabled = true
  }
}

integrationTest.doFirst {
  println 'Starting the embedded tomcat server'
  tasks.tomcatRun.daemon=true
  tasks.tomcatRun.execute()
}

integrationTest.doLast {
  println 'Stopping the embedded tomcat server'
  tasks.tomcatStop.execute()
}

import org.ajoberstar.gradle.jacoco.tasks.*

task testReport(type: JacocoReport) {
  executionData test

    // specify one or more source sets that you want to report on the coverage of
    sourceSets project.sourceSets.main

    destPath 'build/reports/coverage-unit'
}

task integrationTestReport(type: JacocoReport) {
    // can include one or more execution files
    executionData integrationTest

    // specify one or more source sets that you want to report on the coverage of
    sourceSets project.sourceSets.main

    destPath 'build/reports/coverage-integration'
}

gradle.taskGraph.afterTask { task ->
   if (task == test) {
     tasks.testReport.execute()
   }
   if (task == integrationTest) {
     tasks.integrationTestReport.execute()
   }
}

尝试使用插件后,我决定从 Jars 手动运行 Jacoco。可以在 https://github.com/marcellodesales/gradle-rest-assured-selenium-jacoco 下载完整的应用程序.这是添加 JVM 选项以添加 JVM 选项的更改的脚本片段:

task integrationTest(type: Test, dependsOn: jar, description: 'Runs the integration tests.', group: 'verification') {
  testClassesDir = sourceSets.integrationTest.output.classesDir
  classpath = sourceSets.integrationTest.runtimeClasspath
  systemProperties['jar.path'] = jar.archivePath

  // use JaCoCo agent to record execution coverage data
  jvmArgs "-javaagent:$configurations.jacoco.asPath=destfile=$buildDir/jacoco-int.exec"
}
check.dependsOn integrationTest

integrationTest.doFirst {
  println 'Starting the embedded tomcat server'
  tasks.tomcatRun.daemon = true
  tasks.tomcatRun.execute()
}

integrationTest.doLast {
  println 'Stopping the embedded tomcat server'
  tasks.tomcatStop.execute()
}

最佳答案

在您的示例代码中,您将构建脚本配置为运行 integTests 的覆盖率而不是生产代码。是要为与tomcat服务器通信的API收集覆盖率数据,还是要为tomcat上运行的代码收集覆盖率数据。对于后者,您必须检测在 tomcat jvm 中执行的代码,而不是 integTest 代码。因此,您必须将 javaagent 设置添加到“tomcatRun”任务。

希望对您有所帮助,

干杯,

雷内

关于tomcat - Jacoco 代码覆盖率在 Tomcat 上收集 0% : JDK 7 + Gradle 1. 5 + Jacoco 0.6.2 + Tomcat 7.0.29,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16324834/

相关文章:

android - 离线如何在我的Android Studio中安装gradle?

gradle - 迁移到 AndroidX 后出现错误 : cannot find symbol import com. google.firebase.iid.InstanceIdResult

maven - 作业终止后的 JaCoCo 内存分配

java - 如何使用 Java 正确运行 Eclemma 覆盖

unit-testing - Jacoco for IntegrationTests 的代码覆盖率报告在 Weblogic 服务器上运行

java.lang.ClassNotFoundException : javax. ws.rs.MessageProcessingException 异常

tomcat - 为什么 Upstart 的工头导出运行 3 个 unix 进程?

java - Quartz 找不到与 Spring 的交易

apache - 在tomcat7中设置子域

spring - org.hibernate.gradle.tools用户和密码在数据库部分不起作用