首页 文章

Jacoco单元和集成测试覆盖范围 - 个人和整体[关闭]

提问于
浏览
5

我有一个项目(ProjectA),其中包含一些单元测试和集成测试 .

以下是结构 .

ProjectA - src / java(java源代码)

  • test / java(Junit单元测试)
  • 测试/资源(Junit单元测试所需的资源)
  • src / java-test(集成测试)
  • conf(包含构建/测试/编译目的所需的.xml文件)

我运行以下命令 - 所有这些命令都有效,但我怀疑build.gradle / GRADLE_HOME / init.d / * .gradle文件中的配置如何影响我所获得的 .

好像我错过了一些东西而没有得到我想要的东西 .

Commands

  • gradle clean build - 它工作正常
  • gradle clean build jacocoTestReport - 它工作正常 .
  • gradle clean build integrationTest jacocoTestReport - 它工作正常(如果我有一个tomcat实例并在同一个ProjectA的另一个putty窗口中运行) .

在第3个子弹操作完成后,我在Jenkins作业工作区中看到额外的文件夹“build”及其子文件夹(除了从源/版本控制中检出的文件夹) .

即在 - JenkinsWorkspace下
/ build

  • classes (包含以下内容的.class文件,作为sourceSets部分之一提及)
    ---- integrationTest
    ----主要
    ----测试

  • resources (这包含源控件中"conf"文件夹下的所有文件.properties / .xml文件 .

  • reports (包含.xml / .html文件,用于单元或IT测试的PMD / CheckStyle / Findbugs和测试结果,但不包括两者) . ---- checkstyle
    ---- findbugs
    ---- pmd
    ---- jacoco
    ----测试( Note :这是复数,即它不是"test",它被定义为sourceSets中的一个条目)

  • jacoco (这包含2个.exec文件,即test.exec和integrationTest.exec都有不同的文件大小)
    ---- test.exec
    ---- integrationTest.exec

  • jacocoHtml (此文件夹包含许多文件夹(包含.html文件),主要包含"index.html" .
    ---- somexxxfolders
    ---- ---- somexxfolder's.html文件
    ---- index.html
    ----其他等文件/文件夹

  • test-results (这包含一些.xml文件但仅适用于单元测试或集成测试 - 但对于给定时间的两种测试类型都是 not ) .

即如果我运行"gradle clean build",那么你将看到与单元测试相关的.xml文件,如果我运行"gradle clean build integrationTest",则单元测试.xml文件被覆盖,我看到的.xml文件只与integrationTest任务相关/生成 .

以下是常见的gradle之一(GRADLE_HOME / init.d / some.common.gradle文件)

//
//Extra file can hold global Gradle settings so that these dont have to be inserted in project
//specific build.gradle file.
//Filename: extraN.common<anyname>.gradle
allprojects {
   apply plugin: 'java'
   apply plugin: 'pmd'
   apply plugin: 'findbugs'
   apply plugin: 'checkstyle'
   apply plugin: 'jacoco'
   apply plugin: 'sonar-runner'
   tasks.withType(Compile) {
     options.debug = true
     options.compilerArgs = ["-g"]
   }
   sourceSets {
      main {
         java {
            // MOTE: If your project's build.gradle specify the sourceSet section, the following
            // values will be overwritten by what project's build.gradle will set.
            //
            // If you project's sourceSet structure if different in each project, then in this
            // global common .gradle file, you can define srcDir for main or other sections like
            // test, integrationTest etc as shown below -- commented out. If that's the case, 
            // then uncomment the below line and comment out using // -- srcDir 'src/java' line
            // for sourceSets.main.java section. This rule applies to other sections as well.
            // srcDir 'no_src_dir_set_yet'

            srcDir 'src/java'
         }
         resources {
            srcDir 'conf'
         }
      }
      test {
         java {
            srcDir 'test/java'
         }
         resources {
            srcDir 'test/resources'
            srcDir 'conf'
         }
      }
      integrationTest {
         java {
            srcDir 'src/java-test'
         }
         resources {
            srcDir 'conf'
         }
      }
   }
   def sonarServerUrl = "dev.sandbox.server.com"
   sonarRunner {
      sonarProperties {
         property "sonar.host.url", "http://$sonarServerUrl:9000"
         property "sonar.jdbc.url", "jdbc:h2:tcp://$sonarServerUrl:9092/sonar"
         property "sonar.jdbc.driverClassName", "org.h2.Driver"
         property "sonar.jdbc.username", "sonar"
         property "sonar.jdbc.password", "sonar"
         properties ["sonar.sources"] += sourceSets.main.allSource.srcDirs
         //properties ["sonar.tests"] += sourceSets.test.java.srcDirs
         properties ["sonar.tests"] += sourceSets.integrationTest.allSource.srcDirs
      }
   }
   checkstyle {
        configFile = new File(rootDir, "config/checkstyle.xml")
        ignoreFailures = true
        //sourceSets = [sourceSets.main, sourceSets.test, sourceSets.integrationTest]
        sourceSets = [sourceSets.main]
   }
   findbugs {
        ignoreFailures = true
        sourceSets = [sourceSets.main]
   }
   pmd {
        ruleSets = ["basic", "braces", "design"]
        ignoreFailures = true
   }
   jacoco {
      toolVersion = "0.6.2.201302030002"
      reportsDir = file("$buildDir/customJacocoReportDir")
   }
   task testReport(type: TestReport) {
      destinationDir = file("$buildDir/reports/allTests")
   }
   test {
        jacoco {
            //destinationFile = file("$buildDir/jacoco/jacocoTest.exec")
            destinationFile = file("$buildDir/jacoco/test.exec")
            //classDumpFile = file("$buildDir/jacoco/classpathdumps")
            classDumpFile = file("$buildDir/build/classes/test")
        }
   }
   jacocoTestReport {
         group = "Reporting"
         description = "Generate Jacoco coverage reports after running tests."
         reports {
                xml{
                    enabled true
                    destination "${buildDir}/reports/jacoco/jacoco.xml"
                }
                csv.enabled false
                html{
                    enabled true
                    destination "${buildDir}/jacocoHtml"
                }
        }
        additionalSourceDirs = files(sourceSets.main.allJava.srcDirs)
        //additionalSourceDirs = files([sourceSets.main.allJava.srcDirs, xxxx, 'xxxxyyyy' ])
   }
}

build.gradle 文件看起来像:

import com.tr.ids.gradle.CustomFileUtil
apply plugin: 'CustomSetup'
apply plugin: 'java'
apply plugin: 'customJarService'
apply plugin: 'customWarService'
sourceSets {
   main {
      java {
         srcDir 'src/java'
      }
   }
   test {
      java {
         srcDir 'test/java'
      }
      resources {
         srcDir 'test/resources'
         srcDir 'conf'
      }
   }
   integrationTest {
      java {
         srcDir 'src/java-test'
      }
   }
}
// Read dependency lists from external files. Our custom plugin reads a dep-xxx.txt file for compile/test/war related .jar file entries
// where each entry is like: groupid:artifactid:x.x.x
// and these artifact jars are available in Artifactory
List depListCompile = customFileUtil.readIntoList( "$projectDir/dep-compile.txt" )
List depListTest = customFileUtil.readIntoList( "$projectDir/dep-testArtifacts.txt" )
List depListWar = customFileUtil.readIntoList( "$projectDir/dep-war.txt" )
// Define dependencies
dependencies {
   // Compilation
   compile  depListCompile
   // Unit Tests
   testCompile depListTest
   // Integration tests
   // Everything from compile and testCompile targets
   integrationTestCompile configurations.compile
   integrationTestCompile configurations.testCompile
   // Output of compiling "main" files
   integrationTestCompile sourceSets.main.output
   // Additional dependencies from war and others
   integrationTestCompile depListTest, depListWar
   // All configuration files
   integrationTestRuntime files( 'conf' )
}
task deployTomcat( type: Copy, dependsOn: [ jar, compileIntegrationTestJava, warService ] ) {
   from "$buildDir/customWar/${project.name}.war"
   into "$projectDir/tomcat/webapps"
}
build {
  dependsOn deployTomcat
}
task integrationTest( type: Test, dependsOn: cleanTest ) {
   jacoco {
      //destinationFile = file("$buildDir/jacoco/jacocoTest.exec")
      destinationFile = file("$buildDir/jacoco/integrationTest.exec")
      //classDumpFile = file("$buildDir/jacoco/classpathdumps")
      classDumpFile = file("$buildDir/classes/integrationTest")
   }
   testClassesDir = sourceSets.integrationTest.output.classesDir
   classpath = sourceSets.integrationTest.runtimeClasspath
}
apply plugin: 'eclipse'
eclipse.classpath {
   // Define output directory so Eclipse does not accidentally clobber /bin
   defaultOutputDir = file( 'out/classes' )
   // Add integration test
   plusConfigurations += configurations.integrationTestCompile
   // Remove unnecessary files
   file.whenMerged { classpath ->
      classpath.entries.removeAll { entry -> ( entry.path.indexOf( '/build/classes/main' ) > 0 ) }
      classpath.entries.removeAll { entry -> ( entry.path.indexOf( '/build/resources/main' ) > 0 ) }
   }
}

My questions

1)为什么"gradle clean build integrationTest" - 它正在成功运行,它会覆盖构建/报告/测试和构建/测试结果文件夹中的测试结果 .

2)无论我在.comc文件下为普通gradle文件提供什么名称进行测试,在build.gradle中为jacoco的integrationTest任务,它始终创建test.exec和integrationTest.exec文件,但生成的build / jacocoHtml文件夹index.html文件不显示与单元/集成测试相关的coverage /文件 . 为了证明这一点,如果我运行“gradle clean build IntegrationTest jacocoTestReport sonarRunner”,我会看到作业的工作区,现在包含“.sonar”文件夹和build / reports / sonar文件夹,其中包含另一个名为“overall-xxx.exec”的文件“某些文件,但该文件大小不接近Unit test.exec和IT integrationTest.exec文件大小的”总和“ . 虽然它比test.exec文件大小少了几个字节 .

