我有一些依赖于测试数据的集成测试。此测试数据在阶段pre-integration-test
中创建,并在阶段post-integration-test
中删除。
我的问题是,如果我在Maven命令行上使用-DskipITs
,这些阶段仍会执行。
有没有办法让-DskipITs
也跳过预集成测试和集成后测试阶段?
这是pom中的插件定义:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>sql-maven-plugin</artifactId>
<version>1.5</version>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
</dependencies>
<configuration>
<driver>com.mysql.jdbc.Driver</driver>
<url>${database.url}</url>
<username>${database.user}</username>
<password>${database.pw}</password>
</configuration>
<executions>
<execution>
<id>create-integration-test-data</id>
<phase>pre-integration-test</phase>
<goals>
<goal>execute</goal>
</goals>
<configuration>
<orderFile>descending</orderFile>
<fileset>
<basedir>${basedir}/src/test/resources/sql</basedir>
<includes>
<include>AdministrationTestTeardown.sql</include>
<include>AdministrationTestSetup.sql</include>
</includes>
</fileset>
</configuration>
</execution>
<execution>
<id>remove-data-after-test</id>
<phase>post-integration-test</phase>
<goals>
<goal>execute</goal>
</goals>
<configuration>
<fileset>
<basedir>${basedir}/src/test/resources/sql</basedir>
<includes>
<include>AdministrationTestTeardown.sql</include>
</includes>
</fileset>
</configuration>
</execution>
</executions>
</plugin>
答案 0 :(得分:10)
更好的方法是使用 skipITs 属性跳过预集成和放大器的执行。后整合阶段。
在您的情况下,它看起来像:
<executions>
<execution>
<id>create-integration-test-data</id>
<phase>pre-integration-test</phase>
<goals>
<goal>execute</goal>
</goals>
<configuration>
<skip>${skipITs}</skip>
<orderFile>descending</orderFile>
<fileset>
<basedir>${basedir}/src/test/resources/sql</basedir>
<includes>
<include>AdministrationTestTeardown.sql</include>
<include>AdministrationTestSetup.sql</include>
</includes>
</fileset>
</configuration>
</execution>
<execution>
<id>remove-data-after-test</id>
<phase>post-integration-test</phase>
<goals>
<goal>execute</goal>
</goals>
<configuration>
<skip>${skipITs}</skip>
<fileset>
<basedir>${basedir}/src/test/resources/sql</basedir>
<includes>
<include>AdministrationTestTeardown.sql</include>
</includes>
</fileset>
</configuration>
</execution>
</executions>
因此,每当您使用 -DskipITs 运行mvn命令时,它将跳过集成测试以及此插件的执行。
答案 1 :(得分:6)
您可以使用由skipITs
和Maven插件的<skip>
选项激活的配置文件来完全跳过插件的执行。有几种方法可以做到这一点。
您可以在配置文件中设置Maven属性,然后使用该属性跳过操作。
<profiles>
<profile>
<id>skip-integration-test-data-creation</id>
<activation>
<property>
<name>skipITs</name>
</property>
</activation>
<properties>
<skip-integration-test-data-creation>true</skip-integration-test-data-creation>
</properties>
</profile>
</profiles>
-
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>sql-maven-plugin</artifactId>
...
<skip>${skip-integration-test-data-creation}</skip>
...
</plugin>
另一种解决方案是将所有不应运行的<plugin>
部分放入单独的配置文件中,并在-DskipITs
处于活动状态时禁用该配置文件。
<profiles>
<profile>
<id>skip-integration-test-data-creation</id>
<activation>
<property>
<!-- Disable profile if skipITs is set -->
<name>!skipITs</name>
</property>
</activation>
<build>
<plugins>
<plugin>
<!-- Here: All plugins that should not run if skipITs is set -->
</plugin>
</plugins>
</build>
</profile>
</profiles>