Gradle jacoco代码覆盖率 - 然后在Jenkins中发布/显示 [英] Gradle jacoco code coverage - Then publish/show in Jenkins

查看:403
本文介绍了Gradle jacoco代码覆盖率 - 然后在Jenkins中发布/显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图为Java应用程序项目设置代码覆盖率。

项目名称:NewApp



项目结构:
$ b


  • src / java / **(源代码)

  • src / java (单元测试-Jnuit)
  • 测试/ it-test(集成测试)
  • test / at-tests(验收测试)
  • li>
  • tomcat / *(包含tomcat启动/停止脚本)

  • xx / .. etc文件夹是常用应用程序所必需的。 b

    $ b

    Gradle版本:1.6



    环境:Linux

    我有一个正在运行的gradle构建脚本,它从构建工件存储库(artifactory / maven for ex)中获取应用程序(NewApp)依赖关系(即应用程序用于构建过程的服务jar),并构建应用程序。 / p>

    现在,我想使用JaCoCo插件为我的NewApp应用程序项目获取代码覆盖率。



    I遵循每个Gradle / Ja的文档可可,但它似乎并没有创建任何报告/ ... jacoco等文件夹,我可以找到Jacoco覆盖报告做了什么。



    我的问题:
    1.为了使用单元测试(Junit)获得代码覆盖率,我假设我所要做的就是以下内容,并且它不会要求我在运行单元测试(测试任务,即gradle测试)之前启动/停止tomcat以通过使用单元测试获取代码覆盖率。请告知/更正。代码(仅用于Gradle jacoco单元测试部分) - 我使用的是:

      apply plugin:'jacoco'

    test {
    include'src / java-test / **'
    }

    jacocoTestReport {
    group =reporting
    description =运行测试后生成Jacoco覆盖率报告。
    reports {
    xml.enabled true
    html.enabled true
    csv.enabled false
    }
    // classDirectories = fileTree(dir:'build / (包括:'com / thc / **')
    additionalSourceDirs = files(sourceSets.main.allJava.srcDirs)
    }

    和集成测试:

      task integrationTest(type:Test){
    include'test / java / **'
    }

    由于jacocoTestReport依赖于测试任务,因此将首先调用它们,然后再调用jacocoTestReport将报告它为代码覆盖找到的内容。


    1. 为了获得集成测试的代码覆盖率,我假设我必须先启动tomcat(即之前运行/调用测试目标进行集成测试),然后调用gradle integrationTest或gradle test任务,然后调用s顶级tomcat - 获取代码覆盖率报告。从其他博客文章中,我也发现应该设置JAVA_OPTS变量来在tomcat启动之前分配jacoco代理。

    例如:设置JAVA_OPTS变量例如:

      export JACOCO = -  Xms256m -Xmx512m -XX:MaxPermSize = 1024m -javaagent:/ production / jenkinsAKS / jobs / NewApp / workspace / jacoco-0.6.3.201306030806 / lib / jacocoagent.jar = destfile = / production / jenkinsAKS / jobs / NewApp / workspace / jacoco / jacoco.exec,append = true,includes = *
    export JAVA_OPTS = $ JAVA_OPTS $ JACOCO




    1. groovy - 我不确定在build.gradle(构建脚本)中应该写些什么代码才能使上述集成/单元测试工作,如果它涉及到tomcat的启动/停止。如果有人可以提供样本脚本来做到这一点,我会尝试。

    2. 现在我没有得到任何代码覆盖率,当我发布雅可可代码时詹金斯的报道(使用詹金斯发布发布Jacoco报告的行为)。 Jenkins构建的仪表板显示代码覆盖率为0%(例如,条形图显示所有红色,实际代码覆盖率不显示绿色)。

      解决方案

      让它工作。

      Gradle 1.7
      - 下载包含二进制文件/ src和文档的.zip文件。
      - 转到文件夹:如果您将以上.zip解压缩到C:\gradle-1.7
      C:\gradle-1.7 \samples\testing\jacoco\quickstart




      • 运行:
        gradle build jacocoTestReport

      • 在构建之后会看到一个新的文件夹build。
        - 如果只调用构建任务,jacoco文件夹将获得classdumps和.exec的创建。
        - 创建jacoco和jacocoHtml文件夹 - 如果同时创建jacocoTestReport



      很有趣。 p>

      我还看到,包含以下内容会更好:



      build.gradle中的以下部分

        ///// 
      tasks.withType(编译){
      options.debug = true
      options.compilerArgs = [-g]
      }
      ////


      I'm trying to setup code coverage for a Java application project.

      Project name : NewApp

      Project structure:

      • src/java/** (source code)
      • src/java-test (unit tests - Jnuit)
      • test/it-test (integration test)
      • test/at-tests (acceptance tests)
      • tomcat/* (contain tomcat start/stop scripts)
      • xx/.. etc folders which are required for a usual application.

      Gradle version : 1.6

      Environment : Linux

      I have a running gradle build script that fetches application (NewApp) dependencies (i.e. service jars used by the app for build process) from a build artifact repository (artifactory/maven for ex), and builds the app.

      Now at this point, I wanted to get code coverage using JaCoCo plugin for my NewApp application project.

      I followed the documentation per Gradle/Jacoco but it doesn't seems to create any reports/... folder for jacoco etc where I can find what Jacoco coverage report did.

      My questions: 1. For getting code coverage using Unit tests (Junit), I assume all I have to do is the following and it will NOT require me to start/stop the tomcat before running unit test (test task i.e. "gradle test") to get code coverage for/via using unit tests. Please advise/correct. The code (just for Gradle jacoco unit test part) - I'm using is:

      apply plugin: 'jacoco'
      
      test {
            include 'src/java-test/**'
      }
      
      jacocoTestReport {
          group = "reporting"
          description = "Generate Jacoco coverage reports after running tests."
          reports {
              xml.enabled true
              html.enabled true
              csv.enabled false
          }
          //classDirectories = fileTree(dir: 'build/classes/main', include: 'com/thc/**')
          //sourceDirectories = fileTree(dir: 'scr/java', include: 'com/thc/**')
          additionalSourceDirs = files(sourceSets.main.allJava.srcDirs)
      }
      

      and for Integration tests:

      task integrationTest(type: Test) {
              include 'test/java/**'
      }
      

      As jacocoTestReport is depends upon test task(s), thus they will be called first and then finally jacocoTestReport will report what it found for the code coverage.

      1. For getting code coverage for integration tests, I assume I must start tomcat first (i.e. before running / calling test target for integration tests), then call "gradle integrationTest" or "gradle test" task and then stop tomcat -- to get the code coverage report. From other blog posts I also found that one should setup JAVA_OPTS variable to assign jacoco agent before tomcat starts.

      for ex: setting JAVA_OPTS variable like:

      export JACOCO="-Xms256m -Xmx512m -XX:MaxPermSize=1024m -javaagent:/production/jenkinsAKS/jobs/NewApp/workspace/jacoco-0.6.3.201306030806/lib/jacocoagent.jar=destfile=/production/jenkinsAKS/jobs/NewApp/workspace/jacoco/jacoco.exec,append=true,includes=*"
      export JAVA_OPTS="$JAVA_OPTS $JACOCO"
      

      1. Being new to Gradle/groovy - I'm not sure what code should I write within build.gradle (build script) to get the above Integration/Unit tests working if it involves start/stop of tomcat. If someone can provide a sample script to do that, I'll try.

      2. I'm not getting any code coverage right now, when I publish Jacoco code coverage in Jenkins (using Jenkins post build action for publishing Jacoco reports). Jenkins build dashboard shows 0% for code coverage (i.e. bars showing all red color, no green for actual code coverage).

      Need your advice to get some traction on this.

      解决方案

      Got it working.

      Gradle 1.7 - download the .zip which contains the binaries/src and documentation. - Go to folder: if you unzip the above .zip at C:\gradle-1.7 C:\gradle-1.7\samples\testing\jacoco\quickstart

      • Run: gradle build jacocoTestReport

      • You’ll see a new folder "build" after the build. – folder jacoco gets created with classdumps and .exec if only build task is called. – folder jacoco and jacocoHtml gets created – if both build jacocoTestReport is called

      have fun.

      I also saw that it’s better to include:

      the following section in build.gradle

      /////
       tasks.withType(Compile) {
       options.debug = true
       options.compilerArgs = ["-g"]
       }
       ////
      

      这篇关于Gradle jacoco代码覆盖率 - 然后在Jenkins中发布/显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