当前位置: 首页 > 知识库问答 >
问题:

无法从jacoco netbeans获得任何代码覆盖率

岳英锐
2023-03-14

我正在做一个大学项目,我需要覆盖当地法规,看看我的测试是否做得好。然而,netbeans控制台保持

--- jaco-maven-plugin:0.7.7.201606060606:报告(报告) @ 客户端服务提供器---由于缺少执行数据文件而跳过 JaCoCo 执行。

这是我的pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <!--
    In this file, DO NOT EDIT any of following elements
    -->
    <groupId>com.mycompany</groupId>
    <artifactId>ClientServicesProvider</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <mainClass>com.mycompany.tp3.MainApp</mainClass>
    </properties>

    <dependencies>
        <!-- Students can only add new dependencies to this section -->

        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>5.3.2</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>5.3.2</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.xmlunit</groupId>
            <artifactId>xmlunit-core</artifactId>
            <version>2.6.2</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.xmlunit</groupId>
            <artifactId>xmlunit-matchers</artifactId>
            <version>2.6.2</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.hamcrest</groupId>
            <artifactId>hamcrest-core</artifactId>
            <version>1.3</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.jfoenix</groupId>
            <artifactId>jfoenix</artifactId>
            <version>8.0.8</version>
        </dependency>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <scope>test</scope>
            <version>2.44.0</version>
        </dependency>
        <dependency>
            <groupId>com.opera</groupId>
            <artifactId>operadriver</artifactId>
            <scope>test</scope>
            <version>1.5</version>
            <exclusions>
                <exclusion>
                    <groupId>org.seleniumhq.selenium</groupId>
                    <artifactId>selenium-remote-driver</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <!-- Required for compiling the project usign maven -->
            <plugin><!-- Compiler configuration-->
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.6.0</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>

                    <showWarnings>true</showWarnings>   <!-- Needs this -->



                    <!-- <encoding>${project.build.sourceEncoding}</encoding> -->

                </configuration>
            </plugin>



            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>1.2.1</version>
                <executions>
                    <execution>
                        <id>unpack-dependencies</id>

                        <phase>package</phase>
                        <goals>
                            <goal>exec</goal>
                        </goals>
                        <configuration>
                            <executable>${java.home}/../bin/javafxpackager</executable>
                            <arguments>
                                <argument>-createjar</argument>
                                <argument>-nocss2bin</argument>
                                <argument>-appclass</argument>
                                <argument>${mainClass}</argument>
                                <argument>-srcdir</argument>
                                <argument>${project.build.directory}/classes</argument>
                                <argument>-outdir</argument>
                                <argument>${project.build.directory}</argument>
                                <argument>-outfile</argument>
                                <argument>${project.build.finalName}.jar</argument>
                            </arguments>
                        </configuration>
                    </execution>
                    <execution>
                        <id>default-cli</id>
                        <goals>
                            <goal>exec</goal>                            
                        </goals>
                        <configuration>
                            <executable>${java.home}/bin/java</executable>
                            <commandlineArgs>${runfx.args}</commandlineArgs>
                        </configuration>
                    </execution>
                </executions>  
            </plugin>

            <!-- Required for running unit tests -->
            <plugin>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.22.1</version>
                <configuration>
                    <argLine>-Dfile.encoding=UTF-8</argLine>
                    <!-- new configuration needed for coverage per test -->
                    <properties>
                        <property>
                            <name>listener</name>
                            <value>org.sonar.java.jacoco.JUnitListener</value>
                        </property>
                    </properties>

                </configuration>
                <dependencies>
                    <!-- This dependency must be included, otherwise Maven Surefire will not recognise the test cases -->
                    <dependency>
                        <groupId>org.junit.platform</groupId>
                        <artifactId>junit-platform-surefire-provider</artifactId>
                        <version>1.2.0</version>
                    </dependency>
                </dependencies>

            </plugin>

            <!-- Required for generating coverage report -->
            <plugin>
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
                <version>0.7.7.201606060606</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>prepare-agent</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>report</id>
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>report</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>


            <!-- Required for generating PIT Mutation reports -->
            <plugin>
                <groupId>org.pitest</groupId>
                <artifactId>pitest-maven</artifactId>
                <version>1.4.3</version>
                <dependencies>
                    <dependency> <!-- Only required because PITest does not work with JUnit5 without it -->
                        <groupId>org.pitest</groupId>
                        <artifactId>pitest-junit5-plugin</artifactId>
                        <version>0.8</version>
                    </dependency>
                </dependencies>
                <configuration>
                    <!--<timestampedReports>false</timestampedReports>-->
                    <!--<inScopeClasses>
                        <param>com.mycompany.*</param>
                        <param>lapr.project.model.*</param>
                        <param>lapr.project.utils.*</param>
                    </inScopeClasses>-->
                    <targetClasses>
                        <param>com.mycompany.criagajos3000.*</param>
                        <param>com.mycompany.tp3.*</param>
                        <param>lapr.project.autorizacao.*</param>
                        <param>lapr.project.gpsd.controller.*</param>
                        <param>lapr.project.gpsd.model.*</param>

                    </targetClasses>
                    <targetTests>
                        <param>com.mycompany.criagajos3000.*</param>
                        <param>com.mycompany.tp3.*</param>
                        <param>lapr.project.autorizacao.*</param>
                        <param>lapr.project.gpsd.controller.*</param>
                        <param>lapr.project.gpsd.model.*</param>
                    </targetTests>
                    <outputFormats>
                        <outputFormat>XML</outputFormat>
                        <outputFormat>HTML</outputFormat>
                    </outputFormats>
                    <!--<verbose>true</verbose>-->
                </configuration>
            </plugin>

            <!-- Build an executable JAR -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>3.1.0</version>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <classpathPrefix>lib/</classpathPrefix>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

