【问题标题】:Jacoco Maven offline instrumentation - TomcatJacoco Maven 离线检测 - Tomcat
【发布时间】:2018-03-04 07:17:59
【问题描述】:

我正在尝试获取集成测试的代码覆盖率报告。 Jacoco maven 插件能够为单元测试提供代码覆盖率,但为集成测试提供 0% 的覆盖率。集成测试正在打应用程序的其余 api 端点,已部署在 tomcat 中。

我的 maven jacoco 插件和 surefire 插件看起来像这样。

<plugin>
            <groupId>org.jacoco</groupId>
            <artifactId>jacoco-maven-plugin</artifactId>
            <version>0.7.9</version>
            <executions>
                <execution>
                    <id>prepare-agent</id>
                    <goals>
                        <goal>prepare-agent</goal>
                    </goals>
                </execution>
                <execution>
                    <id>prepare-agent-integration</id>
                    <goals>
                        <goal>prepare-agent-integration</goal>
                    </goals>
                </execution>
                <execution>
                    <id>report</id>
                    <phase>prepare-package</phase>
                    <goals>
                        <goal>report</goal>
                    </goals>
                </execution>
                <execution>
                    <id>post-unit-test</id>
                    <phase>integration-test</phase>
                    <goals>
                        <goal>report</goal>
                    </goals>
                    <configuration>
                        <dataFile>${project.build.directory}/target/jacoco-it.exec</dataFile>
                        <outputDirectory>${project.build.directory}/target/jacoco-ut</outputDirectory>
                    </configuration>
                </execution>
            </executions>

            <configuration>
                <systemPropertyVariables>
                    <jacoco-agent.destfile>target/jacoco.exec</jacoco-agent.destfile>
                </systemPropertyVariables>
            </configuration>
        </plugin>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.20</version>
            <configuration>
                <!-- <skip>true</skip> -->
                <!-- <systemPropertyVariables> <jacoco-agent.destfile>target/jacoco.exec</jacoco-agent.destfile> 
                    </systemPropertyVariables> -->
            </configuration>
            <!-- <configuration> <skip>true</skip> </configuration> -->
            <executions>
                <execution>
                    <id>unit-tests</id>
                    <phase>test</phase>
                    <goals>
                        <goal>test</goal>
                    </goals>
                    <configuration>
                        <!-- Never skip running the tests when the test phase is invoked -->
                        <!-- <skip>true</skip> -->
                        <argLine>@{argLine}
                            -javaagent:c:\\iat\\mavenrepository\\org\\jacoco\\org.jacoco.agent\\0.7.10-SNAPSHOT\\org.jacoco.agent-0.7.10-SNAPSHOT-runtime.jar=destfile=C:\\Users\\dmahapat\\Workspaces\\MyEclipse
                            2016 CI\\JaxRsApp\\target\\jacoco.exec</argLine>
                        <includes>
                            <include>**/*UnitTest.java</include>
                        </includes>
                        <excludes>
                            <exclude>**/*IntegrationTest.java</exclude>
                        </excludes>
                    </configuration>
                </execution>
                <execution>
                    <id>integration-tests</id>
                    <phase>integration-test</phase>
                    <goals>
                        <goal>test</goal>
                    </goals>
                    <configuration>
                        <!-- Never skip running the tests when the integration-test phase 
                            is invoked -->
                        <!-- argLine>-javaagent:$WORKSPACE/target/lib/jacoco-agent-0.7.9.jar=includes=*,destfile=*/jacoco-coverage.exec,append=false</argLine -->
                        <skip>false</skip>
                        <argLine>@{argLine}
                            -javaagent:c:\\iat\\mavenrepository\\org\\jacoco\\org.jacoco.agent\\0.7.10-SNAPSHOT\\org.jacoco.agent-0.7.10-SNAPSHOT-runtime.jar=destfile=C:\\Users\\dmahapat\\Workspaces\\MyEclipse
                            2016 CI\\JaxRsApp\\target\\jacoco-it.exec
                        </argLine>

                        <includes>
                            <include>**/*IntegrationTest.java</include>
                        </includes>
                        <excludes>
                            <exclude>**/*UnitTest.java</exclude>
                        </excludes>
                    </configuration>
                </execution>
            </executions>
        </plugin>

我在测试阶段执行单元测试,在集成测试阶段执行集成测试。 我得到的最新错误是“由于缺少执行数据文件而跳过 JaCoCo 执行。”

