JaCoCo 测试在 pom.xml 的 jar 文件上运行

发布于 2025-01-16 14:35:17 字数 3600 浏览 3 评论 0原文

我正在尝试获取与我获得 jar 文件的开源项目相关的一些测试用例的覆盖范围。

我使用 Maven 作为自动构建工具和 jacoco 插件来获取项目测试的覆盖范围。

我想要实现的是使用以下命令可以获得的相同输出:

-jar jacococli.jar report jacoco.exec \
                       --classfiles <project.jar> \
                       --sourcefiles <myTestsDirectory> \
                       --html <jacocoReportsDirectory> \
                       --xml <jacocoReportsDirectory>coverageFile.xml \
                       --csv <jacocoReportsDirectory>coverageFile.csv

当我从命令行运行此命令时,我可以检索针对整个项目(jar)计算的覆盖率的报告。

我不知道如何在 pom.xml 中插入这个命令(或者可以得到相同输出的命令)。我试图将它放入 Surefire 插件 argLine (使用 -javaagent 命令),但这让我无处可去。

这就是我到目前为止所得到的:

<build>
        <sourceDirectory>src</sourceDirectory>
        <testSourceDirectory>src/test</testSourceDirectory>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
                <version>0.8.7</version>

                <executions>
                    <execution>
                        <id>prepare-agent</id>
                        <goals>
                            <goal>prepare-agent</goal>
                       </goals>
                   </execution>
                   <execution>
                       <id>report</id>
                       <phase>prepare-package</phase>
                       <goals>
                           <goal>report</goal>
                       </goals>
                   </execution>
                   <execution>
                       <id>post-unit-test</id>
                       <phase>test</phase>
                       <goals>
                           <goal>report</goal>
                       </goals>
                       <configuration>
                           <dataFile>target/coverage-reports/jacoco.exec</dataFile>
                           <outputDirectory>target/coverage-reports/jacoco-reports</outputDirectory>
                     </configuration>
                   </execution>
               </executions>
           </plugin>
           <plugin>
               <groupId>org.apache.maven.plugins</groupId>
               <artifactId>maven-surefire-plugin</artifactId>
               <version>3.0.0-M4</version>
               <configuration>
                   <argLine>
                       -javaagent:lib/jacocoagent.jar=destfile=target/coverage-reports/jacoco.exec
                   </argLine>
                   <reportsDirectory>target/coverage-reports/surefire-reports</reportsDirectory>
               </configuration>
           </plugin>
       </plugins>
   </build>

将整个 src 目录作为 sourceDirectory 获取我的测试用例作为覆盖率计算的目标,但我不希望这样。

注意:src 中没有测试所针对的 java 类,因此无需将其作为 sourceDirectory。我认为这必须以某种方式指向罐子。

I am trying to get the coverage of a few test cases related to an open source project of which I got the jar file.

I'm using maven as automatic build tool and the jacoco plugin for maven to get the coverage of the tests on the project.

What I would like to achieve is the same output that I can get with the command:

-jar jacococli.jar report jacoco.exec \
                       --classfiles <project.jar> \
                       --sourcefiles <myTestsDirectory> \
                       --html <jacocoReportsDirectory> \
                       --xml <jacocoReportsDirectory>coverageFile.xml \
                       --csv <jacocoReportsDirectory>coverageFile.csv

When I run this from the commandline I can retrieve the report for the coverage computed on the whole project (the jar).

I don't know how to insert this command (or something that can get me the same output) in the pom. I tried to put it in the surefire plugin argLine (with the -javaagent command) but this got me nowhere.

This is where I got so far:

<build>
        <sourceDirectory>src</sourceDirectory>
        <testSourceDirectory>src/test</testSourceDirectory>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
                <version>0.8.7</version>

                <executions>
                    <execution>
                        <id>prepare-agent</id>
                        <goals>
                            <goal>prepare-agent</goal>
                       </goals>
                   </execution>
                   <execution>
                       <id>report</id>
                       <phase>prepare-package</phase>
                       <goals>
                           <goal>report</goal>
                       </goals>
                   </execution>
                   <execution>
                       <id>post-unit-test</id>
                       <phase>test</phase>
                       <goals>
                           <goal>report</goal>
                       </goals>
                       <configuration>
                           <dataFile>target/coverage-reports/jacoco.exec</dataFile>
                           <outputDirectory>target/coverage-reports/jacoco-reports</outputDirectory>
                     </configuration>
                   </execution>
               </executions>
           </plugin>
           <plugin>
               <groupId>org.apache.maven.plugins</groupId>
               <artifactId>maven-surefire-plugin</artifactId>
               <version>3.0.0-M4</version>
               <configuration>
                   <argLine>
                       -javaagent:lib/jacocoagent.jar=destfile=target/coverage-reports/jacoco.exec
                   </argLine>
                   <reportsDirectory>target/coverage-reports/surefire-reports</reportsDirectory>
               </configuration>
           </plugin>
       </plugins>
   </build>

Giving the whole src directory as the sourceDirectory gets my test cases as the target of the coverage computation and I don't want that.

Note: there are no java classes in src to which the tests are targeted, so there's no need to put that as sourceDirectory. I assume this has to point to the jar somehow.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文