从Hudson / Jenkins上传JUnit XML到Sonar

时间:2015-03-07 17:53:56

标签: java maven junit jenkins hudson

作为一名.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中遗漏了一些明显的东西。如果您需要任何其他信息,请告知我们。

3 个答案:

答案 0 :(得分:0)

我通常使用这三个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>

请参阅Integration测试必须以... IntegrationTest和其他测试结束...测试。

在此示例中,使用了一些可能有用的配置文件。您可以删除然后调整上述代码。

<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>

答案 1 :(得分:0)

项目的packaging元素设置为pom。完成此操作后,maven将不会运行除install之外的任何目标(并且可能sonar也不会)。将包装设置为jar后再次尝试怎么样?

答案 2 :(得分:0)

您不必在pom.xml中进行任何更改。 Sonar解释了JUnit报告。 在Jenkins中你需要安装声纳插件并添加声纳作为Build Step(从组合框中选择)。 接下来你需要添加post build action - 发布JUnit测试结果报告并添加表达式。

我使用failafe插件进行int测试,因此表达式为:

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

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

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

表达式的分隔符是空格。

希望这会有所帮助。