【发布时间】:2014-07-03 12:34:03
【问题描述】:
我使用以下东西:
maven - 构建项目
jenkins - 用于 CI
声纳 - 用于代码分析
我的项目包含单元测试和集成测试。我已经配置了上述工具以协同工作。
集成测试与单元测试分开。要运行集成测试,我需要使用特定配置文件运行 maven。 Sonar 执行单元测试并收集指标。
如何强制 Sonar 执行特定配置文件并从集成测试中收集结果?
编辑
我的万无一失和故障安全部分:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.12</version>
<configuration>
<skipTests>${skip.unit.tests}</skipTests>
<excludes>
<exclude>**/*IT*.java</exclude>
</excludes>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.12</version>
<executions>
<execution>
<id>integration-tests</id>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
<configuration>
<skipTests>${skip.integration.tests}</skipTests>
</configuration>
</execution>
</executions>
</plugin>
编辑 2
当我使用 clean packag -DskipTests 运行 jenkins build 时,否则我的测试将被执行两次。
第一次在詹金斯构建期间,第二次在声纳执行它们时。问题是当我开始使用 clean verify 目标构建 jenkins 时。我可以看到单元测试和集成测试都已执行。在这一步之后,Sonar 开始他的工作,只执行单元测试。
编辑 3
我已经设法从控制台使用 maven 使用声纳。我在pom.xml 中添加了以下配置文件和适当的插件:
<profile>
<id>sonar</id>
<!-- Enable coverage computation via JaCoCo for Sonar's needs -->
<build>
<plugins>
<!--
Compute integration test coverage for Sonar
Note: REQUIRES MAVEN 3 - throws InstantiationException: java.util.List otherwise
Beware: Sonar doesn't run the verify phase; you should always 'mvn clean install' before running sonar
-->
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<configuration>
<propertyName>jacoco.agent.argLine</propertyName> <!-- default: argLine -->
<includes>
<include>pl/cyfronet/**</include>
</includes>
<destFile>${project.build.directory}/jacoco-integration.exec</destFile> <!-- agent -->
<dataFile>${project.build.directory}/jacoco-integration.exec</dataFile> <!-- report -->
</configuration>
<executions>
<execution>
<id>agent</id>
<goals><goal>prepare-agent</goal></goals>
</execution>
<execution>
<!--
Generate coverage report html in target/site/jacoco/ from target/jacoco.exec
Exec.: mvn verify site
-->
<id>report</id>
<phase>site</phase>
<goals><goal>report</goal></goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
我还将settings.xml 文件添加到我的~/.m2/ 目录中。
<settings>
<profiles>
<profile>
<id>sonar</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<!-- Example for MySQL-->
<sonar.jdbc.url>
jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8
</sonar.jdbc.url>
<sonar.jdbc.username>sonar</sonar.jdbc.username>
<sonar.jdbc.password>sonar</sonar.jdbc.password>
</properties>
</profile>
</profiles>
</settings>
之后,我使用以下参数运行 mvn:
mvn sonar:sonar -Dsonar.jacoco.itReportPath=target/jacoco-integration.exec。报告已正确生成到声纳。
我试图在我的詹金斯中使用它。我已经用 maven 目标 sonar:sonar -Dsonar.jacoco.itReportPath=target/jacoco-integration.exec 替换了构建后的声纳,但它不起作用。我的集成测试没有被调用。有什么想法吗?
【问题讨论】:
-
如果您必须使用配置文件来运行集成测试而不是走错路,因为 maven 具有明确的生命周期部分(集成前测试、集成测试和集成后- tests) 可以与 maven-failsafe-plugin 结合使用来运行集成测试 (
mvn clean verify)。此外,SonarQube 不运行单元测试。 Maven 运行测试,并通过 jenkins 插件或通过 maven 插件将指标传输到 SonarQube 服务器。 -
我现在已经更改了我的 pom.xml。当我调用
mvn clean verify时,我的所有测试都已执行。但尽管如此,jenkins 调用的构建是在没有集成测试的情况下执行的。 -
您如何命名您的集成测试?根据maven-failsafe-plugin的命名约定。
-
我使用
IT*.java命名约定。我添加了我的pom.xml文件的 sn-p。 -
maven-surefire-plugin 中的 exclude 不是必须的,因为它是默认的。在您的 Jenkins 配置中,您如何在那里调用 Maven?
标签: maven jenkins integration-testing sonarqube