gpt4 book ai didi

maven - 如何衡量 RestFul API 的代码覆盖率

转载 作者:行者123 更新时间:2023-11-28 20:28:52 25 4
gpt4 key购买 nike

我有一个基于 Maven 的多模块 Jersey 项目,它公开了很少的 RestFul API。项目结构就像 -

项目

  • 模块 1

    ------------源

    --------单元测试

  • 模块 2

    ------------源

    --------单元测试

  • 模块 3

    ------------源

    --------单元测试

  • ModuleN - 此模块包含集成测试,它将命中项目公开的端点并像黑盒一样测试整个服务

    ------------源

    --------单元测试

我想构建这个项目并在构建阶段执行单元测试,然后创建一个 jar,将这个 jar 部署到某个地方,执行集成测试(它存在于项目的一个模块中,这将达到 REST 端点)然后我想要测量组合覆盖率(单元+集成测试)。

我浏览了很多博客和文章,但到处都是半信息。有人可以指点我或指导我该怎么做。

谢谢-沙希德

最佳答案

正如我回答的here:您可以将所有报告放在一个文件夹中(不要忘记调用不同的文件夹!)并为此使用合并 mojo,或者通过添加标志“附加”为所有报告使用一个中央唯一文件:

-javaagent:append=true,destFile=/home/YourProject/report.exec

[此处][2] 您将找到有关如何配置代理的更多信息。

希望对您有所帮助!

(这是我对同一问题的 other 回答的副本)

编辑:

只要您要求不同类型的执行(单元和集成),我假设您使用不同的插件来执行测试。

您需要做的是准备两个 JaCoCo 代理并将它们的 argLine 提供给将运行您的测试的插件:

        <plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.7.7.201606060606</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}/jacoco.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}/jacoco.exec</dataFile>
<!-- Sets the output directory for the code coverage report. -->
<outputDirectory>${project.reporting.outputDirectory}</outputDirectory>
</configuration>
</execution>
<!-- The Executions required by unit tests are omitted. -->
<!--
Prepares the property pointing to the JaCoCo runtime agent which
is passed as VM argument when Maven the Failsafe plugin is executed.
-->
<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}/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}/jacoco-it.exec</dataFile>
<!-- Sets the output directory for the code coverage report. -->
<outputDirectory>${project.reporting.outputDirectory}</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>

然后您必须配置您的插件(通常使用 failsafe 和 surefire)以接受为 JaCoCo 代理创建的 argLine:

            <plugin>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<argLine>${surefireArgLine}</argLine>
</configuration>
</plugin>

故障安全插件也是如此。

关于maven - 如何衡量 RestFul API 的代码覆盖率,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39120353/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com