共有1个答案

庄文栋
2023-03-14

您的pom.xml包含

                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.22.1</version>
                <configuration>
                    <argLine>-Dfile.encoding=UTF-8</argLine>

然而,引用JaCoCo在http://www.jacoco.org/jacoco/trunk/doc/prepare-agent-mojo.html留档:

如果您的项目已经为测试执行定义了VM参数,请确保它们将包含由JaCoCo定义的属性。

在maven-surefire插件的情况下,执行此操作的方法之一是使用语法进行后期属性评估:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-surefire-plugin</artifactId>
  <configuration>
    <argLine>@{argLine} -your -extra -arguments</argLine>
  </configuration>
</plugin>

另一种方法是将“argLine”定义为Maven属性,而不是maven-surefire-plugin配置的一部分:

<properties>
  <argLine>-your -extra -arguments</argLine>
</properties>
...
<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-surefire-plugin</artifactId>
  <configuration>
    <!-- no argLine here -->
  </configuration>
</plugin>

因此,将< code>argLine定义为属性:

    <build>
      <properties>
        <argLine>-Dfile.encoding=UTF-8</argLine>
      </properties>
      ...
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.22.1</version>
        <configuration>
          <!-- no argLine here -->

与您的问题无关,但我也建议使用最新的JaCoCo版本,即今天的0.8.4,而不是3年前的版本0.7.7

 类似资料:
  • 我正在使用Jacoco插件使用jenkins获得代码覆盖率。我能够看到代码覆盖率。当我尝试使用相同的jacoco.exec文件来生成声纳代码覆盖率时,它显示:由于没有类文件,无法对项目覆盖率进行JaCoCo分析。我使用以下声纳属性: 我正在使用SonarQube 5.3

  • 在maven构建过程中,我使用启动我的应用程序,并使用故障安全运行集成测试。现在我想从测试中的应用程序获取覆盖数据。 问题是,覆盖率报告包含我的应用程序的所有类,但没有覆盖率数据(全部为0%)。当我将jacoco代理附加到failsafe插件时,它会生成测试类的代码覆盖率,这并没有真正的帮助。 有什么想法吗? process exec maven插件配置: 使用以下Jacoco配置:

  • 当我的单元测试通过Emma被“覆盖”时,我试图让Sonar IT代码覆盖为我工作。 我的情况: (1)我有大量使用JMockit的单元测试。删除JMockit不是一个选项。我想获得这些测试的单元测试覆盖报告。 (2)我进行了集成测试,简单地用不同的输入场景运行应用程序的核心服务器端部分(这是一个Spring web应用程序)。我想要它的代码覆盖报告。 对于第(1)部分,我选择使用Emma进行单元测

  • 代码覆盖是查找未被测试执行的代码区域的过程。不过要记住的是这并不能说明你测试代码的有效性。 在requirements.txt文件中添加依赖包: coverage==4.4.2 然后,我们在manage.py中新增一个命令: import coverage COV = coverage.coverage( branch=True, include='project/*',

  • [在这里输入图像描述][1][在这里输入图像描述][2]我知道很多人都有类似的问题。我展示了很多答案,尝试了在声纳网站上给出的示例代码。那个样品工作得很好。我还显示以下链接 maven 3.0.5 请帮我解决这个问题。 下面是父模块pom文件 我真的觉得这是jacoco或Sonarqube的bug。可能它不兼容Java8之类的东西。我几乎什么都试过了。sonar java插件2.5.1不推荐使用许

  • 问题是当我们开发顶点代码,然后我们编写测试类,它至少覆盖顶点类75%的代码覆盖率,现在当我登录到开发人员控制台时,我能够看到代码覆盖率很少,因为手动需要去开发人员控制台,我想要一些可以向高级经理展示的关于整个组织的Salesforce Test代码覆盖率的报告 但是有没有可能在一个非常类似于sonarqube代码覆盖率报告的复杂报告中得到这个 提前谢谢卡洛琳