【问题标题】:Upload JUnit XMLs to Sonar from Hudson / Jenkins从 Hudson / Jenkins 将 JUnit XML 上传到 Sonar
【发布时间】:2015-03-07 17:53:55
【问题描述】:

作为一名 .Net 开发人员,我对 pom.xml 的概念并不十分熟悉,但谷歌搜索了一下:

<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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>com.mycompany.my_dept</groupId>
    <artifactId>my_dept_parent</artifactId>
    <version>14.03.00.00-SNAPSHOT</version>
  </parent>
  <groupId>com.mycompany.my_dept.my_app</groupId>
  <artifactId>DB_UnitTests</artifactId>
  <version>15.01.00.50-SNAPSHOT</version>
  <packaging>pom</packaging>
  <name>DB_UnitTests</name>
  <properties>
    <sonar.junit.reportsPath>${basedir}\..\JUnit</sonar.junit.reportsPath>
  </properties>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.5</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

我一直在尝试将 JUnit 测试结果从我的 Hudson 工作区中的“JUnit”文件夹上传到我们的企业声纳实例。在 Hudson 处理完这个 POM 之后,我看到在声纳上创建了一个空白页面,其中有 0 个问题并且没有提及单元测试。查看构建控制台,没有提及 JUnit 或任何可能表明依赖项加载失败的内容。

我可能在 POM 中遗漏了一些明显的东西。如果您需要任何其他信息来回答这个问题,请告诉我。

【问题讨论】:

    标签: java maven junit jenkins hudson


    【解决方案1】:

    项目的packaging 元素设置为pom。完成此操作后,maven 将不会运行除 install 之外的任何目标(也许 sonar 也不会)。将包装设置为jar 后重试如何?

    【讨论】:

    • 不幸的是,这似乎有相同的结果。
    【解决方案2】:

    您不必对 pom.xml 进行任何更改。 Sonar 解释 JUnit 报告。 在 Jenkins 中,您需要安装声纳插件并将声纳添加为构建步骤(从组合框中选择)。 接下来是您需要添加构建后操作 - 发布 JUnit 测试结果报告并添加表达式。

    我使用故障安全插件进行 int 测试,所以表达式是:

    **/target/failsafe-reports/*.xml
    

    对于单元测试,我使用surefire插件,表达式为:

    **/target/surefire-reports/*.xml
    

    表达式的分隔符是空格。

    希望这会有所帮助。

    【讨论】:

      【解决方案3】:

      我通常使用这三个 maven 插件的组合:Surefire(单元测试)、Failsafe(集成测试)和 jacoco(报告创建)

      pom.xml 中的属性

          <properties>
              <sonar.surefire.reportsPath>target/surefire-reports</sonar.surefire.reportsPath>
              <sonar.jacoco.reportPath>target/jacoco.exec</sonar.jacoco.reportPath>
              <sonar.jacoco.outReportPath>target/jacoco</sonar.jacoco.outReportPath>
              <sonar.jacoco.itReportPath>target/jacoco-it.exec</sonar.jacoco.itReportPath>
              <sonar.jacoco.outItReportPath>target/jacoco-it</sonar.jacoco.outItReportPath>
          </properties>
      

      还有这三个插件

      <build>
           <plugins>
                    <plugin>
                          <groupId>org.apache.maven.plugins</groupId>
                          <artifactId>maven-surefire-plugin</artifactId>
                          <version>2.15</version>
                          <configuration>
                              <!-- Sets the VM argument line used when unit tests are run. -->
                              <argLine>${surefireArgLine}</argLine>
                              <!-- Skips unit tests if the value of skip.unit.tests property is true -->
                              <skipTests>${skip.unit.tests}</skipTests>
                              <!-- Excludes integration tests when unit tests are run. -->
                              <excludes>
                                  <exclude>**/*IntegrationTest.java</exclude>
                              </excludes>
                          </configuration>
                      </plugin>
      
                      <plugin>
                          <groupId>org.apache.maven.plugins</groupId>
                          <artifactId>maven-failsafe-plugin</artifactId>
                          <version>2.15</version>
                          <executions>
                              <!-- Ensures that both integration-test and verify goals of the Failsafe Maven plugin are executed. -->
                              <execution>
                                  <id>integration-tests</id>
                                  <goals>
                                      <goal>integration-test</goal>
                                      <goal>verify</goal>
                                  </goals>
                                  <configuration>
                                      <!-- Sets the VM argument line used when integration tests are run. -->
                                      <argLine>${failsafeArgLine}</argLine>
                                      <!-- Skips integration tests if the value of skip.integration.tests property is true -->
                                      <skipTests>${skip.integration.tests}</skipTests>
                                      <includes>
                                          <include>**/*IntegrationTest.java</include>
                                      </includes>
                                  </configuration>
                              </execution>
                          </executions>
                      </plugin>
                      <plugin>
                          <groupId>org.jacoco</groupId>
                          <artifactId>jacoco-maven-plugin</artifactId>
                          <version>0.7.0.201403182114 </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>${sonar.jacoco.reportPath}</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>${sonar.jacoco.reportPath}</dataFile>
                                      <!-- Sets the output directory for the code coverage report. -->
                                      <outputDirectory>${sonar.jacoco.outReportPath}</outputDirectory>
                                  </configuration>
                              </execution>
                              <!-- 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>${sonar.jacoco.itReportPath}</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>${sonar.jacoco.itReportPath}</dataFile>
                                      <!-- Sets the output directory for the code coverage report. -->
                                      <outputDirectory>${sonar.jacoco.outItReportPath}</outputDirectory>
                                  </configuration>
                              </execution>
                          </executions>
                      </plugin>
             </plugins>
      </build>
      

      请注意,集成测试必须以 ...IntegrationTest 结尾,而其他测试必须以 ...Test 结尾。

      在此示例中,使用了一些配置文件,它们可能有用。然后你可以删除并修改上面的代码。

      <properties>
          <!-- Only unit tests are run by default. -->
              <skip.integration.tests>true</skip.integration.tests>
              <skip.unit.tests>false</skip.unit.tests>
      </properties>
      
          <profiles>
              <profile>
                  <id>all-tests</id>
                  <properties>
                      <build.profile.id>all-tests</build.profile.id>
                      <!-- All tests are run. -->
                      <skip.integration.tests>false</skip.integration.tests>
                      <skip.unit.tests>false</skip.unit.tests>
                  </properties>
              </profile>
              <profile>
                  <id>dev</id>
              </profile>
              <profile>
                  <id>integration-tests</id>
                  <properties>
                      <!-- Used to locate the profile specific configuration file. -->
                      <build.profile.id>integration-test</build.profile.id>
                      <!-- Only integration tests are run. -->
                      <skip.integration.tests>false</skip.integration.tests>
                      <skip.unit.tests>true</skip.unit.tests>
                  </properties>
              </profile>
          </profiles>
      

      【讨论】:

      • 感谢您发布此内容,但我不使用任何这些测试运行程序。我已经有了你只想上传到声纳的junit xmls。
      • 好的。如果你没有得到答案,试试这个,也许它可以帮助你。
      猜你喜欢
      • 1970-01-01
      • 2017-10-03
      • 2013-01-08
      • 2012-07-10
      • 2012-10-27
      • 2016-07-24
      • 1970-01-01
      • 1970-01-01
      • 2014-11-01
      相关资源
      最近更新 更多