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

时间:2019-09-05 09:57:20

标签: 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命令时,它不会生成任何代码覆盖率报告。知道我在这里做错了吗?

1 个答案:

答案 0 :(得分:0)

  

当我执行mvn测试命令时

run的配置目标maven-antrun-plugin中,绑定到post-integration-test阶段的test阶段,因此没有被命令mvn test执行-请参见https://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html