3)我可以设置什么配置以对Unit和IT测试进行全面覆盖,即整体... exec文件获得良好的大小(在运行sonarRunner任务之后) . 在sonarRunner任务期间,我确实看到SonarRunner任务的“jacocoSensor步骤”确实同时看到UT和IT .exec文件以及整个.exec文件(Sonar的一个很好的功能) .

2 回答

  • 4

    找到我的第二个问题的答案 . 高级信息:

    • Gradle 1.6 jacocoTestReport使用不同的变量,Gradle> = 1.7使用不同的变量 .

    例如:我们可以通过使用CORRECT变量更改“test”或“integrationTest”任务来调整单元测试和集成测试.exec文件创建 - 或者它不会生成“test.exec”和“integrationTest.exec”默认文件名 . 见下面的例子 .

    task integrationTest(type: Test)test 部分可以为我们使用的给定Gradle版本提供正确的变量 .

    task integrationTest (type: Test) { 
    testClassesDir = sourceSets.integrationTest.output.classesDir
    
    classpath = sourceSets.integrationTest.runtimeClasspath
    
       testReportDir = file("$buildDir/reports/tests/IT")
       testResultsDir = file("$buildDir/test-results/IT")
    
    
    
      ignoreFailures = true
       jacoco {
    
     //This works with 1.6
     destPath = file("$buildDir/jacoco/IT/jacocoIT.exec")
     classDumpPath = file("$buildDir/jacoco/IT/classpathdumps")
    
     /*
     Following works only with versions >= 1.7 version of Gradle
     destinationFile = file("$buildDir/jacoco/IT/jacocoIT.exec")
     classDumpFile = file("$buildDir/jacoco/IT/classpathdumps")
     */
       }
    }
    

    同样,对于test 任务,您可以将其定义为../../UT/jacocoUT.exec和../../ UT / classpathdumps ...

    如果我在项目的工作区中运行“sonar-runner”Linux / Unix声纳 - 跑步器可执行文件,则会创建

    • .sonar文件夹,但如果我运行调用Gradle的“Jenkins”作业“clean build integrationTest jacocoTestReport sonarRunner”,则会创建build / sonar文件夹并成为SONAR的工作DIR(这在输出期间显示) .

    • 在Jenkins>在Post build部分下,我在Jenkins仪表板上提到了以下和NOW,jacoco代码覆盖率报告两个jacoco文件(.html和.xml) - 包括单元和集成测试代码覆盖率数据 .

    詹金斯的记录Jacoco覆盖率报告部分包含以下框:

    我提到:

    exec文件的路径: /build/jacoco/UT/jacocoUT.exec, /build/jacoco/IT/jacocoIT.exec Path to class dirs: /build/jacoco/ / classpathdumps / com / thc(这是Jacoco检测类所在的位置).. build / jacoco / UT / classpathdumps / com / thc和build / jacoco / IT / classpathdumps / com / thc将被选为将被替换为build / jacoco下的任何文件夹 . 此值也可以设置为"build/classes"文件夹 .

    源文件的路径:(如果我使用src / java,源文件的链接很少不起作用,即文件内容不显示)..使用 ..它现在有效 .

    休息箱 - 留空 .

    • 还在想 - 为什么整体-jacoco.exec没有文件大小= jacocoUT.exec和jacocoIT.exec的总和

    此时,我可以看到单元和集成测试的Jacoco代码覆盖,即通过访问作业仪表板上的Jacoco代码覆盖图像和访问源代码链接,以及如果你去浏览“build / reports / jacoco / html” /index.html“或”build / jacocoHtml / index.html“文件 .

    仍然试图找出 - 需要做什么才能让SONAR选择这两个.exec文件(我为sonar.xxx设置了有效值,为源代码,测试,二进制文件,...报告路径等设置了UT / IT exec文件的变量...在SonarQube仪表板上,单元测试覆盖率显示正常,但集成测试覆盖率仍为0.0% .

    我将尽快粘贴common.gradle和project的build.gradle文件的副本....以便更好看 .

  • 1

    好的,在这篇文章中找到了UT / IT文件夹问题的解决方案和我的问题(1) . 它实际上是“cleanTest”任务,它是Gradle中的默认RULE之一 .

    运行“gradle tasks -all”可以提供Gradle支持的大量任务,最后这个输出会告诉我们如果我们调用clean这样的规则,那么它将擦除这些文件夹 . 正如您在上面的代码中看到的那样,IntegrationTest依赖于cleanTest,因此当我调用“gradle clean build integrationTest”时,它首先运行单元测试(通过构建任务,默认情况下单元测试运行,并在Gradle中使用构建步骤)然后进行集成测试因此,通过“integrationTest”任务,在运行集成测试时,它调用了cleanTest任务,它清除了我在公共gradle脚本(/init.d/commmon-some-name.gradle文件)中提到的“UT”文件夹我提到了报告/结果目录的IT文件夹 .

    从integrationTest任务中删除cleanTest作为dependsOn,解决了擦除问题 .

    task integrationTest( type: Test, dependsOn: cleanTest ) {
    //task integrationTest( type: Test ) {
    

    输出以下命令:仅显示最后几行...

    gradle tasks -all

    integrationTest
        classes - Assembles binary 'main'.
        cleanTest
        compileIntegrationTestJava - Compiles source set 'integrationTest:java'.
        compileJava - Compiles source set 'main:java'.
        integrationTestClasses - Assembles binary 'integrationTest'.
        processIntegrationTestResources - Processes source set 'integrationTest:resources'.
        processResources - Processes source set 'main:resources'.
    jarService
    sonarRunner [test]
    
    Rules
    -----
    Pattern: build<ConfigurationName>: Assembles the artifacts of a configuration.
    Pattern: upload<ConfigurationName>: Assembles and uploads the artifacts belonging to a configuration.
    Pattern: clean<TaskName>: Cleans the output files of a task.
    
    BUILD SUCCESSFUL
    

相关问题