java - 配置 Jacoco 进行集成测试代码覆盖率

标签 java maven jacoco jacoco-maven-plugin

我正在尝试配置 Jacoco 代理以实现代码覆盖率,但它没有生成任何覆盖率。这是我当前的配置:

我有 2 个项目,我们称它们为项目 A 和项目 B。项目 A 是我想要为其生成覆盖率报告的地方。它在 Wildfly 上运行,我将 Jacoco Agent 配置为 vm 参数,如下所示:

-javaagent:/jacocoagent.jar=port=36320,destfile=jacoco-it.exec,output=tcpserver

项目 B 是一个 Maven 项目,我在其中执行测试用例。我像这样配置了 jacoco maven 插件:

      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-antrun-plugin</artifactId>
        <version>1.8</version>
        <executions>
          <execution>
            <id>generate-report</id>
            <phase>post-integration-test</phase>
            <goals>
              <goal>run</goal>
            </goals>
            <configuration>
              <skip>${skip.int.tests.report}</skip>
              <target>
                <!-- Execute an ant task within maven -->
                <echo message="Generating JaCoCo Reports"/>
                <taskdef name="report" classname="org.jacoco.ant.ReportTask">
                  <classpath path="${basedir}/target/jacoco-jars/org.jacoco.ant.jar"/>
                </taskdef>
                <mkdir dir="${basedir}/target/coverage-report"/>
                <report>
                  <executiondata>
                    <fileset dir="${basedir}">
                      <include name="target/jacoco-it*.exec"/>
                    </fileset>
                  </executiondata>
                  <structure name="jacoco-multi Coverage Project">
                    <group name="jacoco-multi">
                      <classfiles>
                        <fileset dir="target/classes"/>
                      </classfiles>
                      <sourcefiles encoding="UTF-8">
                        <fileset dir="src"/>
                      </sourcefiles>
                    </group>
                  </structure>
                  <html destdir="${basedir}/target/coverage-report/html"/>
                  <xml destfile="${basedir}/target/coverage-report/coverage-report.xml"/>
                  <csv destfile="${basedir}/target/coverage-report/coverage-report.csv"/>
                </report>
              </target>
            </configuration>
          </execution>
        </executions>
        <dependencies>
          <dependency>
            <groupId>org.jacoco</groupId>
            <artifactId>org.jacoco.ant</artifactId>
            <version>0.8.4</version>
          </dependency>
        </dependencies>
      </plugin>

我像这样配置了 Maven Surefire 插件:

      <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.14.1</version>
            <configuration>
           <!--    Suite testng xml file to consider for test execution -->
                <environmentVariables>
                <BLUEOPTIMA_HOME>${project.basedir}/</BLUEOPTIMA_HOME>
                </environmentVariables>
                <systemPropertyVariables>
                    <BLUEOPTIMA_HOME>${project.basedir}</BLUEOPTIMA_HOME>
                </systemPropertyVariables>
                <suiteXmlFiles>
                    <suiteXmlFile>${project.basedir}/testng.xml</suiteXmlFile>
                    <!--<suiteXmlFile>suites-test-testng.xml</suiteXmlFile> -->
                </suiteXmlFiles>
            </configuration>
        </plugin>

当我执行 mvn test 命令时,它没有生成任何代码覆盖率报告。知道我在这里做错了什么吗?

最佳答案

When I execute the mvn test command

在您的配置目标中,maven-antrun-pluginrun 绑定(bind)到 post-integration-test 阶段,该阶段位于 之后>test 阶段,因此不由命令 mvn test 执行 - 请参阅 https://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html

关于java - 配置 Jacoco 进行集成测试代码覆盖率,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57802925/

相关文章:

java - 从比赛中打印高于平均时间

java - 使用 jsoup 将 HTML 解析为格式化的纯文本

java - 在 Mac OS X Mavericks 上使用 Maven 插件启动 Google Appengine devserver 时出现延迟

android - JaCoCo gradle 插件排除

java - 如何解析 postman 集合?

java - 为什么我的大整数前面有一个减号?

eclipse - 我在项目属性中看不到项目方面。知道一定出了什么问题吗?

gradle - 如何在手动测试中生成jacoco覆盖率?

Android Studio : Could not find org. jacoco :org. jacoco.agent :org. gradle.testing.jacoco.plugins.JacocoPluginExtension_Decorated

java - 使用 Jackson 反序列化任意 JSON