【问题讨论】:

    标签: java tomcat integration-testing jacoco jacoco-maven-plugin


    【解决方案1】:

    在 Evgeny 的帮助下,我完成了这项工作。 将服务器更改为 glassfish 并将 ide 更改为 intellij 以便于调试。

    使用以下 JVM 选项启动 glassfish 服务器。

    -DargLine=-javaagent:c:/iat/mavenrepository/org/jacoco/org.jacoco.agent/0.7.9/org.jacoco.agent-0.7.9-runtime.jar=destfile=C:/dmahapat_JaxRsApp/target/coverage-reports/jacoco-it.exec
    

    更新 pom

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    
    <modelVersion>4.0.0</modelVersion>
    
    <groupId>org.mathworks</groupId>
    <artifactId>JaxRsApp</artifactId>
    <packaging>war</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <name>JaxRsApp</name>
    
    <build>
        <finalName>JaxRsApp</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.5.1</version>
                <inherited>true</inherited>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>build-helper-maven-plugin</artifactId>
                <version>3.0.0</version>
                <executions>
                    <execution>
                        <id>add-test-source</id>
                        <goals>
                            <goal>add-test-source</goal>
                        </goals>
                        <configuration>
                            <sources>
                                <source>${project.basedir}\src\integration-test\java</source>
                            </sources>
                        </configuration>
                    </execution>
                    <execution>
                        <id>add-test-resource</id>
                        <goals>
                            <goal>add-test-resource</goal>
                        </goals>
                        <configuration>
                            <resources>
                                <resource>
                                    <!-- Don't forget <directory> label -->
                                    <directory>${project.basedir}\src\integration-test\resources</directory>
                                </resource>
                            </resources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
            <groupId>org.jacoco</groupId>
            <artifactId>jacoco-maven-plugin</artifactId>
            <version>0.7.9</version>
                <executions>
                    <!--
                        Prepares the property pointing to the JaCoCo runtime agent which
                        is passed as VM argument when Maven the Surefire plugin is executed.
                    -->
                    <execution>
                        <id>pre-unit-test</id>
                        <goals>
                            <goal>prepare-agent</goal>
                        </goals>
                        <configuration>
                            <!-- Sets the path to the file which contains the execution data. -->
                            <destFile>${project.build.directory}/coverage-reports/jacoco-ut.exec</destFile>
                            <!--
                                Sets the name of the property containing the settings
                                for JaCoCo runtime agent.
                            -->
                            <propertyName>surefireArgLine</propertyName>
                        </configuration>
                    </execution>
                    <!--
                        Ensures that the code coverage report for unit tests is created after
                        unit tests have been run.
                    -->
                    <execution>
                        <id>post-unit-test</id>
                        <phase>test</phase>
                        <goals>
                            <goal>report</goal>
                        </goals>
                        <configuration>
                            <!-- Sets the path to the file which contains the execution data. -->
                            <dataFile>${project.build.directory}/coverage-reports/jacoco-ut.exec</dataFile>
                            <!-- Sets the output directory for the code coverage report. -->
                            <outputDirectory>${project.reporting.outputDirectory}/jacoco-ut</outputDirectory>
                        </configuration>
                    </execution>
    
                    <execution>
                        <id>pre-integration-test</id>
                        <phase>pre-integration-test</phase>
                        <goals>
                            <goal>prepare-agent</goal>
                        </goals>
                        <configuration>
                            <!-- Sets the path to the file which contains the execution data. -->
                            <destFile>${project.build.directory}/coverage-reports/jacoco-it.exec</destFile>
                            <!--
                                Sets the name of the property containing the settings
                                for JaCoCo runtime agent.
                            -->
                            <propertyName>failsafeArgLine</propertyName>
                        </configuration>
                    </execution>
                    <!--
                        Ensures that the code coverage report for integration tests after
                        integration tests have been run.
                    -->
                    <execution>
                        <id>post-integration-test</id>
                        <phase>post-integration-test</phase>
                        <goals>
                            <goal>report</goal>
                        </goals>
                        <configuration>
                            <!-- Sets the path to the file which contains the execution data. -->
                            <dataFile>${project.build.directory}/coverage-reports/jacoco-it.exec</dataFile>
                            <!-- Sets the output directory for the code coverage report. -->
                            <outputDirectory>${project.reporting.outputDirectory}/jacoco-it</outputDirectory>
                        </configuration>
                    </execution>
                </executions>
    
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.20.1</version>
                <configuration>
                   <argLine>${surefireArgLine}</argLine>
                    <excludes>
                        <exclude>**/*IntegrationTest*</exclude>
                    </excludes>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-failsafe-plugin</artifactId>
                <version>2.20.1</version>
                <configuration>
                  <argLine>${failsafeArgLine}</argLine>
                    <includes>
                        <include>**/*IntegrationTest.java</include>
                    </includes>
                    <excludes>
                        <exclude>**/*UnitTest.java</exclude>
                    </excludes>
                </configuration>
                <executions>
                    <execution>
                        <id>integration-tests</id>
                        <phase>integration-test</phase>
                        <goals>
                            <goal>integration-test</goal>
                            <goal>verify</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
    
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.glassfish.jersey</groupId>
                <artifactId>jersey-bom</artifactId>
                <version>${jersey.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    
    <dependencies>
        <dependency>
            <groupId>org.glassfish.jersey.containers</groupId>
            <artifactId>jersey-container-servlet-core</artifactId>
            <!-- use the following artifactId if you don't need servlet 2.x compatibility -->
            <!-- artifactId>jersey-container-servlet</artifactId -->
        </dependency>
    
        <dependency>
            <groupId>org.glassfish.jersey.media</groupId>
            <artifactId>jersey-media-moxy</artifactId>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.3.5</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpmime -->
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpmime</artifactId>
            <version>4.5.3</version>
        </dependency>
    
        <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.8.7</version>
        </dependency>
    </dependencies>
    <properties>
        <jersey.version>2.25.1</jersey.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    
    </properties>
    

    【讨论】:

      【解决方案2】:

      引用documentation of prepare-agent-integration:

      与 prepare-agent 相同,但提供适用于集成测试的默认值:

      • 绑定到预集成测试阶段
      • 不同的目标文件

      引用documentation of prepare-agent:

      准备一个指向 JaCoCo 运行时代理的属性,该属性可以作为 VM 参数传递给被测应用程序。默认情况下,根据项目打包类型设置具有以下名称的属性:

      • tycho.testArgLine 用于封装类型 eclipse-test-plugin 和
      • argLine 否则。

      在大多数情况下,maven-surefire-plugin 会自动选择 argLine 来启动 JVM 执行单元测试 - http://maven.apache.org/surefire/maven-surefire-plugin/test-mojo.html#argLine

      argLine:要在命令行上设置的任意 JVM 选项。

      这解释了为什么您会获得单元测试的覆盖率。

      要获得集成测试的覆盖率,您必须确保将此属性传递给被测应用程序的 JVM,即执行 Tomcat 的 JVM,这完全取决于您启动它的方式。

      【讨论】:

      • 我是否需要通过提供以下参数来启动tomcat -javaagent:c:\\iat\\mavenrepository\\org\\jacoco\\org.jacoco.agent\\0.7.10-SNAPSHOT\ \org.jacoco.agent-0.7.10-SNAPSHOT-runtime.jar=destfile=C:\\Users\\dmahapat\\Workspaces\\MyEclipse2016 CI\\JaxRsApp\\target\\jacoco-it.exec
      • 如果您使用 Maven 启动 Tomcat,那么 jacoco-maven-plugin 会为您准备此参数,因此您只需将其作为通常的 Maven 属性 - ${argLine} 传递给 Tomcat。
      • 嗨 Govin,我们切换到 glassfish 4.1.0 并添加 arg 行作为 domain.xml 中的 jvm-options 格式 -DargLine=-javaagent:c:/iat/mavenrepository /org/jacoco/org.jacoco.agent/0.7.9/org.jacoco.agent-0.7.9-runtime.jar=destfile=C:/Users/dmahapat/Workspaces/MyEclipse 2016 CI/dmahapat_JaxRsApp 2/target/jacoco -it.exec 但是每次我们尝试重新启动 glassfish 时,jvm 选项都会被删除,我们仍然会看到相同的错误“由于缺少执行数据文件而跳过 JaCoCo 执行”。我可以和你分享一下最新的 pom 吗?
      • 看起来,我收到了这个错误,因为路径中有空格。我将其更改为 -DargLine=-javaagent:c:/iat/mavenrepository/org/jacoco/org.jacoco.agent/0.7.9/org.jacoco.agent-0.7.9-runtime.jar= destfile=C:\Users\dmahapat\Workspaces\MyEcli~1\dmahap~1\target\jacoco-it.exec 并重新启动 glassfish。现在我仍然在报告中得到 0% 的覆盖率。 “由于缺少执行数据文件,跳过 JaCoCo 执行。”错误仍然存​​在
      猜你喜欢
      • 2017-05-13
      • 1970-01-01
      • 2020-10-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-27
      • 2015-09-07
      • 2017-06-05
      相关资源
      最近更新 更多