我有一些依赖于测试数据的集成测试。此测试数据在第 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>
更好的方法是使用 skipIT 属性跳过集成前和集成后阶段的执行。
在您的情况下,它将看起来像:
<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>
因此,每当您使用 -DskipIT 运行 mvn 命令时,它都会跳过集成测试以及此插件的执行。
由存在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>