【问题标题】:Get IT test in Sonar trough maven在 Sonar trough maven 中进行 IT 测试
【发布时间】:2018-05-10 14:57:15
【问题描述】:

再见, 我正在尝试让我的 IT 部门参与到声纳的测试范围中。我有一个多模块项目,我希望它适用于所有模块。就我找到的文档而言:

但是当我运行它时,测试似乎有 0%,所以很明显我做错了什么。在我确实出错的地方加上一些会很好。

我的主要 Pom:

      <properties>
        <maven-failsafe-plugin.version>2.20.1</maven-failsafe-plugin.version>
        <maven-surefire-plugin.version>2.20.1</maven-surefire-plugin.version>
        <sonar.java.coveragePlugin>jacoco</sonar.java.coveragePlugin>
        <sonar.dynamicAnalysis>reuseReports</sonar.dynamicAnalysis>             
        <sonar.jacoco.reportPaths>${project.basedir}/../target/</sonar.jacoco.reportPaths>
        <argLine>-Xmx256m -XX:MaxPermSize=200m</argLine>
      </properties>
        ...
<build>
<plugins>
  <plugin>
    <groupId>org.jacoco</groupId>
    <artifactId>jacoco-maven-plugin</artifactId>
    <version>${jacoco-maven-plugin.version}</version>
    <configuration>
      <append>true</append>
    </configuration>
    <executions>
      <execution>
        <id>agent-for-ut</id>
        <goals>
          <goal>prepare-agent</goal>
        </goals>
        <configuration>
          <destFile>${project.basedir}/../target/jacoco-ut.exec</destFile>
        </configuration>
      </execution>
      <execution>
        <id>agent-for-it</id>
        <goals>
          <goal>prepare-agent-integration</goal>
        </goals>
        <configuration>
          <destFile>${project.basedir}/../target/jacoco-it.exec</destFile>
        </configuration>
      </execution>
      <execution>
        <id>jacoco-site</id>
        <phase>verify</phase>
        <goals>
          <goal>report</goal>
        </goals>
      </execution>
    </executions>
  </plugin>
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-failsafe-plugin</artifactId>
    <version>${maven-failsafe-plugin.version}</version>
  </plugin>
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>${maven-surefire-plugin.version}</version>
    <configuration>
      <trimStackTrace>false</trimStackTrace>
    </configuration>
  </plugin>
</plugins>
</build>

我整天都在搞乱我的感觉,并没有找到明确的答案我做错了什么。所以一些输入会很有帮助

【问题讨论】:

    标签: java maven sonarqube code-coverage jacoco


    【解决方案1】:

    单元测试和集成测试的覆盖率有点脆弱......

    您的配置看起来不错。我认为可能发生的情况是“argLine”属性被替换或没有为surefire或故障安全插件正确设置。如果您使用 -X 运行 mvn 目标,请仔细查看故障安全启动时会发生什么,其值是多少。 argLine 应该包含 jacoco 代理以收集覆盖信息。

    另一件事:故障安全可能会将覆盖结果写入与 surefire 相同的 jacoco.exec 文件中。

    我所做的(不确定它是否是所有方法中最聪明的):将所有内容放在配置文件中,并为 jacoco-plugin 使用自定义属性,并为覆盖范围使用单独的文件,以便声纳报告可以获取它们:

    仅当您想知道哪些测试涵盖哪些生产代码时才需要 sonar-jacoco-listeners。在声纳中,这会显示在绿色栏中,测试称为代码。

    除此之外:

    pom 有一些属性:

        <surefire.jvm.args></surefire.jvm.args>
        <failsafe.jvm.args></failsafe.jvm.args>
        <jacoco.append>true</jacoco.append>
    </properties>
    

    如果需要,可以设置这些,并且配置使用自己的属性以不与 argLine 冲突(这是万能和故障安全的默认设置)

    sonar.jacoco.reportPath 可用于为所有 maven 模块编写一个文件,以防某些集成测试在不同的模块中,并且您也想测量覆盖率(代码方面不太好,但很好...现实和东西):

    <sonar.jacoco.itReportPath>${project.basedir}/../target/jacoco-it.exec</sonar.jacoco.itReportPath>
    

    这是我的覆盖率简介:(采用包括:下面的 my/packages/* 模式!)

     <profile>
            <dependencies>
                <dependency>
                    <groupId>org.codehaus.sonar-plugins.java</groupId>
                    <artifactId>sonar-jacoco-listeners</artifactId>
                    <version>3.2</version>
                    <scope>test</scope>
                </dependency>
            </dependencies>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.jacoco</groupId>
                        <artifactId>jacoco-maven-plugin</artifactId>
                        <version>${jacoco.version}</version>
                        <executions>
                            <!-- prepare configuration for surefire tests -->
                            <execution>
                                <id>prepare-agent</id>
                                <phase>initialize</phase>
                                <goals>
                                    <goal>prepare-agent</goal>
                                </goals>
                                <configuration>
                                    <destFile>${sonar.jacoco.reportPath}</destFile>
                                    <propertyName>jacoco.agent.argLine</propertyName>
                                    <append>true</append>
                                </configuration>
                            </execution>
                            <!-- prepare configuration for failsafe integration tests -->
                            <execution>
                                <id>prepare-agent-integration</id>
                                <phase>pre-integration-test</phase>
                                <goals>
                                    <goal>prepare-agent-integration</goal>
                                </goals>
                                <configuration>
                                    <destFile>${sonar.jacoco.itReportPath}</destFile>
                                    <propertyName>jacoco.agent.it.argLine</propertyName>
                                    <append>true</append>
                                </configuration>
                            </execution>
                        </executions>
                        <configuration>
                            <includes>
                                <include>my/packages/*</include>
                            </includes>
                        </configuration>
                    </plugin>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-surefire-plugin</artifactId>
                        <configuration>
                            <properties>
                                <property>
                                    <name>listener</name>
                                    <value>org.sonar.java.jacoco.JUnitListener</value>
                                </property>
                            </properties>
                            <argLine>${jacoco.agent.argLine} ${surefire.jvm.args}</argLine>
                        </configuration>
                    </plugin>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-failsafe-plugin</artifactId>
                        <configuration>
                            <argLine>${jacoco.agent.it.argLine} ${failsafe.jvm.args}</argLine>
                        </configuration>
                    </plugin>
                </plugins>
            </build>
        </profile>
    

    所以原理是一样的,在正确的阶段设置 jacoco 代理并运行测试。我假设 jacoco 代理设置不正确,或者您的 argLine 与构建期间发生的某些事情发生冲突。

    【讨论】:

    • 今天晚些时候我会研究这个。如果您认为它很容易修复。
    猜你喜欢
    • 2014-10-25
    • 2012-09-07
    • 2017-06-05
    • 2016-09-25
    • 2013-03-16
    • 1970-01-01
    • 2013-05-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多